mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
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:
parent
e8edbda897
commit
22f06d6ce3
3 changed files with 18 additions and 3 deletions
|
@ -335,18 +335,21 @@ class FileInput:
|
||||||
pass
|
pass
|
||||||
# The next few lines may raise OSError
|
# The next few lines may raise OSError
|
||||||
os.rename(self._filename, self._backupfilename)
|
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:
|
try:
|
||||||
perm = os.fstat(self._file.fileno()).st_mode
|
perm = os.fstat(self._file.fileno()).st_mode
|
||||||
except OSError:
|
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:
|
else:
|
||||||
mode = os.O_CREAT | os.O_WRONLY | os.O_TRUNC
|
mode = os.O_CREAT | os.O_WRONLY | os.O_TRUNC
|
||||||
if hasattr(os, 'O_BINARY'):
|
if hasattr(os, 'O_BINARY'):
|
||||||
mode |= os.O_BINARY
|
mode |= os.O_BINARY
|
||||||
|
|
||||||
fd = os.open(self._filename, mode, perm)
|
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:
|
try:
|
||||||
os.chmod(self._filename, perm)
|
os.chmod(self._filename, perm)
|
||||||
except OSError:
|
except OSError:
|
||||||
|
|
|
@ -326,6 +326,16 @@ class FileInputTests(BaseTests, unittest.TestCase):
|
||||||
with open(temp_file, 'rb') as f:
|
with open(temp_file, 'rb') as f:
|
||||||
self.assertEqual(f.read(), b'New line.')
|
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 test_file_hook_backward_compatibility(self):
|
||||||
def old_hook(filename, mode):
|
def old_hook(filename, mode):
|
||||||
return io.StringIO("I used to receive only filename and mode")
|
return io.StringIO("I used to receive only filename and mode")
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Fix :mod:`fileinput` module didn't support ``errors`` option when
|
||||||
|
``inplace`` is true.
|
Loading…
Add table
Add a link
Reference in a new issue