From 4b99d39bc45be50af8a4e3bdd77816597a5bf0f7 Mon Sep 17 00:00:00 2001 From: Daniel Li Date: Wed, 29 Jan 2025 10:56:51 -0500 Subject: [PATCH] gh-120170: Exclude __mp_main__ in C version of whichmodule() Importing multiprocessing adds an alias to __main__ named __mp_main__. In #23403, the Python version of whichmodule() was fixed to exclude __mp_main__. Apply the same fix to the C version of the function. --- .../Library/2024-06-06-17-49-07.gh-issue-120170.DUxhmT.rst | 3 +++ Modules/_pickle.c | 4 ++++ 2 files changed, 7 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2024-06-06-17-49-07.gh-issue-120170.DUxhmT.rst diff --git a/Misc/NEWS.d/next/Library/2024-06-06-17-49-07.gh-issue-120170.DUxhmT.rst b/Misc/NEWS.d/next/Library/2024-06-06-17-49-07.gh-issue-120170.DUxhmT.rst new file mode 100644 index 00000000000000..ce7d874aa20375 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-06-06-17-49-07.gh-issue-120170.DUxhmT.rst @@ -0,0 +1,3 @@ +Fix an issue in the :mod:`!_pickle` extension module in which importing +:mod:`multiprocessing` could change how pickle identifies which module an +object belongs to, potentially breaking the unpickling of those objects. diff --git a/Modules/_pickle.c b/Modules/_pickle.c index 754a326822e0f0..93f1b53bb25c8c 100644 --- a/Modules/_pickle.c +++ b/Modules/_pickle.c @@ -1888,6 +1888,10 @@ _checkmodule(PyObject *module_name, PyObject *module, _PyUnicode_EqualToASCIIString(module_name, "__main__")) { return -1; } + if (PyUnicode_Check(module_name) && + _PyUnicode_EqualToASCIIString(module_name, "__mp_main__")) { + return -1; + } PyObject *candidate = get_deep_attribute(module, dotted_path, NULL); if (candidate == NULL) {