diff options
author | Steve Dower <steve.dower@python.org> | 2023-01-16 17:00:34 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-01-16 17:00:34 (GMT) |
commit | 87ade7ebda2e04aa9fb1930894fd3cadb88eee0b (patch) | |
tree | 4a819479460d0aad1edd9bffc358f8f59fae6e39 /Modules/getpath.py | |
parent | 63690e9af88602dbc5a4dd6bc1571f935b69821f (diff) | |
download | cpython-87ade7ebda2e04aa9fb1930894fd3cadb88eee0b.zip cpython-87ade7ebda2e04aa9fb1930894fd3cadb88eee0b.tar.gz cpython-87ade7ebda2e04aa9fb1930894fd3cadb88eee0b.tar.bz2 |
gh-100320: Fix path calculations on Windows when python.exe is moved outside of the normal location (GH-100947)
Diffstat (limited to 'Modules/getpath.py')
-rw-r--r-- | Modules/getpath.py | 48 |
1 files changed, 30 insertions, 18 deletions
diff --git a/Modules/getpath.py b/Modules/getpath.py index fc533a8..e847141 100644 --- a/Modules/getpath.py +++ b/Modules/getpath.py @@ -597,25 +597,27 @@ else: # Detect exec_prefix by searching from executable for the platstdlib_dir if PLATSTDLIB_LANDMARK and not exec_prefix: - if executable_dir: - if os_name == 'nt': - # QUIRK: For compatibility and security, do not search for DLLs - # directory. The fallback below will cover it - exec_prefix = executable_dir - else: - exec_prefix = search_up(executable_dir, PLATSTDLIB_LANDMARK, test=isdir) + if os_name == 'nt': + # QUIRK: Windows always assumed these were the same + # gh-100320: Our PYDs are assumed to be relative to the Lib directory + # (that is, prefix) rather than the executable (that is, executable_dir) + exec_prefix = prefix + if not exec_prefix and executable_dir: + exec_prefix = search_up(executable_dir, PLATSTDLIB_LANDMARK, test=isdir) if not exec_prefix and EXEC_PREFIX: exec_prefix = EXEC_PREFIX if not exec_prefix or not isdir(joinpath(exec_prefix, PLATSTDLIB_LANDMARK)): if os_name == 'nt': # QUIRK: If DLLs is missing on Windows, don't warn, just assume - # that it's all the same as prefix. - # gh-98790: We set platstdlib_dir here to avoid adding "DLLs" into - # sys.path when it doesn't exist, which would give site-packages - # precedence over executable_dir, which is *probably* where our PYDs - # live. Ideally, whoever changes our layout will tell us what the - # layout is, but in the past this worked, so it should keep working. - platstdlib_dir = exec_prefix = prefix + # that they're in exec_prefix + if not platstdlib_dir: + # gh-98790: We set platstdlib_dir here to avoid adding "DLLs" into + # sys.path when it doesn't exist in the platstdlib place, which + # would give Lib packages precedence over executable_dir where our + # PYDs *probably* live. Ideally, whoever changes our layout will tell + # us what the layout is, but in the past this worked, so it should + # keep working. + platstdlib_dir = exec_prefix else: warn('Could not find platform dependent libraries <exec_prefix>') @@ -701,8 +703,14 @@ elif not pythonpath_was_set: except OSError: break if isinstance(v, str): - pythonpath.append(v) + pythonpath.extend(v.split(DELIM)) i += 1 + # Paths from the core key get appended last, but only + # when home was not set and we aren't in a build dir + if not home_was_set and not venv_prefix and not build_prefix: + v = winreg.QueryValue(key, None) + if isinstance(v, str): + pythonpath.extend(v.split(DELIM)) finally: winreg.CloseKey(key) except OSError: @@ -714,13 +722,17 @@ elif not pythonpath_was_set: pythonpath.append(joinpath(prefix, p)) # Then add stdlib_dir and platstdlib_dir - if os_name == 'nt' and venv_prefix: - # QUIRK: Windows generates paths differently in a venv + if os_name == 'nt': + # QUIRK: Windows generates paths differently if platstdlib_dir: pythonpath.append(platstdlib_dir) if stdlib_dir: pythonpath.append(stdlib_dir) - if executable_dir not in pythonpath: + if executable_dir and executable_dir not in pythonpath: + # QUIRK: the executable directory is on sys.path + # We keep it low priority, so that properly installed modules are + # found first. It may be earlier in the order if we found some + # reason to put it there. pythonpath.append(executable_dir) else: if stdlib_dir: |