I’m working on a tool that introspects running python code using a sys.setprofile
trace function. When an exception is raised by an introspected function, I would like to obtain the exception object somehow so I can introspect it, but I can’t work out how to do this.
The documentation says:
'return'
A function (or other code block) is about to return. The local trace function is called; arg is the value that will be returned, or
None
if the event is caused by an exception being raised. The trace function’s return value is ignored.
When handling the return
frame, is there a way to distinguish between the case when an exception is raised and the case when the function just returned None
?
Do I have to switch to the more complicated sys.settrace
to deal with this case? I’m only interested (right now) in exceptions that bubble out of the function I’m introspecting causing an early return, not any caught locally and handled.
I have tried using sys.exception()
, but this just returns None
.