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`
This commit is contained in:
Barney Gale 2025-04-28 20:18:56 +01:00 committed by GitHub
parent 606003ffa9
commit 336322b341
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 13 additions and 13 deletions

View file

@ -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')