diff --git a/src/_pytest/pytester.py b/src/_pytest/pytester.py index 99d33954d..f93e9c94a 100644 --- a/src/_pytest/pytester.py +++ b/src/_pytest/pytester.py @@ -121,13 +121,18 @@ def pytest_configure(config: Config) -> None: class LsofFdLeakChecker: def get_open_files(self) -> List[Tuple[str, str]]: + if sys.version_info >= (3, 11): + # New in Python 3.11, ignores utf-8 mode + encoding = locale.getencoding() + else: + encoding = locale.getpreferredencoding(False) out = subprocess.run( ("lsof", "-Ffn0", "-p", str(os.getpid())), stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, check=True, text=True, - encoding=locale.getpreferredencoding(False), + encoding=encoding, ).stdout def isopen(line: str) -> bool: diff --git a/testing/test_parseopt.py b/testing/test_parseopt.py index b6df035aa..1b80883ee 100644 --- a/testing/test_parseopt.py +++ b/testing/test_parseopt.py @@ -290,10 +290,10 @@ class TestParser: def test_argcomplete(pytester: Pytester, monkeypatch: MonkeyPatch) -> None: - try: + if sys.version_info >= (3, 11): # New in Python 3.11, ignores utf-8 mode - encoding = locale.getencoding() # type: ignore[attr-defined] - except AttributeError: + encoding = locale.getencoding() + else: encoding = locale.getpreferredencoding(False) try: bash_version = subprocess.run(