Commit a74f8ef4 authored by Éric Araujo's avatar Éric Araujo

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.
parent c4d7d8c4
...@@ -483,7 +483,7 @@ def getmodule(object, _filename=None): ...@@ -483,7 +483,7 @@ def getmodule(object, _filename=None):
return sys.modules.get(modulesbyfile[file]) return sys.modules.get(modulesbyfile[file])
# Update the filename to module name cache and check yet again # Update the filename to module name cache and check yet again
# Copy sys.modules in order to cope with changes while iterating # Copy sys.modules in order to cope with changes while iterating
for modname, module in sys.modules.items(): for modname, module in list(sys.modules.items()):
if ismodule(module) and hasattr(module, '__file__'): if ismodule(module) and hasattr(module, '__file__'):
f = module.__file__ f = module.__file__
if f == _filesbymodname.get(modname, None): if f == _filesbymodname.get(modname, None):
......
...@@ -905,6 +905,7 @@ Christian Tismer ...@@ -905,6 +905,7 @@ Christian Tismer
Frank J. Tobin Frank J. Tobin
R Lindsay Todd R Lindsay Todd
Bennett Todd Bennett Todd
Erik Tollerud
Matias Torchinsky Matias Torchinsky
Sandro Tosi Sandro Tosi
Richard Townsend Richard Townsend
......
...@@ -87,6 +87,9 @@ Core and Builtins ...@@ -87,6 +87,9 @@ Core and Builtins
Library Library
------- -------
- Issue #13487: Make inspect.getmodule robust against changes done to
sys.modules while it is iterating over it.
- Issue #12618: Fix a bug that prevented py_compile from creating byte - Issue #12618: Fix a bug that prevented py_compile from creating byte
compiled files in the current directory. Initial patch by Sjoerd de Vries. compiled files in the current directory. Initial patch by Sjoerd de Vries.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment