diff Lib/importlib/_bootstrap.py @ 83998:39cc1b04713e

Add a reset_name argument to importlib.util.module_to_load in order to control whether to reset the module's __name__ attribute in case a reload is being done.
author Brett Cannon <brett@python.org>
date Fri, 31 May 2013 18:11:17 -0400
parents 6ace4133c1c0
children 130dfd6b3428
line wrap: on
line diff
--- a/Lib/importlib/_bootstrap.py	Fri May 31 18:02:11 2013 -0400
+++ b/Lib/importlib/_bootstrap.py	Fri May 31 18:11:17 2013 -0400
@@ -493,8 +493,14 @@
 
     """
 
-    def __init__(self, name):
+    def __init__(self, name, *, reset_name=True):
+        """Prepare the context manager.
+
+        The reset_name argument specifies whether to unconditionally reset
+        the __name__ attribute if the module is found to be a reload.
+        """
         self._name = name
+        self._reset_name = reset_name
 
     def __enter__(self):
         self._module = sys.modules.get(self._name)
@@ -508,6 +514,12 @@
             # (otherwise an optimization shortcut in import.c becomes wrong)
             self._module.__initializing__ = True
             sys.modules[self._name] = self._module
+        elif self._reset_name:
+            try:
+                self._module.__name__ = self._name
+            except AttributeError:
+                pass
+
         return self._module
 
     def __exit__(self, *args):