mirror of
https://github.com/python/cpython.git
synced 2025-08-31 05:58:33 +00:00
Issue #17358: imp.load_source() and load_compiled() should now return
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.
This commit is contained in:
parent
9330a94467
commit
5a4c233a9e
2 changed files with 15 additions and 2 deletions
14
Lib/imp.py
14
Lib/imp.py
|
@ -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):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue