Python2: fixed UnicodeEncodeError
git-review crashes when using it with Python 2 (the default one in Ubuntu 14.04): https://bugs.launchpad.net/git-review/+bug/1308050 It seems that in the main() method, 'cmd' can contain utf-8 chars: git-review find '(détachée' (french word for 'detached') as topic. This utf-8 string has caused a crash. I guess we should always have an utf-8 string from a git command but because I'm not so sure about that and I let you review this :-) As advised by Clark Boylan, it can be interesting to encode the string in run_command_status() method. It's just needed to 'encode' it when we have an unicode string (python 2). shlex.split needs a 'str', not an 'unicode' type with utf-8 chars, not a 'bytes' type and not not a 'bytes' type converted to 'str' (with Python 3): >>> shlex.split(str("echo héhé".encode('utf-8'))) ['becho h\\xc3\\xa9h\\xc3\\xa9'] PS: Fixed error when launching tests with Python 2.6 PS2: Used 'sys.version_info' instead of 'sys.version' (thanks to Alex Gaynor) Change-Id: Ie5bed46e562d3f9f2df54abe8b0fd6e849583df7
This commit is contained in:
parent
aab7dcae88
commit
dc71ef7553
@ -121,7 +121,11 @@ def run_command_status(*argv, **env):
|
||||
if VERBOSE:
|
||||
print(datetime.datetime.now(), "Running:", " ".join(argv))
|
||||
if len(argv) == 1:
|
||||
argv = shlex.split(str(argv[0]))
|
||||
# for python2 compatibility with shlex
|
||||
if sys.version_info < (3,) and isinstance(argv[0], unicode):
|
||||
argv = shlex.split(argv[0].encode('utf-8'))
|
||||
else:
|
||||
argv = shlex.split(str(argv[0]))
|
||||
newenv = os.environ
|
||||
newenv.update(env)
|
||||
p = subprocess.Popen(argv, stdout=subprocess.PIPE,
|
||||
|
Loading…
x
Reference in New Issue
Block a user