diff --git a/newsfragments/4255.misc.rst b/newsfragments/4255.misc.rst new file mode 100644 index 0000000000..50a0a3d195 --- /dev/null +++ b/newsfragments/4255.misc.rst @@ -0,0 +1 @@ +Treat ``EncodingWarning``s as errors in tests. -- by :user:`Avasam` diff --git a/pytest.ini b/pytest.ini index e7c96274a3..0c9651d96f 100644 --- a/pytest.ini +++ b/pytest.ini @@ -10,22 +10,31 @@ filterwarnings= # Fail on warnings error + # Workarounds for pypa/setuptools#3810 + # Can't use EncodingWarning as it doesn't exist on Python 3.9. + # These warnings only appear on Python 3.10+ + ## upstream # Ensure ResourceWarnings are emitted default::ResourceWarning + # python/mypy#17057 + ignore:'encoding' argument not specified::mypy + ignore:'encoding' argument not specified::configparser + # ^-- ConfigParser is called by mypy, + # but ignoring the warning in `mypy` is not enough + # to make it work on PyPy + # realpython/pytest-mypy#152 ignore:'encoding' argument not specified::pytest_mypy - # python/cpython#100750 - ignore:'encoding' argument not specified::platform + # TODO: Set encoding when openning/writing tmpdir files with pytest's LocalPath.open + # see pypa/setuptools#4326 + ignore:'encoding' argument not specified::_pytest - # pypa/build#615 - ignore:'encoding' argument not specified::build.env - - # dateutil/dateutil#1284 - ignore:datetime.datetime.utcfromtimestamp:DeprecationWarning:dateutil.tz.tz + # Already fixed in pypa/distutils, but present in stdlib + ignore:'encoding' argument not specified::distutils ## end upstream @@ -69,11 +78,6 @@ filterwarnings= # https://github.com/pypa/setuptools/issues/3655 ignore:The --rsyncdir command line argument and rsyncdirs config variable are deprecated.:DeprecationWarning - # Workarounds for pypa/setuptools#3810 - # Can't use EncodingWarning as it doesn't exist on Python 3.9 - default:'encoding' argument not specified - default:UTF-8 Mode affects locale.getpreferredencoding(). - # Avoid errors when testing pkg_resources.declare_namespace ignore:.*pkg_resources\.declare_namespace.*:DeprecationWarning diff --git a/setuptools/command/editable_wheel.py b/setuptools/command/editable_wheel.py index 1722817f82..b8ed84750a 100644 --- a/setuptools/command/editable_wheel.py +++ b/setuptools/command/editable_wheel.py @@ -565,7 +565,8 @@ def _encode_pth(content: str) -> bytes: This function tries to simulate this behaviour without having to create an actual file, in a way that supports a range of active Python versions. (There seems to be some variety in the way different version of Python handle - ``encoding=None``, not all of them use ``locale.getpreferredencoding(False)``). + ``encoding=None``, not all of them use ``locale.getpreferredencoding(False)`` + or ``locale.getencoding()``). """ with io.BytesIO() as buffer: wrapper = io.TextIOWrapper(buffer, encoding=py39.LOCALE_ENCODING) diff --git a/setuptools/tests/__init__.py b/setuptools/tests/__init__.py index 564adf2b0a..738ebf43be 100644 --- a/setuptools/tests/__init__.py +++ b/setuptools/tests/__init__.py @@ -1,10 +1,15 @@ import locale +import sys import pytest __all__ = ['fail_on_ascii'] - -is_ascii = locale.getpreferredencoding() == 'ANSI_X3.4-1968' +locale_encoding = ( + locale.getencoding() + if sys.version_info >= (3, 11) + else locale.getpreferredencoding(False) +) +is_ascii = locale_encoding == 'ANSI_X3.4-1968' fail_on_ascii = pytest.mark.xfail(is_ascii, reason="Test fails in this locale") diff --git a/setuptools/tests/test_build_meta.py b/setuptools/tests/test_build_meta.py index 43830feb77..cc996b4255 100644 --- a/setuptools/tests/test_build_meta.py +++ b/setuptools/tests/test_build_meta.py @@ -160,7 +160,7 @@ def run(): # to obtain a distribution object first, and then run the distutils # commands later, because these files will be removed in the meantime. - with open('world.py', 'w') as f: + with open('world.py', 'w', encoding="utf-8") as f: f.write('x = 42') try: diff --git a/setuptools/tests/test_build_py.py b/setuptools/tests/test_build_py.py index 4aa1fe68fa..db2052a586 100644 --- a/setuptools/tests/test_build_py.py +++ b/setuptools/tests/test_build_py.py @@ -1,6 +1,7 @@ import os import stat import shutil +import warnings from pathlib import Path from unittest.mock import Mock @@ -162,11 +163,23 @@ def test_excluded_subpackages(tmpdir_cwd): dist.parse_config_files() build_py = dist.get_command_obj("build_py") + msg = r"Python recognizes 'mypkg\.tests' as an importable package" with pytest.warns(SetuptoolsDeprecationWarning, match=msg): # TODO: To fix #3260 we need some transition period to deprecate the # existing behavior of `include_package_data`. After the transition, we # should remove the warning and fix the behaviour. + + if os.getenv("SETUPTOOLS_USE_DISTUTILS") == "stdlib": + # pytest.warns reset the warning filter temporarily + # https://github.com/pytest-dev/pytest/issues/4011#issuecomment-423494810 + warnings.filterwarnings( + "ignore", + "'encoding' argument not specified", + module="distutils.text_file", + # This warning is already fixed in pypa/distutils but not in stdlib + ) + build_py.finalize_options() build_py.run() diff --git a/setuptools/tests/test_windows_wrappers.py b/setuptools/tests/test_windows_wrappers.py index 3f321386f1..b272689351 100644 --- a/setuptools/tests/test_windows_wrappers.py +++ b/setuptools/tests/test_windows_wrappers.py @@ -110,7 +110,11 @@ def test_basic(self, tmpdir): 'arg5 a\\\\b', ] proc = subprocess.Popen( - cmd, stdout=subprocess.PIPE, stdin=subprocess.PIPE, text=True + cmd, + stdout=subprocess.PIPE, + stdin=subprocess.PIPE, + text=True, + encoding="utf-8", ) stdout, stderr = proc.communicate('hello\nworld\n') actual = stdout.replace('\r\n', '\n') @@ -143,7 +147,11 @@ def test_symlink(self, tmpdir): 'arg5 a\\\\b', ] proc = subprocess.Popen( - cmd, stdout=subprocess.PIPE, stdin=subprocess.PIPE, text=True + cmd, + stdout=subprocess.PIPE, + stdin=subprocess.PIPE, + text=True, + encoding="utf-8", ) stdout, stderr = proc.communicate('hello\nworld\n') actual = stdout.replace('\r\n', '\n') @@ -191,6 +199,7 @@ def test_with_options(self, tmpdir): stdin=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, + encoding="utf-8", ) stdout, stderr = proc.communicate() actual = stdout.replace('\r\n', '\n') @@ -240,6 +249,7 @@ def test_basic(self, tmpdir): stdin=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, + encoding="utf-8", ) stdout, stderr = proc.communicate() assert not stdout