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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

FFY00
Copy link
Member

@FFY00 FFY00 commented May 3, 2020

@the-knights-who-say-ni
Copy link

the-knights-who-say-ni commented May 3, 2020

Hello, and thanks for your contribution!

I'm a bot set up to make sure that the project can legally accept this contribution by verifying everyone involved has signed the PSF contributor agreement (CLA).

CLA Missing

Our records indicate the following people have not signed the CLA:

@FFY00

For legal reasons we need all the people listed to sign the CLA before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue.

If you have recently signed the CLA, please wait at least one business day
before our records are updated.

You can check yourself to see if the CLA has been received.

Thanks again for the contribution, we look forward to reviewing it!

@FFY00 FFY00 changed the title bpo-40447: accept pathlib.path in compileall.compile_file bpo-40447: accept pathlib.Path in compileall.compile_file May 3, 2020
@FFY00 FFY00 force-pushed the bpo-40447 branch 2 times, most recently from 99f62ad to 453d882 Compare May 3, 2020
Copy link
Sponsor Member

@isidentical isidentical left a comment

Is there a reason to not to use os.fspath for supporting all path like objects?

@FFY00
Copy link
Member Author

FFY00 commented May 3, 2020

Nop, I just totally forgot about that. Good catch :D

@FFY00 FFY00 force-pushed the bpo-40447 branch 2 times, most recently from 924a7fd to 7b48ce8 Compare May 3, 2020
@FFY00 FFY00 changed the title bpo-40447: accept pathlib.Path in compileall.compile_file bpo-40447: accept all path-like objects in compileall.compile_file May 3, 2020
@FFY00 FFY00 force-pushed the bpo-40447 branch 2 times, most recently from 4a1312b to 6cafeac Compare May 3, 2020
Copy link
Contributor

@hauntsaninja hauntsaninja left a comment

Thanks! Probably worth adding to the tests too :-)

@@ -157,8 +157,8 @@ def compile_file(fullname, ddir=None, force=False, rx=None, quiet=0,
dfile = os.path.join(ddir, name)

if stripdir is not None:
fullname_parts = fullname.split(os.path.sep)
stripdir_parts = stripdir.split(os.path.sep)
fullname_parts = os.fspath(fullname).split(os.path.sep)
Copy link
Contributor

@hauntsaninja hauntsaninja May 3, 2020

Choose a reason for hiding this comment

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

This has redundancy with the logic on line 151. Not sure why that's gated by the condition on quiet, though.

Copy link
Member Author

@FFY00 FFY00 May 3, 2020

Choose a reason for hiding this comment

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

Oh, yes. It is odd.

@berkerpeksag sorry for bothering, do you remember why the quiet check is there? It looks like quiet is being used across the code to provide some set some kind of failing level here. The docstring is also a bit ambiguous.

full output with False or 0, errors only with 1, no output with 2

I am not sure how to parse the behavior for 1.

Should this behavior be changed? If not, the docstring should definitely be touched up.

Copy link
Member Author

@FFY00 FFY00 May 3, 2020

Choose a reason for hiding this comment

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

Anyway, I am moving the path-like object translation up there.

Copy link
Member Author

@FFY00 FFY00 May 3, 2020

Choose a reason for hiding this comment

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

I also added the other path arguments.

Copy link
Contributor

@taleinat taleinat left a comment

Thanks for this PR!

This seems like a good fix, but needs a bit more work:

  1. ISTM that this should also fix the handling of the same parameters in compile_dir.
  2. Tests?
  3. Clarify the NEWS entry (see inline comment.)

@bedevere-bot
Copy link

bedevere-bot commented Oct 18, 2020

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@FFY00 FFY00 force-pushed the bpo-40447 branch 2 times, most recently from d63b4e5 to 31251fb Compare Oct 19, 2020
@FFY00
Copy link
Member Author

FFY00 commented Oct 19, 2020

requested changes; please review again

@taleinat
Copy link
Contributor

taleinat commented Oct 20, 2020

Thanks @FFY00, that was quick!

The NEWS entry mentions fixing compile_dir as well as compile_file, but I don't see that here yet in the code or tests?

@@ -152,8 +152,14 @@ 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):
if isinstance(fullname, os.PathLike):
Copy link
Contributor

@hauntsaninja hauntsaninja Oct 20, 2020

Choose a reason for hiding this comment

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

nit: os.fspath is happy to take str and bytes, so you don't need this check for fullname (for the other three, you do need something to guard against None)

Copy link
Contributor

@taleinat taleinat Oct 20, 2020

Choose a reason for hiding this comment

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

You're right! The last threeos.fspath calls can simply be moved below into each of the blocks handling the three directory arguments, without the isinstance checks. The first one shouldn't need the isinstance guard at all; letting os.fspath error on invalid types seems like precisely what we want here.

Copy link
Member Author

@FFY00 FFY00 Nov 20, 2020

Choose a reason for hiding this comment

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

Sorry for the delay! This fell off my working pool. Made the changes 😊

@FFY00 FFY00 force-pushed the bpo-40447 branch 2 times, most recently from a6a1788 to 599bfd9 Compare Nov 20, 2020
name = os.path.basename(fullname)

dfile = None

if ddir is not None:
dfile = os.path.join(ddir, name)
dfile = os.path.join(os.fspath(ddir), name)
Copy link
Contributor

@hauntsaninja hauntsaninja Nov 20, 2020

Choose a reason for hiding this comment

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

One more nit: I think the call to os.fspath is only needed on line 165 with stripdir; os.path.join is happy to take PathLike objects

Copy link
Contributor

@hauntsaninja hauntsaninja Nov 20, 2020

Choose a reason for hiding this comment

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

Although actually neither split works when stripdir is bytes, which the documentation says it's allowed to be, since os.path.sep is a str. Same for fullname although the documentation doesn't mention that bytes is allowed there.

Copy link
Member Author

@FFY00 FFY00 Nov 29, 2020

Choose a reason for hiding this comment

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

I made the changes, let me know if there's anything else.

@FFY00
Copy link
Member Author

FFY00 commented Oct 19, 2021

This got stale, I now rebased it on main and solved the conflicts. Is there anything else needed?

Signed-off-by: Filipe Laíns <lains@archlinux.org>
@FFY00
Copy link
Member Author

FFY00 commented Oct 25, 2021

@taleinat or @jaraco, would you be able to have a look if you have time?

@taleinat
Copy link
Contributor

taleinat commented Oct 26, 2021

@FFY00, I'll try to take a look today.

@@ -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

@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.

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)

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

@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.

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

Copy link
Member Author

@FFY00 FFY00 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 you mean ddir and prependdir, and yes, it seems like they already work due to the os.path.join usage.

@taleinat
Copy link
Contributor

taleinat commented Oct 26, 2021

@FFY00, this still needs some more attention. If the changes are made quickly then I'll be able to review them promptly.

Signed-off-by: Filipe Laíns <lains@riseup.net>
@FFY00
Copy link
Member Author

FFY00 commented Oct 26, 2021

@taleinat pushed the requested changes.

jaraco
jaraco approved these changes Nov 27, 2021
Copy link
Member

@jaraco jaraco left a comment

LGTM, though I haven't reviewed in detail. Let me know if there's something in particular you'd like me to critique.

Copy link
Contributor

@hauntsaninja hauntsaninja left a comment

Thanks, this looks correct to me.

There's still one issue that I pointed out in #19883 (comment) , but that is separate enough that we can deal with it later.

I'm not sure whether to backport. While the code itself is "new feature" it is a feature that the docs in previous versions promise exists. We should either a) backport this change, or b) amend the docs on old branches. I lean towards a) for simplicity.

cc @JelleZijlstra

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants