Skip to content

Commit 2040bce

Browse files
devdanzinnedbat
andauthored
test(fix): use os.device_encoding(0) to figure out stdin encoding in run_command() (Fix #1777) (#1780)
* Use os.device_encoding(0) to figure out stdin encoding in run_command(). * Use the encoding of stdout (fd 1) to decode the output of subprocess.Popen. Co-authored-by: Ned Batchelder <[email protected]> * refactor to remove a helper used in only one place --------- Co-authored-by: Ned Batchelder <[email protected]>
1 parent dfcd804 commit 2040bce

File tree

2 files changed

+6
-17
lines changed

2 files changed

+6
-17
lines changed

coverage/misc.py

+1-14
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import importlib
1414
import importlib.util
1515
import inspect
16-
import locale
1716
import os
1817
import os.path
1918
import re
@@ -22,7 +21,7 @@
2221

2322
from types import ModuleType
2423
from typing import (
25-
Any, IO, Iterable, Iterator, Mapping, NoReturn, Sequence, TypeVar,
24+
Any, Iterable, Iterator, Mapping, NoReturn, Sequence, TypeVar,
2625
)
2726

2827
from coverage.exceptions import CoverageException
@@ -156,18 +155,6 @@ def ensure_dir_for_file(path: str) -> None:
156155
ensure_dir(os.path.dirname(path))
157156

158157

159-
def output_encoding(outfile: IO[str] | None = None) -> str:
160-
"""Determine the encoding to use for output written to `outfile` or stdout."""
161-
if outfile is None:
162-
outfile = sys.stdout
163-
encoding = (
164-
getattr(outfile, "encoding", None) or
165-
getattr(sys.__stdout__, "encoding", None) or
166-
locale.getpreferredencoding()
167-
)
168-
return encoding
169-
170-
171158
class Hasher:
172159
"""Hashes Python data for fingerprinting."""
173160
def __init__(self) -> None:

tests/helpers.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import contextlib
1010
import dis
1111
import io
12+
import locale
1213
import os
1314
import os.path
1415
import re
@@ -28,7 +29,6 @@
2829
from coverage import env
2930
from coverage.debug import DebugControl
3031
from coverage.exceptions import CoverageWarning
31-
from coverage.misc import output_encoding
3232
from coverage.types import TArc, TLineNo
3333

3434

@@ -44,11 +44,13 @@ def run_command(cmd: str) -> tuple[int, str]:
4444
with open("/tmp/processes.txt", "a") as proctxt: # type: ignore[unreachable]
4545
print(os.getenv("PYTEST_CURRENT_TEST", "unknown"), file=proctxt, flush=True)
4646

47+
encoding = os.device_encoding(1) or locale.getpreferredencoding()
48+
4749
# In some strange cases (PyPy3 in a virtualenv!?) the stdout encoding of
4850
# the subprocess is set incorrectly to ascii. Use an environment variable
4951
# to force the encoding to be the same as ours.
5052
sub_env = dict(os.environ)
51-
sub_env['PYTHONIOENCODING'] = output_encoding()
53+
sub_env['PYTHONIOENCODING'] = encoding
5254

5355
proc = subprocess.Popen(
5456
cmd,
@@ -62,7 +64,7 @@ def run_command(cmd: str) -> tuple[int, str]:
6264
status = proc.returncode
6365

6466
# Get the output, and canonicalize it to strings with newlines.
65-
output_str = output.decode(output_encoding()).replace("\r", "")
67+
output_str = output.decode(encoding).replace("\r", "")
6668
return status, output_str
6769

6870

0 commit comments

Comments
 (0)