Skip to content

Commit 7b9b09a

Browse files
authored
Use utf-8 encoding by default for all subprocess communication. (#10558)
This seems to mostly be a problem on windows. There is even a PEP out to make this the default: https://www.python.org/dev/peps/pep-0597 Fixes: #10551
1 parent 0b026ff commit 7b9b09a

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

tools/shared.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -177,19 +177,20 @@ def run_process(cmd, check=True, input=None, *args, **kw):
177177

178178
debug_text = '%sexecuted %s' % ('successfully ' if check else '', ' '.join(cmd))
179179

180-
if hasattr(subprocess, "run"):
181-
ret = subprocess.run(cmd, check=check, input=input, *args, **kw)
182-
logger.debug(debug_text)
183-
return ret
180+
if hasattr(subprocess, 'run'):
181+
# Python 3.5 and above only
182+
kw.setdefault('encoding', 'utf-8')
183+
result = subprocess.run(cmd, check=check, input=input, *args, **kw)
184+
else:
185+
# Python 2 compatibility: Introduce Python 3 subprocess.run-like behavior
186+
if input is not None:
187+
kw['stdin'] = subprocess.PIPE
188+
proc = Popen(cmd, *args, **kw)
189+
stdout, stderr = proc.communicate(input)
190+
result = Py2CompletedProcess(cmd, proc.returncode, stdout, stderr)
191+
if check:
192+
result.check_returncode()
184193

185-
# Python 2 compatibility: Introduce Python 3 subprocess.run-like behavior
186-
if input is not None:
187-
kw['stdin'] = subprocess.PIPE
188-
proc = Popen(cmd, *args, **kw)
189-
stdout, stderr = proc.communicate(input)
190-
result = Py2CompletedProcess(cmd, proc.returncode, stdout, stderr)
191-
if check:
192-
result.check_returncode()
193194
logger.debug(debug_text)
194195
return result
195196

0 commit comments

Comments
 (0)