summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2014-10-04 20:15:27 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2014-10-04 20:15:27 (GMT)
commite1618491adc5c0ef214c25d4e3b882e8586a2fee (patch)
tree0d91357929681b4329601ede97fab846a182abac
parentfb8eaae6eb3996f9b02db81662be4e92b690d344 (diff)
downloadcpython-e1618491adc5c0ef214c25d4e3b882e8586a2fee.zip
cpython-e1618491adc5c0ef214c25d4e3b882e8586a2fee.tar.gz
cpython-e1618491adc5c0ef214c25d4e3b882e8586a2fee.tar.bz2
Issue #21905: Avoid RuntimeError in pickle.whichmodule() when sys.modules is mutated while iterating.
Patch by Olivier Grisel.
-rw-r--r--Lib/pickle.py4
-rw-r--r--Misc/ACKS1
-rw-r--r--Misc/NEWS3
3 files changed, 7 insertions, 1 deletions
diff --git a/Lib/pickle.py b/Lib/pickle.py
index 3fc2596..663773f 100644
--- a/Lib/pickle.py
+++ b/Lib/pickle.py
@@ -280,7 +280,9 @@ def whichmodule(obj, name, allow_qualname=False):
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
index c91d643..3095047 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -494,6 +494,7 @@ Eddy De Greef
Grant Griffin
Andrea Griffini
Duncan Grisby
+Olivier Grisel
Fabian Groffen
Eric Groo
Dag Gruneau
diff --git a/Misc/NEWS b/Misc/NEWS
index 9915e4e..6112a00 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -22,6 +22,9 @@ Core and Builtins
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.