Original file line numberDiff line numberDiff line change@@ -3,8 +3,8 @@
33"""
4455fromerrnoimport*
6+fromioimportTextIOWrapper, text_encoding
67fromstatimportS_ISDIR, S_ISREG, S_ISLNK, S_IMODE
7-importio
88importos
99importsys
1010try:
@@ -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'notinmode
176+iftext:
177+# Call io.text_encoding() here to ensure any warning is raised at an
178+# appropriate stack level.
179+encoding=text_encoding(encoding)
175180try:
176-returnio.open(path, mode, buffering, encoding, errors, newline)
181+returnopen(path, mode, buffering, encoding, errors, newline)
177182exceptTypeError:
178183pass
179184cls=type(path)
180-text='b'notinmode
181185mode=''.join(sorted(cforcinmodeifcnotin'bt'))
182186iftext:
183187try:
@@ -200,7 +204,7 @@ def magic_open(path, mode='r', buffering=-1, encoding=None, errors=None,
200204else:
201205stream=attr(path, buffering)
202206iftext:
203-stream=io.TextIOWrapper(stream, encoding, errors, newline)
207+stream=TextIOWrapper(stream, encoding, errors, newline)
204208returnstream
205209206210raiseTypeError(f"{cls.__name__} can't be opened with mode {mode!r}")
Original file line numberDiff line numberDiff line change@@ -12,6 +12,7 @@
12121313fromabcimportABC, abstractmethod
1414fromglobimport_PathGlobber
15+fromioimporttext_encoding
1516frompathlib._osimportmagic_open, ensure_distinct_paths, ensure_different_files, copyfileobj
1617frompathlibimportPurePath, Path
1718fromtypingimportOptional, 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)
265269withmagic_open(self, mode='r', encoding=encoding, errors=errors, newline=newline) asf:
266270returnf.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)
394401ifnotisinstance(data, str):
395402raiseTypeError('data must be str, not %s'%
396403data.__class__.__name__)
Original file line numberDiff line numberDiff line change@@ -4,6 +4,7 @@
4455importcollections.abc
66importio
7+importsys
78importunittest
89910from .supportimportis_pypi
@@ -35,6 +36,17 @@ def test_open_r(self):
3536self.assertIsInstance(f, io.TextIOBase)
3637self.assertEqual(f.read(), 'this is file A\n')
40+notgetattr(sys.flags, 'warn_default_encoding', 0),
41+"Requires warn_default_encoding",
42+ )
43+deftest_open_r_encoding_warning(self):
44+p=self.root/'fileA'
45+withself.assertWarns(EncodingWarning) aswc:
46+withmagic_open(p, 'r'):
47+pass
48+self.assertEqual(wc.filename, __file__)
49+3850deftest_open_rb(self):
3951p=self.root/'fileA'
4052withmagic_open(p, 'rb') asf:
@@ -55,6 +67,16 @@ def test_read_text(self):
5567self.assertEqual(q.read_text(encoding='latin-1'), 'äbcdefg')
5668self.assertEqual(q.read_text(encoding='utf-8', errors='ignore'), 'bcdefg')
71+notgetattr(sys.flags, 'warn_default_encoding', 0),
72+"Requires warn_default_encoding",
73+ )
74+deftest_read_text_encoding_warning(self):
75+p=self.root/'fileA'
76+withself.assertWarns(EncodingWarning) aswc:
77+p.read_text()
78+self.assertEqual(wc.filename, __file__)
79+5880deftest_read_text_with_newlines(self):
5981p=self.root/'abc'
6082self.ground.create_file(p, b'abcde\r\nfghlk\n\rmnopq')
Original file line numberDiff line numberDiff line change@@ -4,6 +4,7 @@
4455importio
66importos
7+importsys
78importunittest
89910from .supportimportis_pypi
@@ -35,6 +36,17 @@ def test_open_w(self):
3536f.write('this is file A\n')
3637self.assertEqual(self.ground.readtext(p), 'this is file A\n')
40+notgetattr(sys.flags, 'warn_default_encoding', 0),
41+"Requires warn_default_encoding",
42+ )
43+deftest_open_w_encoding_warning(self):
44+p=self.root/'fileA'
45+withself.assertWarns(EncodingWarning) aswc:
46+withmagic_open(p, 'w'):
47+pass
48+self.assertEqual(wc.filename, __file__)
49+3850deftest_open_wb(self):
3951p=self.root/'fileA'
4052withmagic_open(p, 'wb') asf:
@@ -61,6 +73,16 @@ def test_write_text(self):
6173self.assertRaises(TypeError, p.write_text, b'somebytes')
6274self.assertEqual(self.ground.readbytes(p), b'\xe4bcdefg')
77+notgetattr(sys.flags, 'warn_default_encoding', 0),
78+"Requires warn_default_encoding",
79+ )
80+deftest_write_text_encoding_warning(self):
81+p=self.root/'fileA'
82+withself.assertWarns(EncodingWarning) aswc:
83+p.write_text('abcdefg')
84+self.assertEqual(wc.filename, __file__)
85+6486deftest_write_text_with_newlines(self):
6587# Check that `\n` character change nothing
6688p=self.root/'fileA'