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

Skip to content

Commit a74f8ef

Browse files
committed
Fix inspect.getmodule to use a copy of sys.modules for iteration (#13487).
This fixes a regression compared to 2.x, where sys.modules.items() returns a copy, as indicated by a comment in the source. Diagnosis and patch by Erik Tollerud.
1 parent c4d7d8c commit a74f8ef

3 files changed

Lines changed: 5 additions & 1 deletion

File tree

Lib/inspect.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ def getmodule(object, _filename=None):
483483
return sys.modules.get(modulesbyfile[file])
484484
# Update the filename to module name cache and check yet again
485485
# Copy sys.modules in order to cope with changes while iterating
486-
for modname, module in sys.modules.items():
486+
for modname, module in list(sys.modules.items()):
487487
if ismodule(module) and hasattr(module, '__file__'):
488488
f = module.__file__
489489
if f == _filesbymodname.get(modname, None):

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -905,6 +905,7 @@ Christian Tismer
905905
Frank J. Tobin
906906
R Lindsay Todd
907907
Bennett Todd
908+
Erik Tollerud
908909
Matias Torchinsky
909910
Sandro Tosi
910911
Richard Townsend

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ Core and Builtins
8787
Library
8888
-------
8989

90+
- Issue #13487: Make inspect.getmodule robust against changes done to
91+
sys.modules while it is iterating over it.
92+
9093
- Issue #12618: Fix a bug that prevented py_compile from creating byte
9194
compiled files in the current directory. Initial patch by Sjoerd de Vries.
9295

0 commit comments

Comments
 (0)