]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-38631: Avoid Py_FatalError() in PyModule_Create2() (GH-18212)
authorVictor Stinner <vstinner@python.org>
Mon, 27 Jan 2020 21:37:05 +0000 (22:37 +0100)
committerGitHub <noreply@github.com>
Mon, 27 Jan 2020 21:37:05 +0000 (22:37 +0100)
If PyModule_Create2() is called when the Python import machinery is
not initialized, it now raises a SystemError and returns NULL,
instead of calling Py_FatalError() which aborts the process.

The caller must be prepared to handle NULL anyway.

Objects/moduleobject.c

index 03c7381311aa14bbc253ac0e92f96d454f0d27c5..912c2584015861c94db12863853849d281e65541 100644 (file)
@@ -173,8 +173,11 @@ _add_methods_to_object(PyObject *module, PyObject *name, PyMethodDef *functions)
 PyObject *
 PyModule_Create2(struct PyModuleDef* module, int module_api_version)
 {
-    if (!_PyImport_IsInitialized(_PyInterpreterState_Get()))
-        Py_FatalError("Python import machinery not initialized");
+    if (!_PyImport_IsInitialized(_PyInterpreterState_Get())) {
+        PyErr_SetString(PyExc_SystemError,
+                        "Python import machinery not initialized");
+        return NULL;
+    }
     return _PyModule_CreateInitialized(module, module_api_version);
 }