8000 fix: add encoding arguments by henryiii · Pull Request #1967 · nedbat/coveragepy · GitHub
[go: up one dir, main page]

Skip to content

fix: add encoding arguments #1967

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 16, 2025
Merged

Conversation

henryiii
Copy link
Contributor

This fixes #1966 by adding encoding="utf-8" everywhere. See https://peps.python.org/pep-0597/.

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
@henryiii henryiii force-pushed the henryiii/fix/encoding branch from d096fe4 to 2bfdb5f Compare May 14, 2025 21:34
@nedbat
Copy link
Owner
nedbat commented May 15, 2025

Thanks for starting this. It's turning out to be a real puzzle how to silence them all... I'm trying to tweak this to get it right.

@henryiii
Copy link
Contributor Author
henryiii commented May 15, 2025

I can try to follow up, but traveling for PyCon is getting in the way. :) I think we just have to fix 3.9 and 3.10.

@nedbat
Copy link
Owner
nedbat commented May 16, 2025

This gets us closer:

diff --git a/tests/helpers.py b/tests/helpers.py
index b53ee53a..27d757da 100644
--- a/tests/helpers.py
+++ b/tests/helpers.py
@@ -43,12 +43,14 @@ def run_command(cmd: str) -> tuple[int, str]:
     # Subprocesses are expensive, but convenient, and so may be over-used in
     # the test suite.  Use these lines to get a list of the tests using them:
     if 0:  # pragma: debugging
-        pth = "/tmp/processes.txt"
-        with open(pth, "a", encoding="utf-8") as proctxt:  # type: ignore[unreachable]
+        pth = "/tmp/processes.txt"                      # type: ignore[unreachable]
+        with open(pth, "a", encoding="utf-8") as proctxt:
             print(os.getenv("PYTEST_CURRENT_TEST", "unknown"), file=proctxt, flush=True)

     encoding = os.device_encoding(1) or (
-        locale.getdefaultencoding() if sys.version_info < (3, 10) else locale.getencoding()
+        locale.getpreferredencoding()
+        if sys.version_info < (3, 11)
+        else locale.getencoding()   # type: ignore
     )

     # In some strange cases (PyPy3 in a virtualenv!?) the stdout encoding of
diff --git a/tox.ini b/tox.ini
index e0c5dbee..dbc0f1f9 100644
--- a/tox.ini
+++ b/tox.ini
@@ -28,7 +28,14 @@ setenv =
     pypy3{,9,10,11}: COVERAGE_TEST_CORES=pytrace
     # If we ever need a stronger way to suppress warnings:
     #PYTHONWARNINGS=ignore:removed in Python 3.14; use ast.Constant:DeprecationWarning
-    PYTHONWARNDEFAULTENCODING=1
+    # We want to know about missing encoding arguments, but we need to silence
+    # some warnings that aren't ours.  We can't silence them in 3.9 because
+    # EncodingWarning doesn't exist yet, and it's hard to suppress them in some
+    # environments and not others.  So by default, don't warn, and will enable
+    # the warning in a handful of environments to catch the problems.
+    PYTHONWARNDEFAULTENCODING=
+    py3{10,11,12,13,14}: PYTHONWARNDEFAULTENCODING=1
+    py3{10,11,12,13,14}: PYTHONWARNINGS=ignore::EncodingWarning:pip._internal.utils.subprocess
     # Disable CPython's color output
     PYTHON_COLORS=0

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
henryiii and others added 2 commits May 16, 2025 00:12
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
@henryiii henryiii marked this pull request as ready for review May 16, 2025 05:17
@nedbat
Copy link
Owner
nedbat commented May 16, 2025

Thanks! I have one more warning to suppress that I'll do on master after merging this.

@nedbat nedbat merged commit 2f1da95 into nedbat:master May 16, 2025
44 checks passed
@nedbat
Copy link
Owner
nedbat commented May 16, 2025

One more tweak is in commit 6999a4e.

@nedbat
Copy link
Owner
nedbat commented May 21, 2025

This is now released as part of coverage 7.8.1.

@henryiii
Copy link
Contributor Author

Thanks! I can verify build's test suite now passes without warnings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PYTHONWARNDEFAULTENCODING causes unavoidable warnings
2 participants
0