bpo-31281: Fix pathlib.Path incompatibility in fileinput (gh-3208)
Fix fileinput with inplace=True to accept pathlib.Path objects.
This commit is contained in:
parent
a2344851ab
commit
06de1aeff9
|
@ -330,7 +330,7 @@ class FileInput:
|
|||
else:
|
||||
if self._inplace:
|
||||
self._backupfilename = (
|
||||
self._filename + (self._backup or ".bak"))
|
||||
os.fspath(self._filename) + (self._backup or ".bak"))
|
||||
try:
|
||||
os.unlink(self._backupfilename)
|
||||
except OSError:
|
||||
|
|
|
@ -544,6 +544,19 @@ class FileInputTests(unittest.TestCase):
|
|||
finally:
|
||||
remove_tempfiles(t1)
|
||||
|
||||
def test_pathlib_file_inplace(self):
|
||||
t1 = None
|
||||
try:
|
||||
t1 = Path(writeTmp(1, ['Pathlib file.']))
|
||||
with FileInput(t1, inplace=True) as fi:
|
||||
line = fi.readline()
|
||||
self.assertEqual(line, 'Pathlib file.')
|
||||
print('Modified %s' % line)
|
||||
with open(t1) as f:
|
||||
self.assertEqual(f.read(), 'Modified Pathlib file.\n')
|
||||
finally:
|
||||
remove_tempfiles(t1)
|
||||
|
||||
|
||||
class MockFileInput:
|
||||
"""A class that mocks out fileinput.FileInput for use during unit tests"""
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
Fix ``fileinput.FileInput(files, inplace=True)`` when ``files`` contain
|
||||
``pathlib.Path`` objects.
|
Loading…
Reference in New Issue