we have a python script that uses a c library to call some low level functions. Because of code structure reasons we want to store the stack trace/call stack before each c call.
To do this we use traceback.extract_stack()
to extract the call stack of python.
When an exception occurs later on, we use traceback.format_list(abc)
on each element to format and print the stack trace.
The problem is that the function extract_stack
is too slow. I slows down our code from 1.7 seconds to 11 seconds.
Is there any function to store the stack trace in order to be able to use/print it later? The stacktrace getter must be very fast. The format function can be slow, this is no problem.
Example:
Stack Trace:
- LXScript: '_LXS:TOOL:RUNLX'
File "_LXS:TOOL:RUNLX", line 13, in <module>
File "lxs", line 1, in <module>
- UNIFACE ACTIVATE: 'ACTQREC_SVC' 'EXECLXSRP'
- LXScript: '_DATATRT:ACTQREC:EXECUTE@Main:run'
File "_LXS:TOOL:RUNLX", line 13, in <module>
File "lxs", line 1, in <module>
File "<string>", line 63, in run
File "<string>", line 97, in __doAll
File "<string>", line 127, in __do
File "_DATATRT:ACTQREC:EXECUTE", line 7, in do
- UNIFACE ACTIVATE: 'ACTQ_CSVC' 'EXECBYREC'
- LXScript: 'TOOL:ACTQ:SYNLAB_DATA@Main:runOnBeforeExec'
File "TOOL:ACTQ:SYNLAB_DATA", line 1, in <module>
ImportError: No module named 'localls'
I resolved the problem!!!
We can use
stt_obj = sys._getframe().f_back
to get the "<frame>
?" And then we can useestt_obj = traceback.extract_stack(f=stt_obj)
andtraceback.format_list(estt_obj)
to get the stack trace as string list.