[Python-checkins] bpo-24048: Save the live exception during import.c's remove_module() (GH-13005)

Nick Coghlan webhook-mailer at python.org
Wed May 8 12:31:29 EDT 2019


https://github.com/python/cpython/commit/94a64e9cd411a87514b68082c1c437eb3b49dfb9
commit: 94a64e9cd411a87514b68082c1c437eb3b49dfb9
branch: master
author: Zackery Spytz <zspytz at gmail.com>
committer: Nick Coghlan <ncoghlan at gmail.com>
date: 2019-05-08T12:31:23-04:00
summary:

bpo-24048: Save the live exception during import.c's remove_module() (GH-13005)

Save the live exception during the course of remove_module().

files:
A Misc/NEWS.d/next/Core and Builtins/2019-04-29-03-27-22.bpo-24048.vXxUDQ.rst
M Python/import.c

diff --git a/Misc/NEWS.d/next/Core and Builtins/2019-04-29-03-27-22.bpo-24048.vXxUDQ.rst b/Misc/NEWS.d/next/Core and Builtins/2019-04-29-03-27-22.bpo-24048.vXxUDQ.rst
new file mode 100644
index 000000000000..27c86a47f4b9
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2019-04-29-03-27-22.bpo-24048.vXxUDQ.rst	
@@ -0,0 +1 @@
+Save the live exception during import.c's ``remove_module()``.
diff --git a/Python/import.c b/Python/import.c
index b03bc98773ae..9290f39c0ae2 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -837,14 +837,18 @@ PyImport_AddModule(const char *name)
 static void
 remove_module(PyObject *name)
 {
+    PyObject *type, *value, *traceback;
+    PyErr_Fetch(&type, &value, &traceback);
     PyObject *modules = PyImport_GetModuleDict();
+    if (!PyMapping_HasKey(modules, name)) {
+        goto out;
+    }
     if (PyMapping_DelItem(modules, name) < 0) {
-        if (!PyMapping_HasKey(modules, name)) {
-            return;
-        }
         Py_FatalError("import:  deleting existing key in "
                       "sys.modules failed");
     }
+out:
+    PyErr_Restore(type, value, traceback);
 }
 
 



More information about the Python-checkins mailing list