[issue31030] sys.executable can be not normalized

Eryk Sun report at bugs.python.org
Sat Feb 27 05:33:32 EST 2021


Eryk Sun <eryksun at gmail.com> added the comment:

In Python 3.10 in POSIX, it's still the case that executable, prefix, exec_prefix, base_prefix, and base_exec_prefix in the sys module do not get normalized. For example, in Linux:

    $ .local/bin/../bin/python3.10 -c "import sys; print(sys.executable)"
    /home/someone/.local/bin/../bin/python3.10

In test/test_sys.py, assertEqual(os.path.abspath(sys.executable), sys.executable) fails. In test/test_venv.py, assertIn('home = %s' % path, data) and assertEqual(out.strip(), expected.encode()) both fail, respectively in test_defaults and test_prefixes.

In Windows, prior to Python 3.6, this can be a problem if Python is run via CreateProcessW using a non-normalized path in lpApplicationName (rare, but possible) instead of letting the system search for the executable in lpCommandLine. For example:

    >>> cmd = 'python -c "import sys; print(sys.executable)"'
    >>> exe = r'C:\Program Files\Python35\..\Python35\python.exe'
    >>> subprocess.call(cmd, executable=exe)
    C:\Program Files\Python35\..\Python35\python.exe
    0

It's no longer an issue in Windows with Python 3.6 and above. When getting the program path while initializing, the GetModuleFileNameW(NULL, ..) result gets canonicalized (e.g. via PathCchCanonicalizeEx). For example:

    >>> exe = r'C:\Program Files\Python36\..\Python36\python.exe'
    >>> subprocess.call(cmd, executable=exe)
    C:\Program Files\Python36\python.exe
    0

----------
versions: +Python 3.10, Python 3.8, Python 3.9 -Python 2.7, Python 3.6, Python 3.7

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue31030>
_______________________________________


More information about the Python-bugs-list mailing list