Skip to content

Commit 87ade7e

Browse files
authoredJan 16, 2023
gh-100320: Fix path calculations on Windows when python.exe is moved outside of the normal location (GH-100947)
1 parent 63690e9 commit 87ade7e

File tree

4 files changed

+54
-33
lines changed

4 files changed

+54
-33
lines changed
 

‎Lib/test/test_embed.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -709,8 +709,10 @@ def check_config(self, configs, expected):
709709
if MS_WINDOWS:
710710
value = config.get(key := 'program_name')
711711
if value and isinstance(value, str):
712-
ext = '_d.exe' if debug_build(sys.executable) else '.exe'
713-
config[key] = value[:len(value.lower().removesuffix(ext))]
712+
value = value[:len(value.lower().removesuffix('.exe'))]
713+
if debug_build(sys.executable):
714+
value = value[:len(value.lower().removesuffix('_d'))]
715+
config[key] = value
714716
for key, value in list(expected.items()):
715717
if value is self.IGNORE_CONFIG:
716718
config.pop(key, None)
@@ -1283,7 +1285,7 @@ def test_init_setpythonhome(self):
12831285
stdlib = os.path.join(home, "Lib")
12841286
# Because we are specifying 'home', module search paths
12851287
# are fairly static
1286-
expected_paths = [paths[0], stdlib, os.path.join(home, 'DLLs')]
1288+
expected_paths = [paths[0], os.path.join(home, 'DLLs'), stdlib]
12871289
else:
12881290
version = f'{sys.version_info.major}.{sys.version_info.minor}'
12891291
stdlib = os.path.join(home, sys.platlibdir, f'python{version}')
@@ -1324,7 +1326,7 @@ def test_init_is_python_build_with_home(self):
13241326
stdlib = os.path.join(home, "Lib")
13251327
# Because we are specifying 'home', module search paths
13261328
# are fairly static
1327-
expected_paths = [paths[0], stdlib, os.path.join(home, 'DLLs')]
1329+
expected_paths = [paths[0], os.path.join(home, 'DLLs'), stdlib]
13281330
else:
13291331
version = f'{sys.version_info.major}.{sys.version_info.minor}'
13301332
stdlib = os.path.join(home, sys.platlibdir, f'python{version}')
@@ -1352,7 +1354,7 @@ def test_init_is_python_build_with_home(self):
13521354
config['_is_python_build'] = 1
13531355
exedir = os.path.dirname(sys.executable)
13541356
with open(os.path.join(exedir, 'pybuilddir.txt'), encoding='utf8') as f:
1355-
expected_paths[2] = os.path.normpath(
1357+
expected_paths[1 if MS_WINDOWS else 2] = os.path.normpath(
13561358
os.path.join(exedir, f'{f.read()}\n$'.splitlines()[0]))
13571359
if not MS_WINDOWS:
13581360
# PREFIX (default) is set when running in build directory
@@ -1429,8 +1431,8 @@ def test_init_pybuilddir_win32(self):
14291431

14301432
module_search_paths = self.module_search_paths()
14311433
module_search_paths[-3] = os.path.join(tmpdir, os.path.basename(module_search_paths[-3]))
1432-
module_search_paths[-2] = stdlibdir
1433-
module_search_paths[-1] = tmpdir
1434+
module_search_paths[-2] = tmpdir
1435+
module_search_paths[-1] = stdlibdir
14341436

14351437
executable = self.test_exe
14361438
config = {

‎Lib/test/test_getpath.py

+12-8
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ def test_normal_win32(self):
3838
module_search_paths_set=1,
3939
module_search_paths=[
4040
r"C:\Python\python98.zip",
41-
r"C:\Python\Lib",
4241
r"C:\Python\DLLs",
42+
r"C:\Python\Lib",
43+
r"C:\Python",
4344
],
4445
)
4546
actual = getpath(ns, expected)
@@ -64,8 +65,8 @@ def test_buildtree_win32(self):
6465
module_search_paths_set=1,
6566
module_search_paths=[
6667
r"C:\CPython\PCbuild\amd64\python98.zip",
67-
r"C:\CPython\Lib",
6868
r"C:\CPython\PCbuild\amd64",
69+
r"C:\CPython\Lib",
6970
],
7071
)
7172
actual = getpath(ns, expected)
@@ -134,8 +135,9 @@ def test_registry_win32(self):
134135
r"C:\Python\python98.zip",
135136
"path1-dir",
136137
# should not contain not-subdirs
137-
r"C:\Python\Lib",
138138
r"C:\Python\DLLs",
139+
r"C:\Python\Lib",
140+
r"C:\Python",
139141
],
140142
)
141143
actual = getpath(ns, expected)
@@ -148,8 +150,9 @@ def test_registry_win32(self):
148150
module_search_paths_set=1,
149151
module_search_paths=[
150152
r"C:\Python\python98.zip",
151-
r"C:\Python\Lib",
152153
r"C:\Python\DLLs",
154+
r"C:\Python\Lib",
155+
r"C:\Python",
153156
],
154157
)
155158
actual = getpath(ns, expected)
@@ -174,8 +177,9 @@ def test_symlink_normal_win32(self):
174177
module_search_paths_set=1,
175178
module_search_paths=[
176179
r"C:\Python\python98.zip",
177-
r"C:\Python\Lib",
178180
r"C:\Python\DLLs",
181+
r"C:\Python\Lib",
182+
r"C:\Python",
179183
],
180184
)
181185
actual = getpath(ns, expected)
@@ -202,8 +206,8 @@ def test_symlink_buildtree_win32(self):
202206
module_search_paths_set=1,
203207
module_search_paths=[
204208
r"C:\CPython\PCbuild\amd64\python98.zip",
205-
r"C:\CPython\Lib",
206209
r"C:\CPython\PCbuild\amd64",
210+
r"C:\CPython\Lib",
207211
],
208212
)
209213
actual = getpath(ns, expected)
@@ -232,8 +236,8 @@ def test_buildtree_pythonhome_win32(self):
232236
module_search_paths_set=1,
233237
module_search_paths=[
234238
r"C:\Out\python98.zip",
235-
r"C:\CPython\Lib",
236239
r"C:\Out",
240+
r"C:\CPython\Lib",
237241
],
238242
)
239243
actual = getpath(ns, expected)
@@ -255,8 +259,8 @@ def test_no_dlls_win32(self):
255259
module_search_paths_set=1,
256260
module_search_paths=[
257261
r"C:\Python\python98.zip",
258-
r"C:\Python\Lib",
259262
r"C:\Python",
263+
r"C:\Python\Lib",
260264
],
261265
)
262266
actual = getpath(ns, expected)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Ensures the ``PythonPath`` registry key from an install is used when
2+
launching from a different copy of Python that relies on an existing install
3+
to provide a copy of its modules and standard library.

‎Modules/getpath.py

+30-18
Original file line numberDiff line numberDiff line change
@@ -597,25 +597,27 @@ def search_up(prefix, *landmarks, test=isfile):
597597

598598
# Detect exec_prefix by searching from executable for the platstdlib_dir
599599
if PLATSTDLIB_LANDMARK and not exec_prefix:
600-
if executable_dir:
601-
if os_name == 'nt':
602-
# QUIRK: For compatibility and security, do not search for DLLs
603-
# directory. The fallback below will cover it
604-
exec_prefix = executable_dir
605-
else:
606-
exec_prefix = search_up(executable_dir, PLATSTDLIB_LANDMARK, test=isdir)
600+
if os_name == 'nt':
601+
# QUIRK: Windows always assumed these were the same
602+
# gh-100320: Our PYDs are assumed to be relative to the Lib directory
603+
# (that is, prefix) rather than the executable (that is, executable_dir)
604+
exec_prefix = prefix
605+
if not exec_prefix and executable_dir:
606+
exec_prefix = search_up(executable_dir, PLATSTDLIB_LANDMARK, test=isdir)
607607
if not exec_prefix and EXEC_PREFIX:
608608
exec_prefix = EXEC_PREFIX
609609
if not exec_prefix or not isdir(joinpath(exec_prefix, PLATSTDLIB_LANDMARK)):
610610
if os_name == 'nt':
611611
# QUIRK: If DLLs is missing on Windows, don't warn, just assume
612-
# that it's all the same as prefix.
613-
# gh-98790: We set platstdlib_dir here to avoid adding "DLLs" into
614-
# sys.path when it doesn't exist, which would give site-packages
615-
# precedence over executable_dir, which is *probably* where our PYDs
616-
# live. Ideally, whoever changes our layout will tell us what the
617-
# layout is, but in the past this worked, so it should keep working.
618-
platstdlib_dir = exec_prefix = prefix
612+
# that they're in exec_prefix
613+
if not platstdlib_dir:
614+
# gh-98790: We set platstdlib_dir here to avoid adding "DLLs" into
615+
# sys.path when it doesn't exist in the platstdlib place, which
616+
# would give Lib packages precedence over executable_dir where our
617+
# PYDs *probably* live. Ideally, whoever changes our layout will tell
618+
# us what the layout is, but in the past this worked, so it should
619+
# keep working.
620+
platstdlib_dir = exec_prefix
619621
else:
620622
warn('Could not find platform dependent libraries <exec_prefix>')
621623

@@ -701,8 +703,14 @@ def search_up(prefix, *landmarks, test=isfile):
701703
except OSError:
702704
break
703705
if isinstance(v, str):
704-
pythonpath.append(v)
706+
pythonpath.extend(v.split(DELIM))
705707
i += 1
708+
# Paths from the core key get appended last, but only
709+
# when home was not set and we aren't in a build dir
710+
if not home_was_set and not venv_prefix and not build_prefix:
711+
v = winreg.QueryValue(key, None)
712+
if isinstance(v, str):
713+
pythonpath.extend(v.split(DELIM))
706714
finally:
707715
winreg.CloseKey(key)
708716
except OSError:
@@ -714,13 +722,17 @@ def search_up(prefix, *landmarks, test=isfile):
714722
pythonpath.append(joinpath(prefix, p))
715723

716724
# Then add stdlib_dir and platstdlib_dir
717-
if os_name == 'nt' and venv_prefix:
718-
# QUIRK: Windows generates paths differently in a venv
725+
if os_name == 'nt':
726+
# QUIRK: Windows generates paths differently
719727
if platstdlib_dir:
720728
pythonpath.append(platstdlib_dir)
721729
if stdlib_dir:
722730
pythonpath.append(stdlib_dir)
723-
if executable_dir not in pythonpath:
731+
if executable_dir and executable_dir not in pythonpath:
732+
# QUIRK: the executable directory is on sys.path
733+
# We keep it low priority, so that properly installed modules are
734+
# found first. It may be earlier in the order if we found some
735+
# reason to put it there.
724736
pythonpath.append(executable_dir)
725737
else:
726738
if stdlib_dir:

0 commit comments

Comments
 (0)
Please sign in to comment.