Skip to content

Commit 5782aab

Browse files
authored
Merge pull request #11648 from bluetech/fix-locale-encoding
pytester: avoid EncodingWarning from `locale.getpreferredencoding`
2 parents 172bf89 + ad1bccd commit 5782aab

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

src/_pytest/pytester.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,18 @@ def pytest_configure(config: Config) -> None:
121121

122122
class LsofFdLeakChecker:
123123
def get_open_files(self) -> List[Tuple[str, str]]:
124+
if sys.version_info >= (3, 11):
125+
# New in Python 3.11, ignores utf-8 mode
126+
encoding = locale.getencoding()
127+
else:
128+
encoding = locale.getpreferredencoding(False)
124129
out = subprocess.run(
125130
("lsof", "-Ffn0", "-p", str(os.getpid())),
126131
stdout=subprocess.PIPE,
127132
stderr=subprocess.DEVNULL,
128133
check=True,
129134
text=True,
130-
encoding=locale.getpreferredencoding(False),
135+
encoding=encoding,
131136
).stdout
132137

133138
def isopen(line: str) -> bool:

testing/test_parseopt.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,10 +290,10 @@ def test_multiple_metavar_help(self, parser: parseopt.Parser) -> None:
290290

291291

292292
def test_argcomplete(pytester: Pytester, monkeypatch: MonkeyPatch) -> None:
293-
try:
293+
if sys.version_info >= (3, 11):
294294
# New in Python 3.11, ignores utf-8 mode
295-
encoding = locale.getencoding() # type: ignore[attr-defined]
296-
except AttributeError:
295+
encoding = locale.getencoding()
296+
else:
297297
encoding = locale.getpreferredencoding(False)
298298
try:
299299
bash_version = subprocess.run(

0 commit comments

Comments
 (0)