On Fri, Dec 20 2019, Victor Stinner wrote:
If we add the following function, does it solve your use case?
void _PyErr_GetExcInfo(PyThreadState *tstate, PyObject **p_type, PyObject **p_value, PyObject **p_traceback)
Yes, it would.
In order to retrieve the exception from *any* PyThreadState, the caller has to use _PyErr_GetTopmostException which takes a PyThreadState as argument — though that function is private and therefore not documented or usable in an external module (in theory at least).
What if this function is exported as a private function? Usually, functions in Python header files are prefixed by PyAPI_FUNC(), but for an unknown reason, this one is not:
_PyErr_StackItem *_PyErr_GetTopmostException(PyThreadState *tstate);
Maybe it's because the author didn't want to expose the private _PyErr_StackItem structure?
This is my guess too. It'd make sense since this was struct was introduced around 3.7 and those values where just stored inside PyThreadState before. I guess nobody wanted to commit to an API.
I don't think exposing _PyErr_StackItem is worth it. It's just a placeholder for the usual exception triplet (tp, value, tb).
Should we make _PyErr_GetTopmostException public, or implement something different to retrieve the top most exception from a PyThreadState?
IMHO a _PyErr_GetExcInfo() function taking a tstate parameter is a better idea than exposing the private _PyErr_StackItem structure.
Private functions *can* be used for debuggers, but we don't provide any warranty that the function is not going to disappear. Such functions have been moved to the internal API for example. The internal C API *can* be used, but you have to opt-in so you know that you get unstable APIs :-)
Alright! I didn't know that you could use private functions. I'm totally in the debugger case so it's fine with me. :)
I'll come up with a patch that exposes _PyErr_GetExcInfo() as you suggested.