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-18283: Add support for bytes to shutil.which #11818

Merged
merged 4 commits into from
Feb 13, 2019

Conversation

csabella
Copy link
Contributor

@csabella csabella commented Feb 11, 2019

Original patch by Victor Stinner.

https://bugs.python.org/issue18283

# Check that a given file can be accessed with the correct mode.
# Additionally check that `file` is not a directory, as on Windows
# directories pass the os.access check.
def _access_check(fn, mode):
Copy link
Member

Choose a reason for hiding this comment

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

Just curious any reason over moving this outside which?

Copy link
Member

Choose a reason for hiding this comment

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

As an author of the original patch, I can explain: it's inefficient to define a temporary function for each which() call. Moreover, there is no technical reason to use a nested function.

Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

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

LGTM... well, I wrote the initial patch, so I'm not fair :-)

Just a few suggestions.

Lib/shutil.py Outdated Show resolved Hide resolved
Lib/shutil.py Outdated

# PATHEXT is necessary to check on Windows.
pathext = os.environ.get("PATHEXT", "").split(os.pathsep)
if isinstance(cmd, bytes):
pathext = list(map(os.fsencode, pathext))
Copy link
Member

Choose a reason for hiding this comment

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

Maybe use a list-comprehension instead? I'm not sure what is the best ;-)

Copy link
Contributor

Choose a reason for hiding this comment

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

I think that list(map(os.fsencode, pathext)) it's ok.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did a timeit on both and the list(map()) is slightly faster (on my system), but not much. If it code after this didn't (potentially) iterate over pathext twice, a generator would be ideal.

Also, the following if clause uses:

    if any(cmd.lower().endswith(ext.lower()) for ext in pathext):
        files = [cmd]

Removing the cmd.lower() from the any would be a processing optimization, but it's unrelated to this change, so I didn't put it in.

    cmd_lower = cmd.lower()
    if any(cmd_lower.endswith(ext.lower()) for ext in pathext):
        files = [cmd]

Lib/test/test_shutil.py Outdated Show resolved Hide resolved
Copy link
Contributor

@eamanu eamanu left a comment

Choose a reason for hiding this comment

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

LGTM. it's work correctly

Lib/shutil.py Outdated

# PATHEXT is necessary to check on Windows.
pathext = os.environ.get("PATHEXT", "").split(os.pathsep)
if isinstance(cmd, bytes):
pathext = list(map(os.fsencode, pathext))
Copy link
Contributor

Choose a reason for hiding this comment

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

I think that list(map(os.fsencode, pathext)) it's ok.

Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

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

One last remark :-)

Lib/test/test_shutil.py Outdated Show resolved Hide resolved
@vstinner
Copy link
Member

@giampaolo: I wrote the initial patch, so I would prefer that someone else would review it. Would mind to review this change please?

@giampaolo
Copy link
Contributor

@vstinner no problem; patch LGTM.

@vstinner vstinner merged commit 5680f65 into python:master Feb 13, 2019
@vstinner
Copy link
Member

Thanks @giampaolo for the review ;-)

@vstinner
Copy link
Member

Thanks @csabella, I merged your PR ;-)

@csabella csabella deleted the bpo18283 branch March 1, 2019 18:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants