]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Fix inspect.getmodule to use a copy of sys.modules for iteration (#13487).
authorÉric Araujo <merwok@netwok.org>
Tue, 29 Nov 2011 15:58:53 +0000 (16:58 +0100)
committerÉric Araujo <merwok@netwok.org>
Tue, 29 Nov 2011 15:58:53 +0000 (16:58 +0100)
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.

Lib/inspect.py
Misc/ACKS
Misc/NEWS

index bb46ea6f85c08170f8bbe6d75391c32222e3635f..4e17f1b591cd8035ec378c5661b237c8c5b3c069 100644 (file)
@@ -483,7 +483,7 @@ def getmodule(object, _filename=None):
         return sys.modules.get(modulesbyfile[file])
     # Update the filename to module name cache and check yet again
     # 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__'):
             f = module.__file__
             if f == _filesbymodname.get(modname, None):
index 5d754041492b9d771b92a75ae13f0a000ac288ad..4effde36afe68964fb6c9bc5d2b9675a94c55d96 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -905,6 +905,7 @@ Christian Tismer
 Frank J. Tobin
 R Lindsay Todd
 Bennett Todd
+Erik Tollerud
 Matias Torchinsky
 Sandro Tosi
 Richard Townsend
index f79e57ac189ab19829b4034ce1f1e2d8a6cf3a4e..21728530bbc69e29ea1c5a871aa18d48c944f716 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -87,6 +87,9 @@ Core and Builtins
 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
   compiled files in the current directory.  Initial patch by Sjoerd de Vries.