[Python-checkins] cpython (3.2): Fix inspect.getmodule to use a copy of sys.modules for iteration (#13487).

eric.araujo python-checkins at python.org
Tue Nov 29 17:15:11 CET 2011


http://hg.python.org/cpython/rev/2ef359d7a2e9
changeset:   73789:2ef359d7a2e9
branch:      3.2
user:        Éric Araujo <merwok at netwok.org>
date:        Tue Nov 29 16:58:53 2011 +0100
summary:
  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.

files:
  Lib/inspect.py |  2 +-
  Misc/ACKS      |  1 +
  Misc/NEWS      |  3 +++
  3 files changed, 5 insertions(+), 1 deletions(-)


diff --git a/Lib/inspect.py b/Lib/inspect.py
--- a/Lib/inspect.py
+++ b/Lib/inspect.py
@@ -483,7 +483,7 @@
         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):
diff --git a/Misc/ACKS b/Misc/ACKS
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -905,6 +905,7 @@
 Frank J. Tobin
 R Lindsay Todd
 Bennett Todd
+Erik Tollerud
 Matias Torchinsky
 Sandro Tosi
 Richard Townsend
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -87,6 +87,9 @@
 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.
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list