File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 22__all__ = ['__import__' , 'import_module' , 'invalidate_caches' ]
33
44# 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
613import sys
714
815try :
916 import _frozen_importlib as _bootstrap
1017except ImportError :
1118 from . import _bootstrap
12- _bootstrap ._setup (sys , imp )
19+ _bootstrap ._setup (sys , _imp )
1320else :
1421 # importlib._bootstrap is the built-in import, ensure we don't create
1522 # a second copy of the module.
2229_w_long = _bootstrap ._w_long
2330_r_long = _bootstrap ._r_long
2431
32+ # Fully bootstrapped at this point, import whatever you like, circular
33+ # dependencies and startup overhead minimisation permitting :)
2534
2635# Public API #########################################################
2736
Original file line number Diff line number Diff line change 22
33import os
44import sys
5- import imp
65import importlib
6+ import imp
77import os .path
88from warnings import warn
99from types import ModuleType
Original file line number Diff line number Diff line change 1212
1313import os
1414import sys
15+ import importlib .machinery # importlib first so we can test #15386 via -m
1516import imp
16- import importlib .machinery
1717from pkgutil import read_code , get_loader , get_importer
1818
1919__all__ = [
Original file line number Diff line number Diff line change 165165option '-uall,-gui'.
166166"""
167167
168+ # We import importlib *ASAP* in order to test #15386
169+ import importlib
170+
168171import builtins
169172import faulthandler
170173import getopt
Original file line number Diff line number Diff line change 1+ # We import importlib *ASAP* in order to test #15386
2+ import importlib
13import builtins
24import imp
35from importlib .test .import_ import test_suite as importlib_import_test_suite
46from importlib .test .import_ import util as importlib_util
5- import importlib
67import marshal
78import os
89import platform
@@ -777,6 +778,15 @@ def test_frozen_importlib_is_bootstrap(self):
777778 self .assertEqual (mod .__package__ , 'importlib' )
778779 self .assertTrue (mod .__file__ .endswith ('_bootstrap.py' ), mod .__file__ )
779780
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+
780790
781791class ImportTracebackTests (unittest .TestCase ):
782792
You can’t perform that action at this time.
0 commit comments