Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 5 commits into from
Dec 23, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions Lib/compileall.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ISTM it would be clearer to put the input normalization (os.fspath()) up above with the normalization of fullname and name. I.e., add up above:

stripdir = os.fspath(stripdir) if stripdir is not None else None
Suggested change
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):
Expand Down
28 changes: 28 additions & 0 deletions Lib/test/test_compileall.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
@@ -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`.
Copy link
Contributor

@taleinat taleinat Oct 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This NEWS entry mentions ddir and compile_file(), but this PR still doesn't address those, unless I'm missing something.

Copy link
Contributor

@hauntsaninja hauntsaninja Oct 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think they already work, so we can just remove their mention from here.

The original version of this PR had some more os.fspath, but that was unnecessary and removed because #19883 (comment)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, their mention should be removed from the NEWS entry.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you mean ddir and prependdir, and yes, it seems like they already work due to the os.path.join usage.