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:05:10 -07:00 committed by GitHub
parent e8edbda897
commit 22f06d6ce3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 3 deletions

View file

@ -326,6 +326,16 @@ class FileInputTests(BaseTests, unittest.TestCase):
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")