Skip to content

Commit

Permalink
Improve error reporting during python extension module initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
speth committed Sep 11, 2022
1 parent 61f2688 commit 330f08b
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/extensions/PythonExtensionManager.cpp
Expand Up @@ -75,28 +75,44 @@ PythonExtensionManager::PythonExtensionManager()
// to instantiate ExtensibleSomething objects.
PyModuleDef* modDef = (PyModuleDef*) PyInit_pythonExtensions();
if (!modDef->m_slots || !PyModuleDef_Init(modDef)) {
if (PyErr_Occurred()) {
PyErr_PrintEx(0);
}
throw CanteraError("PythonExtensionManager::PythonExtensionManager",
"Failed to import 'pythonExtensions' module");
}

// Following example creation of minimal ModuleSpec from Python's import.c
PyObject *attrs = Py_BuildValue("{ss}", "name", "pythonExtensions");
if (attrs == NULL) {
if (attrs == nullptr) {
if (PyErr_Occurred()) {
PyErr_PrintEx(0);
}
throw CanteraError("PythonExtensionManager::PythonExtensionManager",
"Py_BuildValue failed");
}
PyObject *spec = _PyNamespace_New(attrs);
Py_DECREF(attrs);
if (spec == NULL) {
if (spec == nullptr) {
if (PyErr_Occurred()) {
PyErr_PrintEx(0);
}
throw CanteraError("PythonExtensionManager::PythonExtensionManager",
"_PyNamespace_New failed");
}
PyObject* pyModule = PyModule_FromDefAndSpec(modDef, spec);
if (!pyModule) {
if (pyModule == nullptr) {
if (PyErr_Occurred()) {
PyErr_PrintEx(0);
}
CanteraError("PythonExtensionManager::PythonExtensionManager",
"PyModule_FromDefAndSpec failed");
}
if (!PyModule_ExecDef(pyModule, modDef)) {
int code = PyModule_ExecDef(pyModule, modDef);
if (code) {
if (PyErr_Occurred()) {
PyErr_PrintEx(0);
}
CanteraError("PythonExtensionManager::PythonExtensionManager",
"PyModule_ExecDef failed");
}
Expand Down

0 comments on commit 330f08b

Please sign in to comment.