Skip to content

Commit 32439d6

Browse files
Issue python#23911: Move path-based bootstrap code to a separate frozen module.
1 parent 6b4c63d commit 32439d6

27 files changed

Lines changed: 6192 additions & 5712 deletions

Lib/ctypes/test/test_values.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,24 @@ class struct_frozen(Structure):
5959
items = []
6060
# _frozen_importlib changes size whenever importlib._bootstrap
6161
# changes, so it gets a special case. We should make sure it's
62-
# found, but don't worry about its size too much.
63-
_fzn_implib_seen = False
62+
# found, but don't worry about its size too much. The same
63+
# applies to _frozen_importlib_external.
64+
bootstrap_seen = []
65+
bootstrap_expected = (
66+
b'_frozen_importlib',
67+
b'_frozen_importlib_external',
68+
)
6469
for entry in ft:
6570
# This is dangerous. We *can* iterate over a pointer, but
6671
# the loop will not terminate (maybe with an access
6772
# violation;-) because the pointer instance has no size.
6873
if entry.name is None:
6974
break
7075

71-
if entry.name == b'_frozen_importlib':
72-
_fzn_implib_seen = True
76+
if entry.name in bootstrap_expected:
77+
bootstrap_seen.append(entry.name)
7378
self.assertTrue(entry.size,
74-
"_frozen_importlib was reported as having no size")
79+
"{} was reported as having no size".format(entry.name))
7580
continue
7681
items.append((entry.name, entry.size))
7782

@@ -81,8 +86,8 @@ class struct_frozen(Structure):
8186
]
8287
self.assertEqual(items, expected)
8388

84-
self.assertTrue(_fzn_implib_seen,
85-
"_frozen_importlib wasn't found in PyImport_FrozenModules")
89+
self.assertEqual(sorted(bootstrap_seen), bootstrap_expected,
90+
"frozen bootstrap modules did not match PyImport_FrozenModules")
8691

8792
from ctypes import _pointer_type_cache
8893
del _pointer_type_cache[struct_frozen]

Lib/imp.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
# Platform doesn't support dynamic loading.
1717
load_dynamic = None
1818

19-
from importlib._bootstrap import SourcelessFileLoader, _ERR_MSG, _exec, _load
19+
from importlib._bootstrap import _ERR_MSG, _exec, _load
20+
from importlib._bootstrap_external import SourcelessFileLoader
2021

2122
from importlib import machinery
2223
from importlib import util

Lib/importlib/__init__.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,25 @@
3030
pass
3131
sys.modules['importlib._bootstrap'] = _bootstrap
3232

33+
try:
34+
import _frozen_importlib_external as _bootstrap_external
35+
except ImportError:
36+
from . import _bootstrap_external
37+
_bootstrap_external._setup(_bootstrap)
38+
else:
39+
_bootstrap_external.__name__ = 'importlib._bootstrap_external'
40+
_bootstrap_external.__package__ = 'importlib'
41+
try:
42+
_bootstrap_external.__file__ = __file__.replace('__init__.py', '_bootstrap_external.py')
43+
except NameError:
44+
# __file__ is not guaranteed to be defined, e.g. if this code gets
45+
# frozen by a tool like cx_Freeze.
46+
pass
47+
sys.modules['importlib._bootstrap_external'] = _bootstrap_external
48+
3349
# To simplify imports in test code
34-
_w_long = _bootstrap._w_long
35-
_r_long = _bootstrap._r_long
50+
_w_long = _bootstrap_external._w_long
51+
_r_long = _bootstrap_external._r_long
3652

3753
# Fully bootstrapped at this point, import whatever you like, circular
3854
# dependencies and startup overhead minimisation permitting :)

0 commit comments

Comments
 (0)