]> granicus.if.org Git - python/commitdiff
Issue #14657: The frozen instance of importlib used for bootstrap is now also the...
authorAntoine Pitrou <solipsis@pitrou.net>
Sun, 17 Jun 2012 20:33:38 +0000 (22:33 +0200)
committerAntoine Pitrou <solipsis@pitrou.net>
Sun, 17 Jun 2012 20:33:38 +0000 (22:33 +0200)
Lib/importlib/__init__.py
Lib/test/test_import.py
Misc/NEWS

index 90e163c433b0889812993279dbaded27eab1077c..e62bdb1654e553fda484eb7088cd2325657e0c59 100644 (file)
@@ -1,21 +1,28 @@
 """A pure Python implementation of import."""
 __all__ = ['__import__', 'import_module', 'invalidate_caches']
 
-from . import _bootstrap
+# Bootstrap help #####################################################
+import imp
+import sys
 
+try:
+    _bootstrap = sys.modules['_frozen_importlib']
+except ImportError:
+    from . import _bootstrap
+    _bootstrap._setup(sys, imp)
+else:
+    # importlib._bootstrap is the built-in import, ensure we don't create
+    # a second copy of the module.
+    _bootstrap.__name__ = 'importlib._bootstrap'
+    _bootstrap.__package__ = 'importlib'
+    _bootstrap.__file__ = __file__.replace('__init__.py', '_bootstrap.py')
+    sys.modules['importlib._bootstrap'] = _bootstrap
 
 # To simplify imports in test code
 _w_long = _bootstrap._w_long
 _r_long = _bootstrap._r_long
 
 
-# Bootstrap help #####################################################
-import imp
-import sys
-
-_bootstrap._setup(sys, imp)
-
-
 # Public API #########################################################
 
 from ._bootstrap import __import__
index 01441ad70ecfc66c6ed5f851fc1d000559a3fe57..7ae690bbdeb1c3872622f246a8632aecdc7afdc4 100644 (file)
@@ -19,7 +19,7 @@ import test.support
 from test.support import (
     EnvironmentVarGuard, TESTFN, check_warnings, forget, is_jython,
     make_legacy_pyc, rmtree, run_unittest, swap_attr, swap_item, temp_umask,
-    unlink, unload, create_empty_file)
+    unlink, unload, create_empty_file, cpython_only)
 from test import script_helper
 
 
@@ -746,6 +746,23 @@ class TestSymbolicallyLinkedPackage(unittest.TestCase):
         sys.path[:] = self.orig_sys_path
 
 
+@cpython_only
+class ImportlibBootstrapTests(unittest.TestCase):
+    # These tests check that importlib is bootstrapped.
+
+    def test_frozen_importlib(self):
+        mod = sys.modules['_frozen_importlib']
+        self.assertTrue(mod)
+
+    def test_frozen_importlib_is_bootstrap(self):
+        from importlib import _bootstrap
+        mod = sys.modules['_frozen_importlib']
+        self.assertIs(mod, _bootstrap)
+        self.assertEqual(mod.__name__, 'importlib._bootstrap')
+        self.assertEqual(mod.__package__, 'importlib')
+        self.assertTrue(mod.__file__.endswith('_bootstrap.py'), mod.__file__)
+
+
 def test_main(verbose=None):
     flag = importlib_util.using___import__
     try:
@@ -753,6 +770,7 @@ def test_main(verbose=None):
         run_unittest(ImportTests, PycacheTests,
                      PycRewritingTests, PathsTests, RelativeImportTests,
                      OverridingImportBuiltinTests,
+                     ImportlibBootstrapTests,
                      TestSymbolicallyLinkedPackage,
                      importlib_import_test_suite())
     finally:
index c6ba9dd41ff2a5b9283dd826c949468d9264889c..3321b244da6f6d38e173c96704d5b9596565640a 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -29,6 +29,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #14657: The frozen instance of importlib used for bootstrap is now
+  also the module imported as importlib._bootstrap.
+
 - Issue #14055: Add __sizeof__ support to _elementtree.
 
 - Issue #15054: A bug in tokenize.tokenize that caused string literals