App: Handle uncleared Python exceptions during object restoration

When a Python-based object throws an exception during the restoration
process (e.g. inside `onDocumentRestored`), the error is caught by a
generic `catch(...)` block in `Document::afterRestore`.

Previously, this block swallowed the C++ exception wrapper but failed to
clear the underlying Python error state. Leaving the interpreter in this
"dirty" state caused a segmentation fault or `SystemError` later in the
loading process when C++ attempted to interact with the Python API
(specifically in `App::Application::setActiveDocument`).

This commit adds a check for `PyErr_Occurred()` inside the catch block.
If an error is detected, the traceback is printed to the console for
debugging, and `PyErr_Clear()` is called to reset the interpreter state,
allowing the application to continue loading the document without
crashing.
This commit is contained in:
Furgo
2025-11-22 19:11:12 +01:00
parent 46234ec246
commit b72f635bb2

View File

@@ -2092,6 +2092,16 @@ bool Document::afterRestore(const std::vector<DocumentObject*>& objArray, bool c
FC_ERR("Failed to restore " << obj->getFullName() << ": " << e.what());
}
catch (...) {
// If a Python exception occurred, it must be cleared immediately.
// Otherwise, the interpreter remains in a dirty state, causing
// Segfaults later when FreeCAD interacts with Python.
if (PyErr_Occurred()) {
Base::Console().error("Python error during object restore:\n");
PyErr_Print(); // Print the traceback to stderr/Console
PyErr_Clear(); // Reset the interpreter state
}
d->addRecomputeLog("Unknown exception on restore", obj);
FC_ERR("Failed to restore " << obj->getFullName() << ": " << "unknown exception");
}