Fix for cases where commands output UTF8 in Windows.

This commit is contained in:
Emilio Graff 2023-07-28 12:36:33 -07:00
parent 2bffc0ad85
commit 54b2d982f3
2 changed files with 19 additions and 0 deletions

View file

@ -93,6 +93,15 @@ def cmd_output_b(
) -> tuple[int, bytes, bytes | None]: ) -> tuple[int, bytes, bytes | None]:
_setdefault_kwargs(kwargs) _setdefault_kwargs(kwargs)
if sys.platform == 'win32':
# In windows, pipes use CP1252 by default, and you'll get an
# exception if the command being run outputs unicode. So we
# set the pipe encoding to utf-8 instead.
#
# See https://stackoverflow.com/a/74607949/149506
import os
os.environ['PYTHONIOENCODING'] = 'utf-8'
try: try:
cmd = parse_shebang.normalize_cmd(cmd, env=kwargs.get('env')) cmd = parse_shebang.normalize_cmd(cmd, env=kwargs.get('env'))
except parse_shebang.ExecutableNotFoundError as e: except parse_shebang.ExecutableNotFoundError as e:

View file

@ -3,6 +3,7 @@ from __future__ import annotations
import os.path import os.path
import stat import stat
import subprocess import subprocess
import sys
import pytest import pytest
@ -106,3 +107,12 @@ def test_rmtree_read_only_directories(tmpdir):
tmpdir.join('x/y/z').chmod(mode_no_w) tmpdir.join('x/y/z').chmod(mode_no_w)
tmpdir.join('x/y/z').chmod(mode_no_w) tmpdir.join('x/y/z').chmod(mode_no_w)
rmtree(str(tmpdir.join('x'))) rmtree(str(tmpdir.join('x')))
@pytest.mark.parametrize('fn', (cmd_output_b, cmd_output_p))
def test_cmd_output_utf8(fn):
"""Makes sure `cmd_output_*` works if the command being
run outputs UTF8 characters."""
ret, out, _ = fn(f'{sys.executable}', '-c', 'print("")')
assert ret == 0
assert out.strip().decode() == ''