I write C++ applications which embed python
interpreter as script engine. And I would like to
perform traceback analysis during error handling
(imagine for instance that I would like to include
python traceback info inside some exception which
will be handled outside library I write, maybe by
sending error description to log server via network)
What I found? PyErr_Fetch gets traceback object and
gives it to me. There are methods which print
traceback to standard error. But if I want to do
something different ... there is no way
to analyze traceback without utilizing some dirty
tricks. Two main problems:
- struct tracebackobject is not present in python
headers, it is only in traceback.c
- it is of course not documented at all.
I finally managed to do what I wanted by copying
tracebackobject definition from traceback.c and by
copying tb_printinternal and suiting it to my needs.
But what will happen if you change this structure
some time in the future?
What exactly do I request? Choose one of the two:
1) Either publish struct tracebackobject inside some
Python headers (traceback.h?), mention in PyErr_Fetch
documentation that the last parameter is of this type
and describe its structure somewhere in python-embed
2) Or keep it secret but add some API which would
allow to navigate traceback without presenting its
internals, say the function like
PyTrace_AnalyzeTraceBack which - given traceback
object - would return file, line, name and next
traceback (or the flag telling that there is no more).
|