[Python-checkins] bpo-40447: accept all path-like objects in compileall.compile_file (GH-19883)

miss-islington webhook-mailer at python.org
Fri Dec 23 15:43:19 EST 2022


https://github.com/python/cpython/commit/d5eb2f4747cd9afbc7f32fd6373a5f73aa59f61c
commit: d5eb2f4747cd9afbc7f32fd6373a5f73aa59f61c
branch: 3.11
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2022-12-23T12:43:13-08:00
summary:

bpo-40447: accept all path-like objects in compileall.compile_file (GH-19883)

(cherry picked from commit 1ecfd1ebf1f53ef6ac82085b25ed09952b470d4e)

Co-authored-by: Filipe Laíns <lains at riseup.net>
Signed-off-by: Filipe Laíns <lains at archlinux.org>
Signed-off-by: Filipe Laíns <lains at riseup.net>
Co-authored-by: Irit Katriel <1055913+iritkatriel at users.noreply.github.com>
Co-authored-by: Shantanu <12621235+hauntsaninja at users.noreply.github.com>

files:
A Misc/NEWS.d/next/Library/2020-05-03-12-55-55.bpo-40447.oKR0Lj.rst
M Lib/compileall.py
M Lib/test/test_compileall.py

diff --git a/Lib/compileall.py b/Lib/compileall.py
index 330a90786efc..a388931fb5a9 100644
--- a/Lib/compileall.py
+++ b/Lib/compileall.py
@@ -154,8 +154,8 @@ def compile_file(fullname, ddir=None, force=False, rx=None, quiet=0,
                           "in combination with stripdir or prependdir"))
 
     success = True
-    if quiet < 2 and isinstance(fullname, os.PathLike):
-        fullname = os.fspath(fullname)
+    fullname = os.fspath(fullname)
+    stripdir = os.fspath(stripdir) if stripdir is not None else None
     name = os.path.basename(fullname)
 
     dfile = None
diff --git a/Lib/test/test_compileall.py b/Lib/test/test_compileall.py
index 73c83c9bf1ef..05154c8f1c60 100644
--- a/Lib/test/test_compileall.py
+++ b/Lib/test/test_compileall.py
@@ -167,6 +167,20 @@ def test_compile_file_pathlike_ddir(self):
                                                 quiet=2))
         self.assertTrue(os.path.isfile(self.bc_path))
 
+    def test_compile_file_pathlike_stripdir(self):
+        self.assertFalse(os.path.isfile(self.bc_path))
+        self.assertTrue(compileall.compile_file(pathlib.Path(self.source_path),
+                                                stripdir=pathlib.Path('stripdir_path'),
+                                                quiet=2))
+        self.assertTrue(os.path.isfile(self.bc_path))
+
+    def test_compile_file_pathlike_prependdir(self):
+        self.assertFalse(os.path.isfile(self.bc_path))
+        self.assertTrue(compileall.compile_file(pathlib.Path(self.source_path),
+                                                prependdir=pathlib.Path('prependdir_path'),
+                                                quiet=2))
+        self.assertTrue(os.path.isfile(self.bc_path))
+
     def test_compile_path(self):
         with test.test_importlib.util.import_state(path=[self.directory]):
             self.assertTrue(compileall.compile_path(quiet=2))
@@ -219,6 +233,20 @@ def test_compile_dir_pathlike(self):
         self.assertRegex(line, r'Listing ([^WindowsPath|PosixPath].*)')
         self.assertTrue(os.path.isfile(self.bc_path))
 
+    def test_compile_dir_pathlike_stripdir(self):
+        self.assertFalse(os.path.isfile(self.bc_path))
+        self.assertTrue(compileall.compile_dir(pathlib.Path(self.directory),
+                                               stripdir=pathlib.Path('stripdir_path'),
+                                               quiet=2))
+        self.assertTrue(os.path.isfile(self.bc_path))
+
+    def test_compile_dir_pathlike_prependdir(self):
+        self.assertFalse(os.path.isfile(self.bc_path))
+        self.assertTrue(compileall.compile_dir(pathlib.Path(self.directory),
+                                               prependdir=pathlib.Path('prependdir_path'),
+                                               quiet=2))
+        self.assertTrue(os.path.isfile(self.bc_path))
+
     @skipUnless(_have_multiprocessing, "requires multiprocessing")
     @mock.patch('concurrent.futures.ProcessPoolExecutor')
     def test_compile_pool_called(self, pool_mock):
diff --git a/Misc/NEWS.d/next/Library/2020-05-03-12-55-55.bpo-40447.oKR0Lj.rst b/Misc/NEWS.d/next/Library/2020-05-03-12-55-55.bpo-40447.oKR0Lj.rst
new file mode 100644
index 000000000000..941038d095b3
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-05-03-12-55-55.bpo-40447.oKR0Lj.rst
@@ -0,0 +1,2 @@
+Accept :class:`os.PathLike` (such as :class:`pathlib.Path`) in the ``stripdir`` arguments of
+:meth:`compileall.compile_file` and :meth:`compileall.compile_dir`.



More information about the Python-checkins mailing list