Skip to content

Commit 706e4b0

Browse files
author
Dominic Price
committed
Escape backslashes when passing cwd to Python on Windows
1 parent 603525c commit 706e4b0

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

frontend/gtkmm/NotebookWindow.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -648,9 +648,9 @@ void NotebookWindow::on_connect()
648648
// prefs.python_path might end in a backslash which will raise an EOF syntax error, so we add a
649649
// semicolon to the end of it and then remove the (empty) last element of the resulting list
650650
if (!trim(prefs.python_path).empty())
651-
console.send_input("sys.path = r'''" + prefs.python_path + ";'''.split(';')[:-1] + sys.path");
651+
console.send_input("sys.path = r'''" + escape_backslashes(prefs.python_path) + ";'''.split(';')[:-1] + sys.path");
652652
if (!name.empty()) {
653-
console.send_input("sys.path.insert(0, '''" + name.substr(0, name.find_last_of("\\/")) + "''')");
653+
console.send_input("sys.path.insert(0, '''" + escape_backslashes(name.substr(0, name.find_last_of("\\/"))) + "''')");
654654
}
655655
}
656656

@@ -1564,7 +1564,7 @@ void NotebookWindow::on_file_save_as()
15641564
else {
15651565
modified=false;
15661566
update_title();
1567-
console.send_input("sys.path.insert(0, r'''" + name.substr(0, name.find_last_of("\\/")) + "''')");
1567+
console.send_input("sys.path.insert(0, r'''" + escape_backslashes(name.substr(0, name.find_last_of("\\/"))) + "''')");
15681568
}
15691569
break;
15701570
}

libs/internal/include/internal/string_tools.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,13 @@ inline std::string nth_line(const std::string& s, size_t n)
4949
}
5050
return s.substr(pos, s.find('\n', pos) - pos);
5151
}
52+
53+
inline std::string escape_backslashes(std::string s)
54+
{
55+
size_t start = 0;
56+
while ((start = s.find('\\', start)) != std::string::npos) {
57+
s.replace(start, 1, "\\\\");
58+
start += 2;
59+
}
60+
return s;
61+
}

0 commit comments

Comments
 (0)