diff Lib/importlib/_bootstrap.py @ 78779:7fa6336e9864

Issue #15781: Fix two small race conditions in import's module locking.
author Antoine Pitrou <solipsis@pitrou.net>
date Tue, 28 Aug 2012 00:24:52 +0200
parents ca4bf8e10bc0
children 4de5e4ec3cff
line wrap: on
line diff
--- a/Lib/importlib/_bootstrap.py	Mon Aug 27 20:27:51 2012 +0300
+++ b/Lib/importlib/_bootstrap.py	Tue Aug 28 00:24:52 2012 +0200
@@ -268,8 +268,10 @@
 
     Should only be called with the import lock taken."""
     lock = None
-    if name in _module_locks:
+    try:
         lock = _module_locks[name]()
+    except KeyError:
+        pass
     if lock is None:
         if _thread is None:
             lock = _DummyModuleLock(name)
@@ -543,6 +545,9 @@
             # implicitly imports 'locale' and would otherwise trigger an
             # infinite loop.
             module = new_module(fullname)
+            # This must be done before putting the module in sys.modules
+            # (otherwise an optimization shortcut in import.c becomes wrong)
+            module.__initializing__ = True
             sys.modules[fullname] = module
             module.__loader__ = self
             try:
@@ -554,8 +559,9 @@
                     module.__package__ = fullname
                 else:
                     module.__package__ = fullname.rpartition('.')[0]
+        else:
+            module.__initializing__ = True
         try:
-            module.__initializing__ = True
             # If __package__ was not set above, __import__() will do it later.
             return fxn(self, module, *args, **kwargs)
         except: