Skip to content

GH-128520: pathlib ABCs tests: use explicit text encoding #133105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/test/test_pathlib/support/local_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_pathlib/support/zip_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
10 changes: 5 additions & 5 deletions Lib/test/test_pathlib/test_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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')
Expand All @@ -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']
Expand Down
12 changes: 6 additions & 6 deletions Lib/test/test_pathlib/test_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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(
Expand All @@ -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')
Expand Down
Loading