Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 48114b9

Browse files
committed
Issue #14657: The frozen instance of importlib used for bootstrap is now also the module imported as importlib._bootstrap.
1 parent 7636b19 commit 48114b9

3 files changed

Lines changed: 37 additions & 9 deletions

File tree

Lib/importlib/__init__.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
"""A pure Python implementation of import."""
22
__all__ = ['__import__', 'import_module', 'invalidate_caches']
33

4-
from . import _bootstrap
4+
# Bootstrap help #####################################################
5+
import imp
6+
import sys
57

8+
try:
9+
_bootstrap = sys.modules['_frozen_importlib']
10+
except ImportError:
11+
from . import _bootstrap
12+
_bootstrap._setup(sys, imp)
13+
else:
14+
# importlib._bootstrap is the built-in import, ensure we don't create
15+
# a second copy of the module.
16+
_bootstrap.__name__ = 'importlib._bootstrap'
17+
_bootstrap.__package__ = 'importlib'
18+
_bootstrap.__file__ = __file__.replace('__init__.py', '_bootstrap.py')
19+
sys.modules['importlib._bootstrap'] = _bootstrap
620

721
# To simplify imports in test code
822
_w_long = _bootstrap._w_long
923
_r_long = _bootstrap._r_long
1024

1125

12-
# Bootstrap help #####################################################
13-
import imp
14-
import sys
15-
16-
_bootstrap._setup(sys, imp)
17-
18-
1926
# Public API #########################################################
2027

2128
from ._bootstrap import __import__

Lib/test/test_import.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from test.support import (
2020
EnvironmentVarGuard, TESTFN, check_warnings, forget, is_jython,
2121
make_legacy_pyc, rmtree, run_unittest, swap_attr, swap_item, temp_umask,
22-
unlink, unload, create_empty_file)
22+
unlink, unload, create_empty_file, cpython_only)
2323
from test import script_helper
2424

2525

@@ -746,13 +746,31 @@ def tearDown(self):
746746
sys.path[:] = self.orig_sys_path
747747

748748

749+
@cpython_only
750+
class ImportlibBootstrapTests(unittest.TestCase):
751+
# These tests check that importlib is bootstrapped.
752+
753+
def test_frozen_importlib(self):
754+
mod = sys.modules['_frozen_importlib']
755+
self.assertTrue(mod)
756+
757+
def test_frozen_importlib_is_bootstrap(self):
758+
from importlib import _bootstrap
759+
mod = sys.modules['_frozen_importlib']
760+
self.assertIs(mod, _bootstrap)
761+
self.assertEqual(mod.__name__, 'importlib._bootstrap')
762+
self.assertEqual(mod.__package__, 'importlib')
763+
self.assertTrue(mod.__file__.endswith('_bootstrap.py'), mod.__file__)
764+
765+
749766
def test_main(verbose=None):
750767
flag = importlib_util.using___import__
751768
try:
752769
importlib_util.using___import__ = True
753770
run_unittest(ImportTests, PycacheTests,
754771
PycRewritingTests, PathsTests, RelativeImportTests,
755772
OverridingImportBuiltinTests,
773+
ImportlibBootstrapTests,
756774
TestSymbolicallyLinkedPackage,
757775
importlib_import_test_suite())
758776
finally:

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ Core and Builtins
2929
Library
3030
-------
3131

32+
- Issue #14657: The frozen instance of importlib used for bootstrap is now
33+
also the module imported as importlib._bootstrap.
34+
3235
- Issue #14055: Add __sizeof__ support to _elementtree.
3336

3437
- Issue #15054: A bug in tokenize.tokenize that caused string literals

0 commit comments

Comments
 (0)