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

distutils: find_executable() fails if the PATH environment variable is not set #78711

Closed
vstinner opened this issue Aug 28, 2018 · 10 comments
Closed
Labels
3.7 (EOL) end of life 3.8 only security fixes stdlib Python modules in the Lib dir

Comments

@vstinner
Copy link
Member

BPO 34530
Nosy @pfmoore, @ncoghlan, @vstinner, @serhiy-storchaka, @dstufft, @miss-islington
PRs
  • bpo-34530: Fix distutils find_executable() #8968
  • bpo-34530: Fix distutils find_executable() #9049
  • [3.7] bpo-34530: Fix distutils find_executable() (GH-9049) #9056
  • [3.6] bpo-34530: Fix distutils find_executable() (GH-9049) #9057
  • [2.7] bpo-34530: Fix distutils find_executable() (GH-9049) #9058
  • [3.6] bpo-34530: Fix distutils find_executable() (GH-9049) #9071
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2018-09-05.14:48:46.462>
    created_at = <Date 2018-08-28.12:47:31.571>
    labels = ['3.8', '3.7', 'library']
    title = 'distutils: find_executable() fails if the PATH environment variable is not set'
    updated_at = <Date 2018-09-05.14:48:46.442>
    user = 'https://github.com/vstinner'

    bugs.python.org fields:

    activity = <Date 2018-09-05.14:48:46.442>
    actor = 'vstinner'
    assignee = 'none'
    closed = True
    closed_date = <Date 2018-09-05.14:48:46.462>
    closer = 'vstinner'
    components = ['Library (Lib)']
    creation = <Date 2018-08-28.12:47:31.571>
    creator = 'vstinner'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 34530
    keywords = ['patch']
    message_count = 10.0
    messages = ['324245', '324246', '324380', '324381', '324539', '324566', '324575', '324578', '324636', '324640']
    nosy_count = 7.0
    nosy_names = ['paul.moore', 'ncoghlan', 'vstinner', 'serhiy.storchaka', 'dstufft', 'Marcus.Smith', 'miss-islington']
    pr_nums = ['8968', '9049', '9056', '9057', '9058', '9071']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue34530'
    versions = ['Python 2.7', 'Python 3.6', 'Python 3.7', 'Python 3.8']

    @vstinner
    Copy link
    Member Author

    Example:

    $ env -i ./python -c 'import distutils.spawn; print(distutils.spawn.find_executable("true"))'
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/home/vstinner/prog/python/master/Lib/distutils/spawn.py", line 176, in find_executable
        path = os.environ['PATH']
      File "/home/vstinner/prog/python/master/Lib/os.py", line 672, in __getitem__
        raise KeyError(key) from None
    KeyError: 'PATH'

    Attached PR fixes the issue for the master branch.

    @vstinner vstinner added 3.7 (EOL) end of life 3.8 only security fixes stdlib Python modules in the Lib dir labels Aug 28, 2018
    @vstinner
    Copy link
    Member Author

    I'm not sure that it's worth it to modify Python 3.7 and older. distutils changes are now well welcomed (breaking setuptools or pip is unpopular :-)).

    @vstinner vstinner removed the 3.7 (EOL) end of life label Aug 28, 2018
    @serhiy-storchaka
    Copy link
    Member

    find_executable() always searches executables from the current directory. This effectively equivalent having os.curdir at the start of path. shutil.which() does this only on Windows. This change LGTM, but it potentially can break user code, thus it may be not safe to make it in maintained versions.

    @vstinner
    Copy link
    Member Author

    find_executable() always searches executables from the current directory.

    Oh! I didn't notice that!

    @vstinner
    Copy link
    Member Author

    vstinner commented Sep 3, 2018

    I missed the fact that find_executable() always lookup in the current directory, whereas shutil.which() doesn't. I abandoned my PR 8968 in favor of PR 9049 which only fix the case where PATH env var is not defined. This PR can easily be backported to all branches, whereas backporting PR 8968 to stable branches would be risky: changes to distutils are likely to break a random package on PyPI.

    @vstinner vstinner added the 3.7 (EOL) end of life label Sep 3, 2018
    @vstinner
    Copy link
    Member Author

    vstinner commented Sep 4, 2018

    New changeset 3948719 by Victor Stinner in branch 'master':
    bpo-34530: Fix distutils find_executable() (GH-9049)
    3948719

    @miss-islington
    Copy link
    Contributor

    New changeset 7aa3ead by Miss Islington (bot) in branch '3.7':
    bpo-34530: Fix distutils find_executable() (GH-9049)
    7aa3ead

    @vstinner
    Copy link
    Member Author

    vstinner commented Sep 4, 2018

    New changeset 7056ca8 by Victor Stinner in branch '2.7':
    bpo-34530: Fix distutils find_executable() (GH-9049) (GH-9058)
    7056ca8

    @vstinner
    Copy link
    Member Author

    vstinner commented Sep 5, 2018

    New changeset e2c1657 by Victor Stinner (Miss Islington (bot)) in branch '3.6':
    bpo-34530: Fix distutils find_executable() (GH-9049) (GH-9057)
    e2c1657

    @vstinner
    Copy link
    Member Author

    vstinner commented Sep 5, 2018

    I chose to merge the simplest change:

    •    path = os.environ['PATH']
      

    + path = os.environ.get('PATH', os.defpath)

    And I added unit tests for find_executable(). The bug is now fixed.

    I'm not longer interested to reuse shutil.which() in distutils.find_executable(), since find_executable() first checks if the executable is in the current directory.

    @vstinner vstinner closed this as completed Sep 5, 2018
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life 3.8 only security fixes stdlib Python modules in the Lib dir
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants