Skip to content

Commit a3f43ba

Browse files
committed
Encode arguments to shlex in Python < 2.7.3
Signed-off-by: Sebastian Ramacher <sebastian+dev@ramacher.at>
1 parent 871d293 commit a3f43ba

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

bpython/_py3compat.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,10 @@
4242
from pygments.lexers import Python3Lexer as PythonLexer
4343
else:
4444
from pygments.lexers import PythonLexer
45+
46+
if py3 or sys.version_info[:3] >= (2, 7, 3):
47+
def prepare_for_exec(arg, encoding=None):
48+
return arg
49+
else:
50+
def prepare_for_exec(arg, encoding=None):
51+
return arg.encode(encoding)

bpython/repl.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
from pygments.token import Token
4646

4747
from bpython import inspection
48-
from bpython._py3compat import PythonLexer, py3
48+
from bpython._py3compat import PythonLexer, py3, prepare_for_exec
4949
from bpython.formatter import Parenthesis
5050
from bpython.translations import _, ngettext
5151
from bpython.clipboard import get_clipboard, CopyFailed
@@ -1029,22 +1029,30 @@ def clear_current_line(self):
10291029
def send_to_external_editor(self, text, filename=None):
10301030
"""Returns modified text from an editor, or the oriignal text if editor
10311031
exited with non-zero"""
1032-
editor_args = shlex.split(self.config.editor)
1032+
1033+
encoding = getpreferredencoding()
1034+
editor_args = shlex.split(prepare_for_exec(self.config.editor,
1035+
encoding))
10331036
with tempfile.NamedTemporaryFile(suffix='.py') as temp:
1034-
temp.write(text.encode(getpreferredencoding()))
1037+
temp.write(text.encode(encoding))
10351038
temp.flush()
1036-
if subprocess.call(editor_args + [temp.name]) == 0:
1039+
1040+
args = editor_args + [prepare_for_exec(temp.name, encoding)]
1041+
if subprocess.call(args) == 0:
10371042
with open(temp.name) as f:
10381043
if py3:
10391044
return f.read()
10401045
else:
1041-
return f.read().decode(getpreferredencoding())
1046+
return f.read().decode(encoding)
10421047
else:
10431048
return text
10441049

10451050
def open_in_external_editor(self, filename):
1046-
editor_args = shlex.split(self.config.editor)
1047-
if subprocess.call(editor_args + [filename]) == 0:
1051+
encoding = getpreferredencoding()
1052+
editor_args = shlex.split(prepare_for_exec(self.config.editor,
1053+
encoding))
1054+
args = editor_args + [prepare_for_exec(filename, encoding)]
1055+
if subprocess.call(args) == 0:
10481056
return True
10491057
return False
10501058

0 commit comments

Comments
 (0)