<div dir="ltr">I realize this broke the buildbots. Missed part of a diff in the commit. I'm trying to split a massive CL into reasonable commit sizes, so please be patient.</div><div class="gmail_extra"><br><br><div class="gmail_quote">
On Fri, May 31, 2013 at 6:11 PM, brett.cannon <span dir="ltr"><<a href="mailto:python-checkins@python.org" target="_blank">python-checkins@python.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<a href="http://hg.python.org/cpython/rev/39cc1b04713e" target="_blank">http://hg.python.org/cpython/rev/39cc1b04713e</a><br>
changeset: 83998:39cc1b04713e<br>
user: Brett Cannon <<a href="mailto:brett@python.org">brett@python.org</a>><br>
date: Fri May 31 18:11:17 2013 -0400<br>
summary:<br>
Add a reset_name argument to importlib.util.module_to_load in order to<br>
control whether to reset the module's __name__ attribute in case a<br>
reload is being done.<br>
<br>
files:<br>
Doc/library/importlib.rst | 6 +++++-<br>
Lib/importlib/_bootstrap.py | 14 +++++++++++++-<br>
Lib/test/test_importlib/test_util.py | 12 ++++++++++++<br>
3 files changed, 30 insertions(+), 2 deletions(-)<br>
<br>
<br>
diff --git a/Doc/library/importlib.rst b/Doc/library/importlib.rst<br>
--- a/Doc/library/importlib.rst<br>
+++ b/Doc/library/importlib.rst<br>
@@ -788,7 +788,7 @@<br>
<br>
.. versionadded:: 3.3<br>
<br>
-.. function:: module_to_load(name)<br>
+.. function:: module_to_load(name, *, reset_name=True)<br>
<br>
Returns a :term:`context manager` which provides the module to load. The<br>
module will either come from :attr:`sys.modules` in the case of reloading or<br>
@@ -796,6 +796,10 @@<br>
:attr:`sys.modules` occurs if the module was new and an exception was<br>
raised.<br>
<br>
+ If **reset_name** is true and the module requested is being reloaded then<br>
+ the module's :attr:`__name__` attribute will<br>
+ be reset to **name**, else it will be left untouched.<br>
+<br>
.. versionadded:: 3.4<br>
<br>
.. decorator:: module_for_loader<br>
diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py<br>
--- a/Lib/importlib/_bootstrap.py<br>
+++ b/Lib/importlib/_bootstrap.py<br>
@@ -493,8 +493,14 @@<br>
<br>
"""<br>
<br>
- def __init__(self, name):<br>
+ def __init__(self, name, *, reset_name=True):<br>
+ """Prepare the context manager.<br>
+<br>
+ The reset_name argument specifies whether to unconditionally reset<br>
+ the __name__ attribute if the module is found to be a reload.<br>
+ """<br>
self._name = name<br>
+ self._reset_name = reset_name<br>
<br>
def __enter__(self):<br>
self._module = sys.modules.get(self._name)<br>
@@ -508,6 +514,12 @@<br>
# (otherwise an optimization shortcut in import.c becomes wrong)<br>
self._module.__initializing__ = True<br>
sys.modules[self._name] = self._module<br>
+ elif self._reset_name:<br>
+ try:<br>
+ self._module.__name__ = self._name<br>
+ except AttributeError:<br>
+ pass<br>
+<br>
return self._module<br>
<br>
def __exit__(self, *args):<br>
diff --git a/Lib/test/test_importlib/test_util.py b/Lib/test/test_importlib/test_util.py<br>
--- a/Lib/test/test_importlib/test_util.py<br>
+++ b/Lib/test/test_importlib/test_util.py<br>
@@ -55,6 +55,18 @@<br>
else:<br>
self.fail('importlib.util.module_to_load swallowed an exception')<br>
<br>
+ def test_reset_name(self):<br>
+ # If reset_name is true then module.__name__ = name, else leave it be.<br>
+ odd_name = 'not your typical name'<br>
+ created_module = imp.new_module(self.module_name)<br>
+ created_module.__name__ = odd_name<br>
+ sys.modules[self.module_name] = created_module<br>
+ with util.module_to_load(self.module_name) as module:<br>
+ self.assertEqual(module.__name__, self.module_name)<br>
+ created_module.__name__ = odd_name<br>
+ with util.module_to_load(self.module_name, reset_name=False) as module:<br>
+ self.assertEqual(module.__name__, odd_name)<br>
+<br>
<br>
class ModuleForLoaderTests(unittest.TestCase):<br>
<span class="HOEnZb"><font color="#888888"><br>
<br>
--<br>
Repository URL: <a href="http://hg.python.org/cpython" target="_blank">http://hg.python.org/cpython</a><br>
</font></span><br>_______________________________________________<br>
Python-checkins mailing list<br>
<a href="mailto:Python-checkins@python.org">Python-checkins@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/python-checkins" target="_blank">http://mail.python.org/mailman/listinfo/python-checkins</a><br>
<br></blockquote></div><br></div>