[Python-checkins] bpo-13487: Use sys.modules.copy() in inspect.getmodule() for thread safety. (GH-18786)

Miss Islington (bot) webhook-mailer at python.org
Wed Mar 4 20:03:36 EST 2020


https://github.com/python/cpython/commit/a12381233a243ba7d5151ebf060feb57dd540bef
commit: a12381233a243ba7d5151ebf060feb57dd540bef
branch: 3.7
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2020-03-04T17:03:29-08:00
summary:

bpo-13487: Use sys.modules.copy() in inspect.getmodule() for thread safety. (GH-18786)


`list(sys.modules.items())` was apparently not immune to "dictionary
changed size during iteration" errors.

Tested internally using an integration test that has run into this a couple of times in the past two years.  With this patch applied, the test is no longer flaky.
(cherry picked from commit 85cf1d514b84dc9a4bcb40e20a12e1d82ff19f20)

Co-authored-by: Gregory P. Smith <gps at google.com>

files:
A Misc/NEWS.d/next/Library/2020-03-04-16-10-59.bpo-13487.gqe4Fb.rst
M Lib/inspect.py

diff --git a/Lib/inspect.py b/Lib/inspect.py
index bbecd98f94abd..9848ca4c50775 100644
--- a/Lib/inspect.py
+++ b/Lib/inspect.py
@@ -729,7 +729,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 list(sys.modules.items()):
+    for modname, module in sys.modules.copy().items():
         if ismodule(module) and hasattr(module, '__file__'):
             f = module.__file__
             if f == _filesbymodname.get(modname, None):
diff --git a/Misc/NEWS.d/next/Library/2020-03-04-16-10-59.bpo-13487.gqe4Fb.rst b/Misc/NEWS.d/next/Library/2020-03-04-16-10-59.bpo-13487.gqe4Fb.rst
new file mode 100644
index 0000000000000..5a1f02a7bdf37
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-03-04-16-10-59.bpo-13487.gqe4Fb.rst
@@ -0,0 +1,3 @@
+Avoid a possible *"RuntimeError: dictionary changed size during iteration"*
+from :func:`inspect.getmodule` when it tried to loop through
+:attr:`sys.modules`.



More information about the Python-checkins mailing list