Skip to content

Commit 336322b

Browse files
authored
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`
1 parent 606003f commit 336322b

File tree

4 files changed

+13
-13
lines changed

4 files changed

+13
-13
lines changed

Lib/test/test_pathlib/support/local_path.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def create_hierarchy(self, p):
8282
readlink = staticmethod(os.readlink)
8383

8484
def readtext(self, p):
85-
with open(p, 'r') as f:
85+
with open(p, 'r', encoding='utf-8') as f:
8686
return f.read()
8787

8888
def readbytes(self, p):

Lib/test/test_pathlib/support/zip_path.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def create_hierarchy(self, p):
6363

6464
def readtext(self, p):
6565
with p.zip_file.open(str(p), 'r') as f:
66-
f = io.TextIOWrapper(f)
66+
f = io.TextIOWrapper(f, encoding='utf-8')
6767
return f.read()
6868

6969
def readbytes(self, p):

Lib/test/test_pathlib/test_read.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def test_is_readable(self):
3232

3333
def test_open_r(self):
3434
p = self.root / 'fileA'
35-
with magic_open(p, 'r') as f:
35+
with magic_open(p, 'r', encoding='utf-8') as f:
3636
self.assertIsInstance(f, io.TextIOBase)
3737
self.assertEqual(f.read(), 'this is file A\n')
3838

@@ -61,7 +61,7 @@ def test_read_bytes(self):
6161

6262
def test_read_text(self):
6363
p = self.root / 'fileA'
64-
self.assertEqual(p.read_text(), 'this is file A\n')
64+
self.assertEqual(p.read_text(encoding='utf-8'), 'this is file A\n')
6565
q = self.root / 'abc'
6666
self.ground.create_file(q, b'\xe4bcdefg')
6767
self.assertEqual(q.read_text(encoding='latin-1'), 'äbcdefg')
@@ -81,11 +81,11 @@ def test_read_text_with_newlines(self):
8181
p = self.root / 'abc'
8282
self.ground.create_file(p, b'abcde\r\nfghlk\n\rmnopq')
8383
# Check that `\n` character change nothing
84-
self.assertEqual(p.read_text(newline='\n'), 'abcde\r\nfghlk\n\rmnopq')
84+
self.assertEqual(p.read_text(encoding='utf-8', newline='\n'), 'abcde\r\nfghlk\n\rmnopq')
8585
# Check that `\r` character replaces `\n`
86-
self.assertEqual(p.read_text(newline='\r'), 'abcde\r\nfghlk\n\rmnopq')
86+
self.assertEqual(p.read_text(encoding='utf-8', newline='\r'), 'abcde\r\nfghlk\n\rmnopq')
8787
# Check that `\r\n` character replaces `\n`
88-
self.assertEqual(p.read_text(newline='\r\n'), 'abcde\r\nfghlk\n\rmnopq')
88+
self.assertEqual(p.read_text(encoding='utf-8', newline='\r\n'), 'abcde\r\nfghlk\n\rmnopq')
8989

9090
def test_iterdir(self):
9191
expected = ['dirA', 'dirB', 'dirC', 'fileA']

Lib/test/test_pathlib/test_write.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def test_is_writable(self):
3131

3232
def test_open_w(self):
3333
p = self.root / 'fileA'
34-
with magic_open(p, 'w') as f:
34+
with magic_open(p, 'w', encoding='utf-8') as f:
3535
self.assertIsInstance(f, io.TextIOBase)
3636
f.write('this is file A\n')
3737
self.assertEqual(self.ground.readtext(p), 'this is file A\n')
@@ -70,7 +70,7 @@ def test_write_text(self):
7070
p.write_text('äbcdefg', encoding='latin-1')
7171
self.assertEqual(self.ground.readbytes(p), b'\xe4bcdefg')
7272
# Check that trying to write bytes does not truncate the file.
73-
self.assertRaises(TypeError, p.write_text, b'somebytes')
73+
self.assertRaises(TypeError, p.write_text, b'somebytes', encoding='utf-8')
7474
self.assertEqual(self.ground.readbytes(p), b'\xe4bcdefg')
7575

7676
@unittest.skipIf(
@@ -86,23 +86,23 @@ def test_write_text_encoding_warning(self):
8686
def test_write_text_with_newlines(self):
8787
# Check that `\n` character change nothing
8888
p = self.root / 'fileA'
89-
p.write_text('abcde\r\nfghlk\n\rmnopq', newline='\n')
89+
p.write_text('abcde\r\nfghlk\n\rmnopq', encoding='utf-8', newline='\n')
9090
self.assertEqual(self.ground.readbytes(p), b'abcde\r\nfghlk\n\rmnopq')
9191

9292
# Check that `\r` character replaces `\n`
9393
p = self.root / 'fileB'
94-
p.write_text('abcde\r\nfghlk\n\rmnopq', newline='\r')
94+
p.write_text('abcde\r\nfghlk\n\rmnopq', encoding='utf-8', newline='\r')
9595
self.assertEqual(self.ground.readbytes(p), b'abcde\r\rfghlk\r\rmnopq')
9696

9797
# Check that `\r\n` character replaces `\n`
9898
p = self.root / 'fileC'
99-
p.write_text('abcde\r\nfghlk\n\rmnopq', newline='\r\n')
99+
p.write_text('abcde\r\nfghlk\n\rmnopq', encoding='utf-8', newline='\r\n')
100100
self.assertEqual(self.ground.readbytes(p), b'abcde\r\r\nfghlk\r\n\rmnopq')
101101

102102
# Check that no argument passed will change `\n` to `os.linesep`
103103
os_linesep_byte = bytes(os.linesep, encoding='ascii')
104104
p = self.root / 'fileD'
105-
p.write_text('abcde\nfghlk\n\rmnopq')
105+
p.write_text('abcde\nfghlk\n\rmnopq', encoding='utf-8')
106106
self.assertEqual(self.ground.readbytes(p),
107107
b'abcde' + os_linesep_byte +
108108
b'fghlk' + os_linesep_byte + b'\rmnopq')

0 commit comments

Comments
 (0)