Skip to content

PurePath.match() incorrectly intercepts and breaks absolute shebangs #348

@mcexit

Description

@mcexit

Install source and version

  • Installed with the MSIX from python.org

Version: 26.2

Describe the bug
In scriptutils.py, _find_shebang_command uses pathlib.PurePath.match(), which performs right-aligned suffix matching. This inadvertently intercepts explicit absolute paths and feeds them into bare-name routing rules.

When installing certain packages via uv (specifically older packages using setup.py that drop .py scripts with absolute shebangs rather than .exe entry-point shims), or when a script utilizes its own local python.exe (such as in a venv), the absolute shebang is falsely intercepted. This prevents the path from reaching _find_on_path(). Instead:

  1. Paths ending in python.exe trigger the is_default check, hijacking execution to the global environment.
  2. Paths ending in python*.exe (e.g., python_test.exe) bypass the default check and fall through to the wildcard fallback, which slices the string ([6:-4]) and crashes while looking for a non-existent tag.

To Reproduce
Steps to reproduce the behavior:

  1. Create or install a script utilizing an isolated local environment (e.g., an older setup.py package installed via uv that relies on a shebang, or a standard venv).
  2. Set the shebang to the absolute path of that isolated executable (e.g., #!C:\path\to\env\Scripts\python.exe).

NOTE: This is uv's default behavior for older packages.

  1. Execute the script via pymanager (py.exe example_script.py) or directly if py files are already associated with it.
  2. Observe that the script ignores the absolute path shebang and falls back to the global default Python environment.
  3. (Optional) Rename the executable to python_test.exe, update the shebang, and observe a crash during tag extraction.

Expected behavior
_find_shebang_command should only match bare alias names. Explicit paths containing directory separators should safely raise LookupError so _parse_shebang can natively delegate them to _find_on_path().

Additional context
As requested by @zooba in #345 this issue tracks the routing behavior observed with absolute shebangs prior to merging the fix. This alias-matching logic also contributes to the behaviors reported in #91 and #307.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions