bpo-40447: accept all path-like objects in compileall.compile_file

Signed-off-by: Filipe Laíns <lains@archlinux.org>
This commit is contained in:
Filipe Laíns 2020-05-03 13:53:39 +01:00 committed by Filipe Laíns
parent fc40b3020c
commit 3672420cc6
No known key found for this signature in database
GPG Key ID: F893C674816AA95D
3 changed files with 32 additions and 3 deletions

View File

@ -152,8 +152,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
@ -163,7 +162,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):

View File

@ -144,6 +144,20 @@ class CompileallTestsBase:
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))
@ -188,6 +202,20 @@ class CompileallTestsBase:
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))
@mock.patch('concurrent.futures.ProcessPoolExecutor')
def test_compile_pool_called(self, pool_mock):
compileall.compile_dir(self.directory, quiet=True, workers=5)

View File

@ -0,0 +1,2 @@
Accept pathlib.Path in the ddir, stripdir and prependdir arguments of
compileall.compile_file and compileall.compile_dir.