[Python-checkins] gh-93157: Fix fileinput didn't support `errors` in `inplace` mode (GH-95128)

miss-islington webhook-mailer at python.org
Sat Jul 23 23:02:54 EDT 2022


https://github.com/python/cpython/commit/4a682b4f1ae3c32d0abd874cda4d2419398f4469
commit: 4a682b4f1ae3c32d0abd874cda4d2419398f4469
branch: 3.10
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2022年07月23日T20:02:40-07:00
summary:
gh-93157: Fix fileinput didn't support `errors` in `inplace` mode (GH-95128)
(cherry picked from commit 5c7f3bcdafedd60a385e8ca5403bc6b0b7a862b3)
Co-authored-by: Inada Naoki <songofacandy at gmail.com>
files:
A Misc/NEWS.d/next/Library/2022-07-22-17-19-57.gh-issue-93157.RXByAk.rst
M Lib/fileinput.py
M Lib/test/test_fileinput.py
diff --git a/Lib/fileinput.py b/Lib/fileinput.py
index 35347185da048..2ce2f911435b4 100644
--- a/Lib/fileinput.py
+++ b/Lib/fileinput.py
@@ -355,18 +355,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 cae0eb1b5f657..21924ff41a72a 100644
--- a/Lib/test/test_fileinput.py
+++ b/Lib/test/test_fileinput.py
@@ -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")
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 0000000000000..054b318ec63f0
--- /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.


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /