-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
"SWIG director method error" from HostFn exception inside Ensemble. #1067
Comments
Update: It's trivially possible to catch runtime errors and rethrow them try:
...
except Exception as err:
traceback.print_exception(*sys.exc_info())
raise However, this is not just awkward, it also does not resolve errors caused by syntax issues within the host function. In this case the below code was at fault (I'm not familiar with when/where Python chooses to 'compile' syntax, unclear if it's a specific to format strings being done late or the whole function). # Note variable 'bar' is not defined/in-scope.
foo = f"bar:{bar:0.3f}" |
A few more iterations of the 10 minute pyflamegpu build process, I've now managed to get the error message to print. %feature("director:except") {
if ($error != NULL) {
PyObject *type, *value, *traceback;
PyErr_Fetch(&type, &value, &traceback);
// Message
PyObject *value_str = PyObject_Str(value);
const char *pStrErrorMessage = PyUnicode_AsUTF8(value_str);
printf("Exception Message: %s\n", pStrErrorMessage);
// @todo type to string
// @todo traceback to string
// Propagate exception
// @todo store the data in here?
throw Swig::DirectorMethodException();
}
} |
I've now got it as far as printing the message, exception type, and host function class name. // swig director exceptions (handle python callback exceptions as C++ exceptions not Runtime errors)
%feature("director:except") {
if ($error != NULL) {
PyObject *type, *value, *traceback;
PyErr_Fetch(&type, &value, &traceback);
PyErr_NormalizeException(&type, &value, &traceback);
PyObject *hostfn = swig_get_self();
// Message
PyObject *value_str = PyObject_Str(value);
const char *pStrErrorMessage = PyUnicode_AsUTF8(value_str);
printf("Exception Message: %s\n", pStrErrorMessage);
// Type
PyObject* type_obj_name = PyObject_GetAttrString(type, "__name__");
PyObject *type_str = PyObject_Str(type_obj_name);
const char *pTypeStr = PyUnicode_AsUTF8(type_str);
printf("Type: %s\n", pTypeStr);
// Director Obj Type
PyObject* hostfn_type = PyObject_Type(hostfn);
PyObject* hostfn_type_name = PyObject_GetAttrString(hostfn_type, "__name__");
PyObject *hostfn_str = PyObject_Str(hostfn_type_name);
const char *pHostFnTypeStr = PyUnicode_AsUTF8(hostfn_str);
printf("HostFnType: %s\n", pHostFnTypeStr);
// @todo traceback to string
PyObject *trace_str = PyObject_Str(traceback);
const char *pTraceStr = PyUnicode_AsUTF8(trace_str);
printf("Trace: %s\n", pTraceStr);
// Cleanup
Py_DECREF(trace_str);
Py_DECREF(type_obj_name);
Py_DECREF(hostfn_type_name);
Py_DECREF(hostfn_type);
Py_DECREF(hostfn_str);
Py_DECREF(type_str);
Py_DECREF(value_str);
Py_DECREF(type);
Py_DECREF(value);
Py_DECREF(traceback);
// Propagate exception
// @todo store the data in here?
throw Swig::DirectorMethodException();
}
} I've now tested this traceback implementation, which works after including Python's |
Tested on Windows with Python 3.9 Closes #1067
Tested on Windows with Python 3.9 Closes #1067
* Python: Retain hostfn exception msg when thrown during ensemble. Tested on Windows with Python 3.9 Closes #1067 * Python 3.11+ fix
If an exception is thrown from a Python host function during a regular
CUDASimulation
's execution, it is propagated back to python and a full error message and stack trace is received.If the same occurs during a
CUDAEnsemble
, it is caught and handled by C/C++ so we only get this detail.According to the SWIG documentation, this is currently a limitation:
From digging around in the generated python, I found that the error handler which generates the C++ exception is passed a
PyObject*
, which presumably represents the exception.I thought using
PyErr_Fetch()
,PyErr_GetExcInfo()
orPyException_GetTraceback()
/PyException_GetContext()
/PyException_GetCause()
I might be able to get the detail (https://docs.python.org/3/c-api/exceptions.html). But no luck. Fetch returns allnull
strings, and the rest cause the program to crash with no output (possible access violation or something if they're returning null ptrs).Needs more investigation.
The text was updated successfully, but these errors were encountered: