]> granicus.if.org Git - python/commitdiff
Close #24748: Restore imp.load_dynamic compatibility
authorNick Coghlan <ncoghlan@gmail.com>
Sat, 5 Sep 2015 11:05:05 +0000 (21:05 +1000)
committerNick Coghlan <ncoghlan@gmail.com>
Sat, 5 Sep 2015 11:05:05 +0000 (21:05 +1000)
To resolve a compatibility problem found with py2exe and
pywin32, imp.load_dynamic() once again ignores previously loaded modules
to support Python modules replacing themselves with extension modules.

Patch by Petr Viktorin.

Lib/imp.py
Lib/test/imp_dummy.py [new file with mode: 0644]
Lib/test/test_imp.py
Misc/NEWS
Modules/_testmultiphase.c

index 2cd64407e8688d633cd389d04128325aa9f38323..f6fff442013ae1d719f28bc1eaa91b337788c6cc 100644 (file)
@@ -334,6 +334,12 @@ if create_dynamic:
         """
         import importlib.machinery
         loader = importlib.machinery.ExtensionFileLoader(name, path)
-        return loader.load_module()
+
+        # Issue #24748: Skip the sys.modules check in _load_module_shim;
+        # always load new extension
+        spec = importlib.machinery.ModuleSpec(
+            name=name, loader=loader, origin=path)
+        return _load(spec)
+
 else:
     load_dynamic = None
diff --git a/Lib/test/imp_dummy.py b/Lib/test/imp_dummy.py
new file mode 100644 (file)
index 0000000..2a4deb4
--- /dev/null
@@ -0,0 +1,3 @@
+# Fodder for test of issue24748 in test_imp
+
+dummy_name = True
index 47bf1de92a43a2393fe87e2ff0d4a5626e5db4f5..ee9ee1ad8c3bcc1237ac1339679e4cd1d8645cb4 100644 (file)
@@ -3,6 +3,7 @@ try:
 except ImportError:
     _thread = None
 import importlib
+import importlib.util
 import os
 import os.path
 import shutil
@@ -275,6 +276,29 @@ class ImportTests(unittest.TestCase):
             self.skipTest("found module doesn't appear to be a C extension")
         imp.load_module(name, None, *found[1:])
 
+    @requires_load_dynamic
+    def test_issue24748_load_module_skips_sys_modules_check(self):
+        name = 'test.imp_dummy'
+        try:
+            del sys.modules[name]
+        except KeyError:
+            pass
+        try:
+            module = importlib.import_module(name)
+            spec = importlib.util.find_spec('_testmultiphase')
+            module = imp.load_dynamic(name, spec.origin)
+            self.assertEqual(module.__name__, name)
+            self.assertEqual(module.__spec__.name, name)
+            self.assertEqual(module.__spec__.origin, spec.origin)
+            self.assertRaises(AttributeError, getattr, module, 'dummy_name')
+            self.assertEqual(module.int_const, 1969)
+            self.assertIs(sys.modules[name], module)
+        finally:
+            try:
+                del sys.modules[name]
+            except KeyError:
+                pass
+
     @unittest.skipIf(sys.dont_write_bytecode,
         "test meaningful only when writing bytecode")
     def test_bug7732(self):
index 288fa62cfa3f24b121122ec048c36af45da7682c..7ea23875310467a0bbbf7c0876a69a2ab26b8e91 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -15,6 +15,11 @@ Core and Builtins
 Library
 -------
 
+- Issue #24748: To resolve a compatibility problem found with py2exe and
+  pywin32, imp.load_dynamic() once again ignores previously loaded modules
+  to support Python modules replacing themselves with extension modules.
+  Patch by Petr Viktorin.
+
 - Issue #24635: Fixed a bug in typing.py where isinstance([], typing.Iterable)
   would return True once, then False on subsequent calls.
 
index 2919687e112db74a42d15e256dada78be11b7f3a..2005205d3358272f313b1cb97049aee1c92551b2 100644 (file)
@@ -582,3 +582,13 @@ PyInit__testmultiphase_exec_unreported_exception(PyObject *spec)
 {
     return PyModuleDef_Init(&def_exec_unreported_exception);
 }
+
+/*** Helper for imp test ***/
+
+static PyModuleDef imp_dummy_def = TEST_MODULE_DEF("imp_dummy", main_slots, testexport_methods);
+
+PyMODINIT_FUNC
+PyInit_imp_dummy(PyObject *spec)
+{
+    return PyModuleDef_Init(&imp_dummy_def);
+}