]> granicus.if.org Git - python/commitdiff
bpo-31281: Fix pathlib.Path incompatibility in fileinput (gh-3208)
authorZhiming Wang <zmwangx@gmail.com>
Mon, 4 Sep 2017 17:37:24 +0000 (01:37 +0800)
committerericvsmith <ericvsmith@users.noreply.github.com>
Mon, 4 Sep 2017 17:37:24 +0000 (13:37 -0400)
Fix fileinput with inplace=True to accept pathlib.Path objects.

Lib/fileinput.py
Lib/test/test_fileinput.py
Misc/NEWS.d/next/Library/2017-08-29-07-14-14.bpo-31281.DcFyNs.rst [new file with mode: 0644]

index 363c241c50e988ea6740859b564c747ae0ddb08f..c6fc9a1981a1fa058b0c9f91f3f58f9cd9d5d6c5 100644 (file)
@@ -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:
index 5df810c8f999b831491ed0c14006e8027a347502..d7efc685d87e383f7b6f1c5edff7bd01f08144ad 100644 (file)
@@ -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"""
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 (file)
index 0000000..7fc8229
--- /dev/null
@@ -0,0 +1,2 @@
+Fix ``fileinput.FileInput(files, inplace=True)`` when ``files`` contain
+``pathlib.Path`` objects.