Skip to content

Commit 6aff23d

Browse files
committed
BUG: when given unicode inputs, arg_split should return unicode outputs. Always use utf-8 to encode the string instead of relying on sys.stdin.encoding, which may not be able to accept the full range of Unicode characters. When given unicode strings, arg_split is probably not receiving input from a terminal.
1 parent ce087d6 commit 6aff23d

2 files changed

Lines changed: 11 additions & 2 deletions

File tree

IPython/utils/process.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,17 @@ def arg_split(s, posix=False):
114114
# http://bugs.python.org/issue1170
115115
# At least encoding the input when it's unicode seems to help, but there
116116
# may be more problems lurking. Apparently this is fixed in python3.
117+
is_unicode = False
117118
if isinstance(s, unicode):
118-
s = s.encode(sys.stdin.encoding)
119+
is_unicode = True
120+
s = s.encode('utf-8')
119121
lex = shlex.shlex(s, posix=posix)
120122
lex.whitespace_split = True
121-
return list(lex)
123+
tokens = list(lex)
124+
if is_unicode:
125+
# Convert the tokens back to unicode.
126+
tokens = [x.decode('utf-8') for x in tokens]
127+
return tokens
122128

123129

124130
def abbrev_cwd():

IPython/utils/tests/test_process.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ def test_arg_split():
6666
"""Ensure that argument lines are correctly split like in a shell."""
6767
tests = [['hi', ['hi']],
6868
[u'hi', [u'hi']],
69+
['hello there', ['hello', 'there']],
70+
[u'h\N{LATIN SMALL LETTER A WITH CARON}llo', [u'h\N{LATIN SMALL LETTER A WITH CARON}llo']],
71+
['something "with quotes"', ['something', '"with quotes"']],
6972
]
7073
for argstr, argv in tests:
7174
nt.assert_equal(arg_split(argstr), argv)

0 commit comments

Comments
 (0)