]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #17358: imp.load_source() and load_compiled() should now return
authorBrett Cannon <brett@python.org>
Sun, 28 Apr 2013 15:53:26 +0000 (11:53 -0400)
committerBrett Cannon <brett@python.org>
Sun, 28 Apr 2013 15:53:26 +0000 (11:53 -0400)
modules which will typically work when reloaded.

A hack is used to support these functions as their API allowed them to
pass in a file object but then operate as if import had loaded them.
Unfortunately the hack kept a reference around for the file object
passed in which would be unusable on reload since it had been closed.
The solution is to simply use the hack for the initial load but then a
proper loader on the module so that imp.reload() at least has a chance
to work.

Lib/imp.py
Misc/NEWS

index 7a7079f71d587aea34d45f284b25c3df04e85f21..6d156d3115c53e248a5264a7683625ca41f030aa 100644 (file)
@@ -111,7 +111,12 @@ def load_source(name, pathname, file=None):
            'importlib.machinery.SourceFileLoader(name, pathname).load_module()'
            ' instead')
     warnings.warn(msg, DeprecationWarning, 2)
-    return _LoadSourceCompatibility(name, pathname, file).load_module(name)
+    _LoadSourceCompatibility(name, pathname, file).load_module(name)
+    module = sys.modules[name]
+    # To allow reloading to potentially work, use a non-hacked loader which
+    # won't rely on a now-closed file object.
+    module.__loader__ = _bootstrap.SourceFileLoader(name, pathname)
+    return module
 
 
 class _LoadCompiledCompatibility(_HackedGetData,
@@ -125,7 +130,12 @@ def load_compiled(name, pathname, file=None):
            'importlib.machinery.SourcelessFileLoader(name, pathname).'
            'load_module() instead ')
     warnings.warn(msg, DeprecationWarning, 2)
-    return _LoadCompiledCompatibility(name, pathname, file).load_module(name)
+    _LoadCompiledCompatibility(name, pathname, file).load_module(name)
+    module = sys.modules[name]
+    # To allow reloading to potentially work, use a non-hacked loader which
+    # won't rely on a now-closed file object.
+    module.__loader__ = _bootstrap.SourcelessFileLoader(name, pathname)
+    return module
 
 
 def load_package(name, path):
index 3c9f4a0a19c4ca713816c18a4e9d7a30064ec1e1..6e6ee2ce78dca808310297e1c2427544c1df00a7 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -39,6 +39,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #17358: Modules loaded by imp.load_source() and load_compiled() (and by
+  extention load_module()) now have a better chance of working when reloaded.
+
 - Issue #17353: Plistlib emitted empty data tags with deeply nested datastructures
 
 - Issue #11714: Use 'with' statements to assure a Semaphore releases a