Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e1d30f2

Browse files
committed
merge for issue #17358
2 parents d4c1b36 + 5a4c233 commit e1d30f2

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

Lib/imp.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,12 @@ def load_source(name, pathname, file=None):
111111
'importlib.machinery.SourceFileLoader(name, pathname).load_module()'
112112
' instead')
113113
warnings.warn(msg, DeprecationWarning, 2)
114-
return _LoadSourceCompatibility(name, pathname, file).load_module(name)
114+
_LoadSourceCompatibility(name, pathname, file).load_module(name)
115+
module = sys.modules[name]
116+
# To allow reloading to potentially work, use a non-hacked loader which
117+
# won't rely on a now-closed file object.
118+
module.__loader__ = _bootstrap.SourceFileLoader(name, pathname)
119+
return module
115120

116121

117122
class _LoadCompiledCompatibility(_HackedGetData,
@@ -125,7 +130,12 @@ def load_compiled(name, pathname, file=None):
125130
'importlib.machinery.SourcelessFileLoader(name, pathname).'
126131
'load_module() instead ')
127132
warnings.warn(msg, DeprecationWarning, 2)
128-
return _LoadCompiledCompatibility(name, pathname, file).load_module(name)
133+
_LoadCompiledCompatibility(name, pathname, file).load_module(name)
134+
module = sys.modules[name]
135+
# To allow reloading to potentially work, use a non-hacked loader which
136+
# won't rely on a now-closed file object.
137+
module.__loader__ = _bootstrap.SourcelessFileLoader(name, pathname)
138+
return module
129139

130140

131141
def load_package(name, path):

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ Core and Builtins
5252
Library
5353
-------
5454

55+
- Issue #17358: Modules loaded by imp.load_source() and load_compiled() (and by
56+
extention load_module()) now have a better chance of working when reloaded.
57+
5558
- Issue #17804: New function ``struct.iter_unpack`` allows for streaming
5659
struct unpacking.
5760

0 commit comments

Comments
 (0)