Python: trace all previous call stack of a library for debug purpose

36 views Asked by At

Is there any elegant and easy way to trace all the call stacks in Python of a library for debugging or code understanding purposes?

Suppose there is a very big huge library that I'm importing and need to understand the code of it.

from xyz import tokenizer

start_call_stack_trace()
tokenizer.tokenize('Hello World', some_parameter='some_value')
stop_call_stack_trace()

Now I want to get a list of all the functions with parameter values & return values (if possible!) that were called in a chain while invoking tokenizer.tokenize().

I was wondering if there is any elegant way to get it and understand the code of XYZ library quickly?

1

There are 1 answers

0
Pravash Panigrahi On

I believe, you can use Python's in-built module - trace, It helps you trace function calls, exceptions, returns, line-by-line execution.

For Example -

import trace

tracer_call = trace.Trace(trace=1, count=1)
tracer_call.runfunc(tokenizer.tokenize('Hello World', some_parameter='some_value'))

output_res = tracer_call.results()
output_res.write_results()

You can refer to this