From 88969facf2727c362569b33bcd43a8db063d1cf9 Mon Sep 17 00:00:00 2001 From: "Renato L. de F. Cunha" Date: Thu, 19 Nov 2020 10:47:11 -0300 Subject: [PATCH 1/2] bpo-42406: Fix whichmodule() with multiprocessing Signed-off-by: Renato L. de F. Cunha --- Lib/pickle.py | 3 ++- .../next/Library/2020-11-19-10-44-41.bpo-42406.r9rNCj.rst | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2020-11-19-10-44-41.bpo-42406.r9rNCj.rst diff --git a/Lib/pickle.py b/Lib/pickle.py index cbac5f168b45eb..4393f818e85e49 100644 --- a/Lib/pickle.py +++ b/Lib/pickle.py @@ -340,7 +340,8 @@ def whichmodule(obj, name): # 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 sys.modules.copy().items(): - if module_name == '__main__' or module is None: + if module_name == '__main__' or module_name == '__mp_main__' \ + or module is None: continue try: if _getattribute(module, name)[0] is obj: diff --git a/Misc/NEWS.d/next/Library/2020-11-19-10-44-41.bpo-42406.r9rNCj.rst b/Misc/NEWS.d/next/Library/2020-11-19-10-44-41.bpo-42406.r9rNCj.rst new file mode 100644 index 00000000000000..c157df138a5ea0 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-11-19-10-44-41.bpo-42406.r9rNCj.rst @@ -0,0 +1,3 @@ +We fixed an issue in `pickle.whichmodule` in which importing +`multiprocessing` could change the how pickle identifies which module an +object belongs to, potentially breaking the unpickling of those objects. From 4f5ee8b1aab460351733723e1297508726ddc871 Mon Sep 17 00:00:00 2001 From: "Gregory P. Smith" Date: Sat, 28 Nov 2020 14:23:21 -0800 Subject: [PATCH 2/2] Use `()`s instead of `\` and mention bpo-42406. --- Lib/pickle.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Lib/pickle.py b/Lib/pickle.py index 4393f818e85e49..e63a8b6e4dbb70 100644 --- a/Lib/pickle.py +++ b/Lib/pickle.py @@ -340,8 +340,9 @@ def whichmodule(obj, name): # 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 sys.modules.copy().items(): - if module_name == '__main__' or module_name == '__mp_main__' \ - or module is None: + if (module_name == '__main__' + or module_name == '__mp_main__' # bpo-42406 + or module is None): continue try: if _getattribute(module, name)[0] is obj: