Skip to content

Commit b9bbb73

Browse files
committed
Engine: handle python exceptions, and print traceback
1 parent b6c1799 commit b9bbb73

File tree

1 file changed

+55
-1
lines changed

1 file changed

+55
-1
lines changed

Engine/AppManager.cpp

+55-1
Original file line numberDiff line numberDiff line change
@@ -3737,6 +3737,60 @@ NATRON_PYTHON_NAMESPACE::interpretPythonScript(const std::string& script,
37373737
Py_DECREF(v);
37383738
}
37393739

3740+
if (error) {
3741+
error->clear();
3742+
}
3743+
PyObject* ex = PyErr_Occurred();
3744+
if (ex) {
3745+
assert(v == NULL);
3746+
if (!error) {
3747+
PyErr_Clear();
3748+
} else {
3749+
PyObject *pyExcType;
3750+
PyObject *pyExcValue;
3751+
PyObject *pyExcTraceback;
3752+
PyErr_Fetch(&pyExcType, &pyExcValue, &pyExcTraceback); // also clears the error indicator
3753+
//PyErr_NormalizeException(&pyExcType, &pyExcValue, &pyExcTraceback);
3754+
3755+
PyObject* pyStr = PyObject_Str(pyExcValue);
3756+
if (pyStr) {
3757+
const char* str = PyString_AsString(pyStr);
3758+
if (error && str) {
3759+
*error += std::string("Python exception: ") + str + '\n';
3760+
}
3761+
Py_DECREF(pyStr);
3762+
3763+
// See if we can get a full traceback
3764+
PyObject* module_name = PyString_FromString("traceback");
3765+
PyObject* pyth_module = PyImport_Import(module_name);
3766+
Py_DECREF(module_name);
3767+
3768+
if (pyth_module != NULL) {
3769+
PyObject* pyth_func = PyObject_GetAttrString(pyth_module, "format_exception");
3770+
Py_DECREF(pyth_module);
3771+
if (pyth_func && PyCallable_Check(pyth_func)) {
3772+
PyObject *pyth_val = PyObject_CallFunctionObjArgs(pyth_func, pyExcType, pyExcValue, pyExcTraceback, NULL);
3773+
if (pyth_val) {
3774+
PyObject *emptyString = PyString_FromString("");
3775+
PyObject *strList = PyObject_CallMethod(emptyString, (char*)"join", (char*)"(O)", pyth_val);
3776+
Py_DECREF(emptyString);
3777+
Py_DECREF(pyth_val);
3778+
pyStr = PyObject_Str(strList);
3779+
Py_DECREF(strList);
3780+
if (pyStr) {
3781+
str = PyString_AsString(pyStr);
3782+
Py_DECREF(pyStr);
3783+
if (error && str) {
3784+
*error += std::string(str) + '\n';
3785+
}
3786+
}
3787+
}
3788+
}
3789+
}
3790+
}
3791+
}
3792+
}
3793+
37403794
if (error) {
37413795
*error = NATRON_PYTHON_NAMESPACE::getPythonStdErr();
37423796
}
@@ -3762,7 +3816,7 @@ NATRON_PYTHON_NAMESPACE::interpretPythonScript(const std::string& script,
37623816
return false;
37633817
}
37643818

3765-
return true;
3819+
return v != NULL;
37663820

37673821
} // NATRON_PYTHON_NAMESPACE::interpretPythonScript
37683822

0 commit comments

Comments
 (0)