Skip to content

Commit c9dc1f4

Browse files
authored
bpo-46297: Fix interpreter crash on startup with multiple PythonPaths set in registry (GH-30466)
1 parent 74d1663 commit c9dc1f4

File tree

4 files changed

+13
-14
lines changed

4 files changed

+13
-14
lines changed

Lib/test/test_getpath.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -734,12 +734,15 @@ def EnumKey(self, hkey, i):
734734
return n.removeprefix(prefix)
735735
raise OSError("end of enumeration")
736736

737-
def QueryValue(self, hkey):
737+
def QueryValue(self, hkey, subkey):
738738
if verbose:
739-
print(f"QueryValue({hkey})")
739+
print(f"QueryValue({hkey}, {subkey})")
740740
hkey = hkey.casefold()
741741
if hkey not in self.open:
742742
raise RuntimeError("key is not open")
743+
if subkey:
744+
subkey = subkey.casefold()
745+
hkey = f'{hkey}\\{subkey}'
743746
try:
744747
return self.keys[hkey]
745748
except KeyError:

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,7 @@ Lars Damerow
400400
Evan Dandrea
401401
Eric Daniel
402402
Scott David Daniels
403+
Derzsi Dániel
403404
Lawrence D'Anna
404405
Ben Darnell
405406
Kushal Das
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixed an interpreter crash on bootup with multiple PythonPaths set in
2+
the Windows registry. Patch by Derzsi Dániel.

Modules/getpath.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@
127127
# checked by looking for the BUILDDIR_TXT file, which contains the
128128
# relative path to the platlib dir. The executable_dir value is
129129
# derived from joining the VPATH preprocessor variable to the
130-
# directory containing pybuilddir.txt. If it is not found, the
130+
# directory containing pybuilddir.txt. If it is not found, the
131131
# BUILD_LANDMARK file is found, which is part of the source tree.
132132
# prefix is then found by searching up for a file that should only
133133
# exist in the source tree, and the stdlib dir is set to prefix/Lib.
@@ -642,19 +642,12 @@ def search_up(prefix, *landmarks, test=isfile):
642642
i = 0
643643
while True:
644644
try:
645-
keyname = winreg.EnumKey(key, i)
646-
subkey = winreg.OpenKeyEx(key, keyname)
647-
if not subkey:
648-
continue
649-
try:
650-
v = winreg.QueryValue(subkey)
651-
finally:
652-
winreg.CloseKey(subkey)
653-
if isinstance(v, str):
654-
pythonpath.append(v)
655-
i += 1
645+
v = winreg.QueryValue(key, winreg.EnumKey(key, i))
656646
except OSError:
657647
break
648+
if isinstance(v, str):
649+
pythonpath.append(v)
650+
i += 1
658651
finally:
659652
winreg.CloseKey(key)
660653
except OSError:

0 commit comments

Comments
 (0)