[Python-checkins] bpo-31281: Fix pathlib.Path incompatibility in fileinput (gh-3208)

ericvsmith webhook-mailer at python.org
Mon Sep 4 13:37:26 EDT 2017


https://github.com/python/cpython/commit/06de1aeff94e524bed21d188065c4cd1590fb046
commit: 06de1aeff94e524bed21d188065c4cd1590fb046
branch: master
author: Zhiming Wang <zmwangx at gmail.com>
committer: ericvsmith <ericvsmith at users.noreply.github.com>
date: 2017-09-04T13:37:24-04:00
summary:

bpo-31281: Fix pathlib.Path incompatibility in fileinput (gh-3208)

Fix fileinput with inplace=True to accept pathlib.Path objects.

files:
A Misc/NEWS.d/next/Library/2017-08-29-07-14-14.bpo-31281.DcFyNs.rst
M Lib/fileinput.py
M Lib/test/test_fileinput.py

diff --git a/Lib/fileinput.py b/Lib/fileinput.py
index 363c241c50e..c6fc9a1981a 100644
--- a/Lib/fileinput.py
+++ b/Lib/fileinput.py
@@ -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:
diff --git a/Lib/test/test_fileinput.py b/Lib/test/test_fileinput.py
index 5df810c8f99..d7efc685d87 100644
--- a/Lib/test/test_fileinput.py
+++ b/Lib/test/test_fileinput.py
@@ -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"""
diff --git a/Misc/NEWS.d/next/Library/2017-08-29-07-14-14.bpo-31281.DcFyNs.rst b/Misc/NEWS.d/next/Library/2017-08-29-07-14-14.bpo-31281.DcFyNs.rst
new file mode 100644
index 00000000000..7fc8229cf47
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2017-08-29-07-14-14.bpo-31281.DcFyNs.rst
@@ -0,0 +1,2 @@
+Fix ``fileinput.FileInput(files, inplace=True)`` when ``files`` contain
+``pathlib.Path`` objects.



More information about the Python-checkins mailing list