File tree Expand file tree Collapse file tree 3 files changed +22
-1
lines changed Expand file tree Collapse file tree 3 files changed +22
-1
lines changed Original file line number Diff line number Diff line change @@ -268,7 +268,9 @@ def reload(module):
268
268
if parent_name and parent_name not in sys .modules :
269
269
msg = "parent {!r} not in sys.modules"
270
270
raise ImportError (msg .format (parent_name ), name = parent_name )
271
- return module .__loader__ .load_module (name )
271
+ module .__loader__ .load_module (name )
272
+ # The module may have replaced itself in sys.modules!
273
+ return sys .modules [module .__name__ ]
272
274
finally :
273
275
try :
274
276
del _RELOADING [name ]
Original file line number Diff line number Diff line change 5
5
import shutil
6
6
import sys
7
7
from test import support
8
+ from test .test_importlib import util
8
9
import unittest
9
10
import warnings
10
11
@@ -285,6 +286,22 @@ def cleanup():
285
286
with self .assertRaisesRegex (ImportError , 'html' ):
286
287
imp .reload (parser )
287
288
289
+ def test_module_replaced (self ):
290
+ # see #18698
291
+ def code ():
292
+ module = type (sys )('top_level' )
293
+ module .spam = 3
294
+ sys .modules ['top_level' ] = module
295
+ mock = util .mock_modules ('top_level' ,
296
+ module_code = {'top_level' : code })
297
+ with mock :
298
+ with util .import_state (meta_path = [mock ]):
299
+ module = importlib .import_module ('top_level' )
300
+ reloaded = imp .reload (module )
301
+ actual = sys .modules ['top_level' ]
302
+ self .assertEqual (actual .spam , 3 )
303
+ self .assertEqual (reloaded .spam , 3 )
304
+
288
305
289
306
class PEP3147Tests (unittest .TestCase ):
290
307
"""Tests of PEP 3147."""
Original file line number Diff line number Diff line change @@ -202,6 +202,8 @@ Library
202
202
- Issue #17269: Workaround for socket.getaddrinfo crash on MacOS X
203
203
with port None or "0" and flags AI_NUMERICSERV.
204
204
205
+ - Issue #18698: Ensure imp.reload() returns the module out of sys.modules.
206
+
205
207
- Issue #18080: When building a C extension module on OS X, if the compiler
206
208
is overriden with the CC environment variable, use the new compiler as
207
209
the default for linking if LDSHARED is not also overriden. This restores
You can’t perform that action at this time.
0 commit comments