[Python-Dev] [Python-checkins] cpython: Add a reset_name argument to importlib.util.module_to_load in order to (original) (raw)
Brett Cannon brett at python.org
Sat Jun 1 00:37:13 CEST 2013
- Next message: [Python-Dev] New FreeBSD 10.0-CURRENT buildbot
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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.
On Fri, May 31, 2013 at 6:11 PM, brett.cannon <python-checkins at python.org>wrote:
http://hg.python.org/cpython/rev/39cc1b04713e changeset: 83998:39cc1b04713e user: Brett Cannon <brett at python.org> date: Fri May 31 18:11:17 2013 -0400 summary: Add a resetname argument to importlib.util.moduletoload in order to control whether to reset the module's name attribute in case a reload is being done.
files: Doc/library/importlib.rst | 6 +++++- Lib/importlib/bootstrap.py | 14 +++++++++++++- Lib/test/testimportlib/testutil.py | 12 ++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-)
diff --git a/Doc/library/importlib.rst b/Doc/library/importlib.rst --- a/Doc/library/importlib.rst +++ b/Doc/library/importlib.rst @@ -788,7 +788,7 @@ .. versionadded:: 3.3 -.. function:: moduletoload(name) +.. function:: moduletoload(name, *, resetname=True) Returns a :term:
context manager
which provides the module to load. The module will either come from :attr:sys.modules
in the case of reloading or @@ -796,6 +796,10 @@ :attr:sys.modules
occurs if the module was new and an exception was raised. + If resetname is true and the module requested is being reloaded then + the module's :attr:_name_
attribute will + be reset to name, else it will be left untouched. + .. versionadded:: 3.4 .. decorator:: moduleforloader diff --git a/Lib/importlib/bootstrap.py b/Lib/importlib/bootstrap.py --- a/Lib/importlib/bootstrap.py +++ b/Lib/importlib/bootstrap.py @@ -493,8 +493,14 @@ """ - def init(self, name): + def init(self, name, *, resetname=True): + """Prepare the context manager. + + The resetname argument specifies whether to unconditionally reset + the name attribute if the module is found to be a reload. + """ self.name = name + self.resetname = resetname 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.resetname: + try: + self.module.name = self.name + except AttributeError: + pass + return self.module def exit(self, *args): diff --git a/Lib/test/testimportlib/testutil.py b/Lib/test/testimportlib/testutil.py --- a/Lib/test/testimportlib/testutil.py +++ b/Lib/test/testimportlib/testutil.py @@ -55,6 +55,18 @@ else: self.fail('importlib.util.moduletoload swallowed an exception') + def testresetname(self): + # If resetname is true then module.name = name, else leave it be. + oddname = 'not your typical name' + createdmodule = imp.newmodule(self.modulename) + createdmodule.name = oddname + sys.modules[self.modulename] = createdmodule + with util.moduletoload(self.modulename) as module: + self.assertEqual(module.name, self.modulename) + createdmodule.name = oddname + with util.moduletoload(self.modulename, resetname=False) as module: + self.assertEqual(module.name, oddname) + class ModuleForLoaderTests(unittest.TestCase): -- Repository URL: http://hg.python.org/cpython
Python-checkins mailing list Python-checkins at python.org http://mail.python.org/mailman/listinfo/python-checkins -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-dev/attachments/20130531/c1bf41a0/attachment.html>
- Next message: [Python-Dev] New FreeBSD 10.0-CURRENT buildbot
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]