Skip to content

Commit fbffd70

Browse files
authored
GH-128520: pathlib ABCs: raise text encoding warnings at correct stack level (#133051)
Ensure that warnings about unspecified text encodings are emitted from `ReadablePath.read_text()`, `WritablePath.write_text()` and `magic_open()` with the correct stack level set.
1 parent 6f04325 commit fbffd70

File tree

4 files changed

+59
-4
lines changed

4 files changed

+59
-4
lines changed

Lib/pathlib/_os.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"""
44

55
from errno import *
6+
from io import TextIOWrapper, text_encoding
67
from stat import S_ISDIR, S_ISREG, S_ISLNK, S_IMODE
7-
import io
88
import os
99
import sys
1010
try:
@@ -172,12 +172,16 @@ def magic_open(path, mode='r', buffering=-1, encoding=None, errors=None,
172172
Open the file pointed to by this path and return a file object, as
173173
the built-in open() function does.
174174
"""
175+
text = 'b' not in mode
176+
if text:
177+
# Call io.text_encoding() here to ensure any warning is raised at an
178+
# appropriate stack level.
179+
encoding = text_encoding(encoding)
175180
try:
176-
return io.open(path, mode, buffering, encoding, errors, newline)
181+
return open(path, mode, buffering, encoding, errors, newline)
177182
except TypeError:
178183
pass
179184
cls = type(path)
180-
text = 'b' not in mode
181185
mode = ''.join(sorted(c for c in mode if c not in 'bt'))
182186
if text:
183187
try:
@@ -200,7 +204,7 @@ def magic_open(path, mode='r', buffering=-1, encoding=None, errors=None,
200204
else:
201205
stream = attr(path, buffering)
202206
if text:
203-
stream = io.TextIOWrapper(stream, encoding, errors, newline)
207+
stream = TextIOWrapper(stream, encoding, errors, newline)
204208
return stream
205209

206210
raise TypeError(f"{cls.__name__} can't be opened with mode {mode!r}")

Lib/pathlib/types.py

+7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
from abc import ABC, abstractmethod
1414
from glob import _PathGlobber
15+
from io import text_encoding
1516
from pathlib._os import magic_open, ensure_distinct_paths, ensure_different_files, copyfileobj
1617
from pathlib import PurePath, Path
1718
from typing import Optional, Protocol, runtime_checkable
@@ -262,6 +263,9 @@ def read_text(self, encoding=None, errors=None, newline=None):
262263
"""
263264
Open the file in text mode, read it, and close the file.
264265
"""
266+
# Call io.text_encoding() here to ensure any warning is raised at an
267+
# appropriate stack level.
268+
encoding = text_encoding(encoding)
265269
with magic_open(self, mode='r', encoding=encoding, errors=errors, newline=newline) as f:
266270
return f.read()
267271

@@ -391,6 +395,9 @@ def write_text(self, data, encoding=None, errors=None, newline=None):
391395
"""
392396
Open the file in text mode, write to it, and close the file.
393397
"""
398+
# Call io.text_encoding() here to ensure any warning is raised at an
399+
# appropriate stack level.
400+
encoding = text_encoding(encoding)
394401
if not isinstance(data, str):
395402
raise TypeError('data must be str, not %s' %
396403
data.__class__.__name__)

Lib/test/test_pathlib/test_read.py

+22
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import collections.abc
66
import io
7+
import sys
78
import unittest
89

910
from .support import is_pypi
@@ -35,6 +36,17 @@ def test_open_r(self):
3536
self.assertIsInstance(f, io.TextIOBase)
3637
self.assertEqual(f.read(), 'this is file A\n')
3738

39+
@unittest.skipIf(
40+
not getattr(sys.flags, 'warn_default_encoding', 0),
41+
"Requires warn_default_encoding",
42+
)
43+
def test_open_r_encoding_warning(self):
44+
p = self.root / 'fileA'
45+
with self.assertWarns(EncodingWarning) as wc:
46+
with magic_open(p, 'r'):
47+
pass
48+
self.assertEqual(wc.filename, __file__)
49+
3850
def test_open_rb(self):
3951
p = self.root / 'fileA'
4052
with magic_open(p, 'rb') as f:
@@ -55,6 +67,16 @@ def test_read_text(self):
5567
self.assertEqual(q.read_text(encoding='latin-1'), 'äbcdefg')
5668
self.assertEqual(q.read_text(encoding='utf-8', errors='ignore'), 'bcdefg')
5769

70+
@unittest.skipIf(
71+
not getattr(sys.flags, 'warn_default_encoding', 0),
72+
"Requires warn_default_encoding",
73+
)
74+
def test_read_text_encoding_warning(self):
75+
p = self.root / 'fileA'
76+
with self.assertWarns(EncodingWarning) as wc:
77+
p.read_text()
78+
self.assertEqual(wc.filename, __file__)
79+
5880
def test_read_text_with_newlines(self):
5981
p = self.root / 'abc'
6082
self.ground.create_file(p, b'abcde\r\nfghlk\n\rmnopq')

Lib/test/test_pathlib/test_write.py

+22
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import io
66
import os
7+
import sys
78
import unittest
89

910
from .support import is_pypi
@@ -35,6 +36,17 @@ def test_open_w(self):
3536
f.write('this is file A\n')
3637
self.assertEqual(self.ground.readtext(p), 'this is file A\n')
3738

39+
@unittest.skipIf(
40+
not getattr(sys.flags, 'warn_default_encoding', 0),
41+
"Requires warn_default_encoding",
42+
)
43+
def test_open_w_encoding_warning(self):
44+
p = self.root / 'fileA'
45+
with self.assertWarns(EncodingWarning) as wc:
46+
with magic_open(p, 'w'):
47+
pass
48+
self.assertEqual(wc.filename, __file__)
49+
3850
def test_open_wb(self):
3951
p = self.root / 'fileA'
4052
with magic_open(p, 'wb') as f:
@@ -61,6 +73,16 @@ def test_write_text(self):
6173
self.assertRaises(TypeError, p.write_text, b'somebytes')
6274
self.assertEqual(self.ground.readbytes(p), b'\xe4bcdefg')
6375

76+
@unittest.skipIf(
77+
not getattr(sys.flags, 'warn_default_encoding', 0),
78+
"Requires warn_default_encoding",
79+
)
80+
def test_write_text_encoding_warning(self):
81+
p = self.root / 'fileA'
82+
with self.assertWarns(EncodingWarning) as wc:
83+
p.write_text('abcdefg')
84+
self.assertEqual(wc.filename, __file__)
85+
6486
def test_write_text_with_newlines(self):
6587
# Check that `\n` character change nothing
6688
p = self.root / 'fileA'

0 commit comments

Comments
 (0)