gh-93157: Fix fileinput didn't support errors in inplace mode (GH-95128)

(cherry picked from commit 5c7f3bcdaf)

Co-authored-by: Inada Naoki <songofacandy@gmail.com>
This commit is contained in:
Miss Islington (bot) 2022-07-23 20:02:40 -07:00 committed by GitHub
parent e71e6e2ce5
commit 4a682b4f1a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 3 deletions

View file

@ -336,6 +336,16 @@ def test_inplace_binary_write_mode(self):
with open(temp_file, 'rb') as f:
self.assertEqual(f.read(), b'New line.')
def test_inplace_encoding_errors(self):
temp_file = self.writeTmp(b'Initial text \x88', mode='wb')
with FileInput(temp_file, inplace=True,
encoding="ascii", errors="replace") as fobj:
line = fobj.readline()
self.assertEqual(line, 'Initial text \ufffd')
print("New line \x88")
with open(temp_file, 'rb') as f:
self.assertEqual(f.read().rstrip(b'\r\n'), b'New line ?')
def test_file_hook_backward_compatibility(self):
def old_hook(filename, mode):
return io.StringIO("I used to receive only filename and mode")