https://github.com/python/cpython/commit/336322b34137d90b0db19545c09a77f050ba8de0 commit: 336322b34137d90b0db19545c09a77f050ba8de0 branch: main author: Barney Gale <barney.g...@gmail.com> committer: barneygale <barney.g...@gmail.com> date: 2025-04-28T20:18:56+01:00 summary:
GH-128520: pathlib ABCs tests: use explicit text encoding (#133105) Follow-up to fbffd70. Set `encoding='utf-8'` when reading and writing text in the tests for the private pathlib ABCs, which allows the tests to run with `-W error -X warn_default_encoding` files: M Lib/test/test_pathlib/support/local_path.py M Lib/test/test_pathlib/support/zip_path.py M Lib/test/test_pathlib/test_read.py M Lib/test/test_pathlib/test_write.py diff --git a/Lib/test/test_pathlib/support/local_path.py b/Lib/test/test_pathlib/support/local_path.py index 4f027754f6a6e1..d481fd45ead49f 100644 --- a/Lib/test/test_pathlib/support/local_path.py +++ b/Lib/test/test_pathlib/support/local_path.py @@ -82,7 +82,7 @@ def create_hierarchy(self, p): readlink = staticmethod(os.readlink) def readtext(self, p): - with open(p, 'r') as f: + with open(p, 'r', encoding='utf-8') as f: return f.read() def readbytes(self, p): diff --git a/Lib/test/test_pathlib/support/zip_path.py b/Lib/test/test_pathlib/support/zip_path.py index 242cab1509627b..2905260c9dfc95 100644 --- a/Lib/test/test_pathlib/support/zip_path.py +++ b/Lib/test/test_pathlib/support/zip_path.py @@ -63,7 +63,7 @@ def create_hierarchy(self, p): def readtext(self, p): with p.zip_file.open(str(p), 'r') as f: - f = io.TextIOWrapper(f) + f = io.TextIOWrapper(f, encoding='utf-8') return f.read() def readbytes(self, p): diff --git a/Lib/test/test_pathlib/test_read.py b/Lib/test/test_pathlib/test_read.py index 9bb5535a6eb310..482203c290a3c4 100644 --- a/Lib/test/test_pathlib/test_read.py +++ b/Lib/test/test_pathlib/test_read.py @@ -32,7 +32,7 @@ def test_is_readable(self): def test_open_r(self): p = self.root / 'fileA' - with magic_open(p, 'r') as f: + with magic_open(p, 'r', encoding='utf-8') as f: self.assertIsInstance(f, io.TextIOBase) self.assertEqual(f.read(), 'this is file A\n') @@ -61,7 +61,7 @@ def test_read_bytes(self): def test_read_text(self): p = self.root / 'fileA' - self.assertEqual(p.read_text(), 'this is file A\n') + self.assertEqual(p.read_text(encoding='utf-8'), 'this is file A\n') q = self.root / 'abc' self.ground.create_file(q, b'\xe4bcdefg') self.assertEqual(q.read_text(encoding='latin-1'), 'äbcdefg') @@ -81,11 +81,11 @@ def test_read_text_with_newlines(self): p = self.root / 'abc' self.ground.create_file(p, b'abcde\r\nfghlk\n\rmnopq') # Check that `\n` character change nothing - self.assertEqual(p.read_text(newline='\n'), 'abcde\r\nfghlk\n\rmnopq') + self.assertEqual(p.read_text(encoding='utf-8', newline='\n'), 'abcde\r\nfghlk\n\rmnopq') # Check that `\r` character replaces `\n` - self.assertEqual(p.read_text(newline='\r'), 'abcde\r\nfghlk\n\rmnopq') + self.assertEqual(p.read_text(encoding='utf-8', newline='\r'), 'abcde\r\nfghlk\n\rmnopq') # Check that `\r\n` character replaces `\n` - self.assertEqual(p.read_text(newline='\r\n'), 'abcde\r\nfghlk\n\rmnopq') + self.assertEqual(p.read_text(encoding='utf-8', newline='\r\n'), 'abcde\r\nfghlk\n\rmnopq') def test_iterdir(self): expected = ['dirA', 'dirB', 'dirC', 'fileA'] diff --git a/Lib/test/test_pathlib/test_write.py b/Lib/test/test_pathlib/test_write.py index 2f3c06b433d224..b958490d0a834f 100644 --- a/Lib/test/test_pathlib/test_write.py +++ b/Lib/test/test_pathlib/test_write.py @@ -31,7 +31,7 @@ def test_is_writable(self): def test_open_w(self): p = self.root / 'fileA' - with magic_open(p, 'w') as f: + with magic_open(p, 'w', encoding='utf-8') as f: self.assertIsInstance(f, io.TextIOBase) f.write('this is file A\n') self.assertEqual(self.ground.readtext(p), 'this is file A\n') @@ -70,7 +70,7 @@ def test_write_text(self): p.write_text('äbcdefg', encoding='latin-1') self.assertEqual(self.ground.readbytes(p), b'\xe4bcdefg') # Check that trying to write bytes does not truncate the file. - self.assertRaises(TypeError, p.write_text, b'somebytes') + self.assertRaises(TypeError, p.write_text, b'somebytes', encoding='utf-8') self.assertEqual(self.ground.readbytes(p), b'\xe4bcdefg') @unittest.skipIf( @@ -86,23 +86,23 @@ def test_write_text_encoding_warning(self): def test_write_text_with_newlines(self): # Check that `\n` character change nothing p = self.root / 'fileA' - p.write_text('abcde\r\nfghlk\n\rmnopq', newline='\n') + p.write_text('abcde\r\nfghlk\n\rmnopq', encoding='utf-8', newline='\n') self.assertEqual(self.ground.readbytes(p), b'abcde\r\nfghlk\n\rmnopq') # Check that `\r` character replaces `\n` p = self.root / 'fileB' - p.write_text('abcde\r\nfghlk\n\rmnopq', newline='\r') + p.write_text('abcde\r\nfghlk\n\rmnopq', encoding='utf-8', newline='\r') self.assertEqual(self.ground.readbytes(p), b'abcde\r\rfghlk\r\rmnopq') # Check that `\r\n` character replaces `\n` p = self.root / 'fileC' - p.write_text('abcde\r\nfghlk\n\rmnopq', newline='\r\n') + p.write_text('abcde\r\nfghlk\n\rmnopq', encoding='utf-8', newline='\r\n') self.assertEqual(self.ground.readbytes(p), b'abcde\r\r\nfghlk\r\n\rmnopq') # Check that no argument passed will change `\n` to `os.linesep` os_linesep_byte = bytes(os.linesep, encoding='ascii') p = self.root / 'fileD' - p.write_text('abcde\nfghlk\n\rmnopq') + p.write_text('abcde\nfghlk\n\rmnopq', encoding='utf-8') self.assertEqual(self.ground.readbytes(p), b'abcde' + os_linesep_byte + b'fghlk' + os_linesep_byte + b'\rmnopq') _______________________________________________ Python-checkins mailing list -- python-checkins@python.org To unsubscribe send an email to python-checkins-le...@python.org https://mail.python.org/mailman3/lists/python-checkins.python.org/ Member address: arch...@mail-archive.com