From b13a0d2301643f63a424b6d9543ab7d1a587b688 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Fri, 22 Jul 2022 17:16:46 +0900 Subject: [PATCH 1/3] fileinput: Make inplace mode supports errors option --- Lib/fileinput.py | 9 ++++++--- Lib/test/test_fileinput.py | 11 +++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/Lib/fileinput.py b/Lib/fileinput.py index 9f41c18510decf..e234dc9ea65f15 100644 --- a/Lib/fileinput.py +++ b/Lib/fileinput.py @@ -335,18 +335,21 @@ def _readline(self): pass # The next few lines may raise OSError os.rename(self._filename, self._backupfilename) - self._file = open(self._backupfilename, self._mode, encoding=encoding) + self._file = open(self._backupfilename, self._mode, + encoding=encoding, errors=self._errors) try: perm = os.fstat(self._file.fileno()).st_mode except OSError: - self._output = open(self._filename, self._write_mode, encoding=encoding) + self._output = open(self._filename, self._write_mode, + encoding=encoding, errors=self._errors) else: mode = os.O_CREAT | os.O_WRONLY | os.O_TRUNC if hasattr(os, 'O_BINARY'): mode |= os.O_BINARY fd = os.open(self._filename, mode, perm) - self._output = os.fdopen(fd, self._write_mode, encoding=encoding) + self._output = os.fdopen(fd, self._write_mode, + encoding=encoding, errors=self._errors) try: os.chmod(self._filename, perm) except OSError: diff --git a/Lib/test/test_fileinput.py b/Lib/test/test_fileinput.py index 819200010a2230..311353e1a76b34 100644 --- a/Lib/test/test_fileinput.py +++ b/Lib/test/test_fileinput.py @@ -326,6 +326,17 @@ 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") + sys.stdout.write("New line \x88\n") + with open(temp_file, 'rb') as f: + self.assertEqual(f.read(), b'New line ?\n') + def test_file_hook_backward_compatibility(self): def old_hook(filename, mode): return io.StringIO("I used to receive only filename and mode") From 1f38dcf7cb9e2da80101830c6e2e1ed85efb8b3e Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Fri, 22 Jul 2022 17:20:02 +0900 Subject: [PATCH 2/3] add news --- .../next/Library/2022-07-22-17-19-57.gh-issue-93157.RXByAk.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2022-07-22-17-19-57.gh-issue-93157.RXByAk.rst diff --git a/Misc/NEWS.d/next/Library/2022-07-22-17-19-57.gh-issue-93157.RXByAk.rst b/Misc/NEWS.d/next/Library/2022-07-22-17-19-57.gh-issue-93157.RXByAk.rst new file mode 100644 index 00000000000000..054b318ec63f0c --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-07-22-17-19-57.gh-issue-93157.RXByAk.rst @@ -0,0 +1,2 @@ +Fix :mod:`fileinput` module didn't support ``errors`` option when +``inplace`` is true. From 73fd6407aa539d76dc16236fac2414ecac8fca1b Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Fri, 22 Jul 2022 17:45:03 +0900 Subject: [PATCH 3/3] fix test on Windows --- Lib/test/test_fileinput.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_fileinput.py b/Lib/test/test_fileinput.py index 311353e1a76b34..ac20c74baa09e2 100644 --- a/Lib/test/test_fileinput.py +++ b/Lib/test/test_fileinput.py @@ -332,10 +332,9 @@ def test_inplace_encoding_errors(self): encoding="ascii", errors="replace") as fobj: line = fobj.readline() self.assertEqual(line, 'Initial text \ufffd') - #print("New line \x88") - sys.stdout.write("New line \x88\n") + print("New line \x88") with open(temp_file, 'rb') as f: - self.assertEqual(f.read(), b'New line ?\n') + self.assertEqual(f.read().rstrip(b'\r\n'), b'New line ?') def test_file_hook_backward_compatibility(self): def old_hook(filename, mode):