[Python-checkins] cpython (3.4): Issue #21905: Avoid RuntimeError in pickle.whichmodule() when sys.modules is

antoine.pitrou python-checkins at python.org
Sat Oct 4 22:17:47 CEST 2014


https://hg.python.org/cpython/rev/86ba3bdfac15
changeset:   92809:86ba3bdfac15
branch:      3.4
parent:      92799:cba365e2117f
user:        Antoine Pitrou <solipsis at pitrou.net>
date:        Sat Oct 04 22:15:27 2014 +0200
summary:
  Issue #21905: Avoid RuntimeError in pickle.whichmodule() when sys.modules is mutated while iterating.

Patch by Olivier Grisel.

files:
  Lib/pickle.py |  4 +++-
  Misc/ACKS     |  1 +
  Misc/NEWS     |  3 +++
  3 files changed, 7 insertions(+), 1 deletions(-)


diff --git a/Lib/pickle.py b/Lib/pickle.py
--- a/Lib/pickle.py
+++ b/Lib/pickle.py
@@ -280,7 +280,9 @@
     module_name = getattr(obj, '__module__', None)
     if module_name is not None:
         return module_name
-    for module_name, module in sys.modules.items():
+    # Protect the iteration by using a list copy of sys.modules against dynamic
+    # modules that trigger imports of other modules upon calls to getattr.
+    for module_name, module in list(sys.modules.items()):
         if module_name == '__main__' or module is None:
             continue
         try:
diff --git a/Misc/ACKS b/Misc/ACKS
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -494,6 +494,7 @@
 Grant Griffin
 Andrea Griffini
 Duncan Grisby
+Olivier Grisel
 Fabian Groffen
 Eric Groo
 Dag Gruneau
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -22,6 +22,9 @@
 Library
 -------
 
+- Issue #21905: Avoid RuntimeError in pickle.whichmodule() when sys.modules
+  is mutated while iterating.  Patch by Olivier Grisel.
+
 - Issue #22219: The zipfile module CLI now adds entries for directories
   (including empty directories) in ZIP file.
 

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


More information about the Python-checkins mailing list