File tree Expand file tree Collapse file tree 5 files changed +27
-5
lines changed Expand file tree Collapse file tree 5 files changed +27
-5
lines changed Original file line number Diff line number Diff line change 2
2
__all__ = ['__import__' , 'import_module' , 'invalidate_caches' ]
3
3
4
4
# Bootstrap help #####################################################
5
- import imp
5
+
6
+ # Until bootstrapping is complete, DO NOT import any modules that attempt
7
+ # to import importlib._bootstrap (directly or indirectly). Since this
8
+ # partially initialised package would be present in sys.modules, those
9
+ # modules would get an uninitialised copy of the source version, instead
10
+ # of a fully initialised version (either the frozen one or the one
11
+ # initialised below if the frozen one is not available).
12
+ import _imp # Just the builtin component, NOT the full Python module
6
13
import sys
7
14
8
15
try :
9
16
import _frozen_importlib as _bootstrap
10
17
except ImportError :
11
18
from . import _bootstrap
12
- _bootstrap ._setup (sys , imp )
19
+ _bootstrap ._setup (sys , _imp )
13
20
else :
14
21
# importlib._bootstrap is the built-in import, ensure we don't create
15
22
# a second copy of the module.
22
29
_w_long = _bootstrap ._w_long
23
30
_r_long = _bootstrap ._r_long
24
31
32
+ # Fully bootstrapped at this point, import whatever you like, circular
33
+ # dependencies and startup overhead minimisation permitting :)
25
34
26
35
# Public API #########################################################
27
36
Original file line number Diff line number Diff line change 2
2
3
3
import os
4
4
import sys
5
- import imp
6
5
import importlib
6
+ import imp
7
7
import os .path
8
8
from warnings import warn
9
9
from types import ModuleType
Original file line number Diff line number Diff line change 12
12
13
13
import os
14
14
import sys
15
+ import importlib .machinery # importlib first so we can test #15386 via -m
15
16
import imp
16
- import importlib .machinery
17
17
from pkgutil import read_code , get_loader , get_importer
18
18
19
19
__all__ = [
Original file line number Diff line number Diff line change 165
165
option '-uall,-gui'.
166
166
"""
167
167
168
+ # We import importlib *ASAP* in order to test #15386
169
+ import importlib
170
+
168
171
import builtins
169
172
import faulthandler
170
173
import getopt
Original file line number Diff line number Diff line change
1
+ # We import importlib *ASAP* in order to test #15386
2
+ import importlib
1
3
import builtins
2
4
import imp
3
5
from importlib .test .import_ import test_suite as importlib_import_test_suite
4
6
from importlib .test .import_ import util as importlib_util
5
- import importlib
6
7
import marshal
7
8
import os
8
9
import platform
@@ -777,6 +778,15 @@ def test_frozen_importlib_is_bootstrap(self):
777
778
self .assertEqual (mod .__package__ , 'importlib' )
778
779
self .assertTrue (mod .__file__ .endswith ('_bootstrap.py' ), mod .__file__ )
779
780
781
+ def test_there_can_be_only_one (self ):
782
+ # Issue #15386 revealed a tricky loophole in the bootstrapping
783
+ # This test is technically redundant, since the bug caused importing
784
+ # this test module to crash completely, but it helps prove the point
785
+ from importlib import machinery
786
+ mod = sys .modules ['_frozen_importlib' ]
787
+ self .assertIs (machinery .FileFinder , mod .FileFinder )
788
+ self .assertIs (imp .new_module , mod .new_module )
789
+
780
790
781
791
class ImportTracebackTests (unittest .TestCase ):
782
792
You can’t perform that action at this time.
0 commit comments