Skip to content

Commit

Permalink
bpo-31281: Fix pathlib.Path incompatibility in fileinput (gh-3208)
Browse files Browse the repository at this point in the history
Fix fileinput with inplace=True to accept pathlib.Path objects.
  • Loading branch information
zmwangx authored and ericvsmith committed Sep 4, 2017
1 parent a234485 commit 06de1ae
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Lib/fileinput.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ def _readline(self):
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:
Expand Down
13 changes: 13 additions & 0 deletions Lib/test/test_fileinput.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,19 @@ def test_pathlib_file(self):
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"""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix ``fileinput.FileInput(files, inplace=True)`` when ``files`` contain
``pathlib.Path`` objects.

0 comments on commit 06de1ae

Please sign in to comment.