Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e161849

Browse files
committed
Issue #21905: Avoid RuntimeError in pickle.whichmodule() when sys.modules is mutated while iterating.
Patch by Olivier Grisel.
1 parent fb8eaae commit e161849

3 files changed

Lines changed: 7 additions & 1 deletion

File tree

Lib/pickle.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,9 @@ def whichmodule(obj, name, allow_qualname=False):
280280
module_name = getattr(obj, '__module__', None)
281281
if module_name is not None:
282282
return module_name
283-
for module_name, module in sys.modules.items():
283+
# Protect the iteration by using a list copy of sys.modules against dynamic
284+
# modules that trigger imports of other modules upon calls to getattr.
285+
for module_name, module in list(sys.modules.items()):
284286
if module_name == '__main__' or module is None:
285287
continue
286288
try:

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,7 @@ Eddy De Greef
494494
Grant Griffin
495495
Andrea Griffini
496496
Duncan Grisby
497+
Olivier Grisel
497498
Fabian Groffen
498499
Eric Groo
499500
Dag Gruneau

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ Core and Builtins
2222
Library
2323
-------
2424

25+
- Issue #21905: Avoid RuntimeError in pickle.whichmodule() when sys.modules
26+
is mutated while iterating. Patch by Olivier Grisel.
27+
2528
- Issue #22219: The zipfile module CLI now adds entries for directories
2629
(including empty directories) in ZIP file.
2730

0 commit comments

Comments
 (0)