From 45af2149cddeb407b6a013d59bb9f7c24e523d0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filipe=20La=C3=ADns?= Date: Sun, 3 May 2020 13:53:39 +0100 Subject: [PATCH 1/3] bpo-40447: accept all path-like objects in compileall.compile_file MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Filipe Laíns --- Lib/compileall.py | 5 ++-- Lib/test/test_compileall.py | 28 +++++++++++++++++++ .../2020-05-03-12-55-55.bpo-40447.oKR0Lj.rst | 2 ++ 3 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2020-05-03-12-55-55.bpo-40447.oKR0Lj.rst diff --git a/Lib/compileall.py b/Lib/compileall.py index 3755e76ba813f5..28e3cd2fbd2564 100644 --- a/Lib/compileall.py +++ b/Lib/compileall.py @@ -154,8 +154,7 @@ 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) name = os.path.basename(fullname) dfile = None @@ -165,7 +164,7 @@ def compile_file(fullname, ddir=None, force=False, rx=None, quiet=0, if stripdir is not None: fullname_parts = fullname.split(os.path.sep) - stripdir_parts = stripdir.split(os.path.sep) + stripdir_parts = os.fspath(stripdir).split(os.path.sep) ddir_parts = list(fullname_parts) for spart, opart in zip(stripdir_parts, fullname_parts): diff --git a/Lib/test/test_compileall.py b/Lib/test/test_compileall.py index 9e15ecf3aae299..7fe2cfe003d27e 100644 --- a/Lib/test/test_compileall.py +++ b/Lib/test/test_compileall.py @@ -169,6 +169,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)) @@ -221,6 +235,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 00000000000000..01d0f1cc6682fb --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-05-03-12-55-55.bpo-40447.oKR0Lj.rst @@ -0,0 +1,2 @@ +Accept :class:`pathlib.Path` in the ``ddir``, ``stripdir`` and ``prependdir`` +arguments of :meth:`compileall.compile_file` and :meth:`compileall.compile_dir`. From 63d02ea43727f70fc4534088e1aaf8c1071d773c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filipe=20La=C3=ADns?= Date: Wed, 27 Oct 2021 00:51:44 +0100 Subject: [PATCH 2/3] requested changes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Filipe Laíns --- Lib/compileall.py | 3 ++- .../next/Library/2020-05-03-12-55-55.bpo-40447.oKR0Lj.rst | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Lib/compileall.py b/Lib/compileall.py index 28e3cd2fbd2564..50183ea85468aa 100644 --- a/Lib/compileall.py +++ b/Lib/compileall.py @@ -155,6 +155,7 @@ def compile_file(fullname, ddir=None, force=False, rx=None, quiet=0, success = True fullname = os.fspath(fullname) + stripdir = os.fspath(stripdir) if stripdir is not None else None name = os.path.basename(fullname) dfile = None @@ -164,7 +165,7 @@ def compile_file(fullname, ddir=None, force=False, rx=None, quiet=0, if stripdir is not None: fullname_parts = fullname.split(os.path.sep) - stripdir_parts = os.fspath(stripdir).split(os.path.sep) + stripdir_parts = stripdir.split(os.path.sep) ddir_parts = list(fullname_parts) for spart, opart in zip(stripdir_parts, fullname_parts): 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 index 01d0f1cc6682fb..1aa09495f339ca 100644 --- 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 @@ -1,2 +1,2 @@ -Accept :class:`pathlib.Path` in the ``ddir``, ``stripdir`` and ``prependdir`` -arguments of :meth:`compileall.compile_file` and :meth:`compileall.compile_dir`. +Accept :class:`pathlib.Path` in the ``stripdir`` arguments of +:meth:`compileall.compile_file` and :meth:`compileall.compile_dir`. From 5d56bbab607bb529f9e588988245ff81c4dfcbbb Mon Sep 17 00:00:00 2001 From: Shantanu <12621235+hauntsaninja@users.noreply.github.com> Date: Thu, 22 Dec 2022 23:13:25 -0600 Subject: [PATCH 3/3] Update Misc/NEWS.d/next/Library/2020-05-03-12-55-55.bpo-40447.oKR0Lj.rst --- .../next/Library/2020-05-03-12-55-55.bpo-40447.oKR0Lj.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 1aa09495f339ca..941038d095b305 100644 --- 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 @@ -1,2 +1,2 @@ -Accept :class:`pathlib.Path` in the ``stripdir`` arguments of +Accept :class:`os.PathLike` (such as :class:`pathlib.Path`) in the ``stripdir`` arguments of :meth:`compileall.compile_file` and :meth:`compileall.compile_dir`.