Skip to content

Commit 83d46e0

Browse files
authored
bpo-40050: Fix importlib._bootstrap_external (GH-19135)
Remove two unused imports: _thread and _weakref. Avoid creating a new winreg builtin module if it's already available in sys.modules. The winreg module is now stored as "winreg" rather than "_winreg".
1 parent 9b8e74c commit 83d46e0

File tree

3 files changed

+1745
-1757
lines changed

3 files changed

+1745
-1757
lines changed

Lib/importlib/_bootstrap_external.py

+13-21
Original file line numberDiff line numberDiff line change
@@ -716,9 +716,9 @@ class WindowsRegistryFinder:
716716
@classmethod
717717
def _open_registry(cls, key):
718718
try:
719-
return _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, key)
719+
return winreg.OpenKey(winreg.HKEY_CURRENT_USER, key)
720720
except OSError:
721-
return _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, key)
721+
return winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, key)
722722

723723
@classmethod
724724
def _search_registry(cls, fullname):
@@ -730,7 +730,7 @@ def _search_registry(cls, fullname):
730730
sys_version='%d.%d' % sys.version_info[:2])
731731
try:
732732
with cls._open_registry(key) as hkey:
733-
filepath = _winreg.QueryValue(hkey, '')
733+
filepath = winreg.QueryValue(hkey, '')
734734
except OSError:
735735
return None
736736
return filepath
@@ -1584,14 +1584,7 @@ def _setup(_bootstrap_module):
15841584
sys = _bootstrap.sys
15851585
_imp = _bootstrap._imp
15861586

1587-
# Directly load built-in modules needed during bootstrap.
15881587
self_module = sys.modules[__name__]
1589-
for builtin_name in ('_io', '_warnings', 'builtins', 'marshal'):
1590-
if builtin_name not in sys.modules:
1591-
builtin_module = _bootstrap._builtin_from_name(builtin_name)
1592-
else:
1593-
builtin_module = sys.modules[builtin_name]
1594-
setattr(self_module, builtin_name, builtin_module)
15951588

15961589
# Directly load the os module (needed during bootstrap).
15971590
os_details = ('posix', ['/']), ('nt', ['\\', '/'])
@@ -1610,23 +1603,22 @@ def _setup(_bootstrap_module):
16101603
continue
16111604
else:
16121605
raise ImportError('importlib requires posix or nt')
1606+
16131607
setattr(self_module, '_os', os_module)
16141608
setattr(self_module, 'path_sep', path_sep)
16151609
setattr(self_module, 'path_separators', ''.join(path_separators))
16161610
setattr(self_module, '_pathseps_with_colon', {f':{s}' for s in path_separators})
16171611

1618-
# Directly load the _thread module (needed during bootstrap).
1619-
thread_module = _bootstrap._builtin_from_name('_thread')
1620-
setattr(self_module, '_thread', thread_module)
1621-
1622-
# Directly load the _weakref module (needed during bootstrap).
1623-
weakref_module = _bootstrap._builtin_from_name('_weakref')
1624-
setattr(self_module, '_weakref', weakref_module)
1625-
1626-
# Directly load the winreg module (needed during bootstrap).
1612+
# Directly load built-in modules needed during bootstrap.
1613+
builtin_names = ['_io', '_warnings', 'marshal']
16271614
if builtin_os == 'nt':
1628-
winreg_module = _bootstrap._builtin_from_name('winreg')
1629-
setattr(self_module, '_winreg', winreg_module)
1615+
builtin_names.append('winreg')
1616+
for builtin_name in builtin_names:
1617+
if builtin_name not in sys.modules:
1618+
builtin_module = _bootstrap._builtin_from_name(builtin_name)
1619+
else:
1620+
builtin_module = sys.modules[builtin_name]
1621+
setattr(self_module, builtin_name, builtin_module)
16301622

16311623
# Constants
16321624
setattr(self_module, '_relax_case', _make_relax_case())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix ``importlib._bootstrap_external``. Remove two unused imports importlib:
2+
``_thread`` and ``_weakref``. Avoid creating a new ``winreg`` builtin module if
3+
it's already available in :data:`sys.modules`.

0 commit comments

Comments
 (0)