0

I have added an assert(0) in a function to understand the sequence of function calls to this function.

Ex:

def my_func():
    ...lines of code...
    assert(0)
    ...more lines of code...

The logs show only this:

Traceback (most recent call last):
  File "/var/www/folder/file.py", line 273, in my_func
    assert(0)
AssertionError

I want to see the complete call trace - Example: first_func -> second_func -> my_func

I tried traceback library but it is showing me the same stacktrace. Please let me know what I am missing here.

Jamal
  • 287
  • 1
  • 4
  • 15
  • possible duplicate of [Print the full traceback in python (without halting the program)](http://stackoverflow.com/questions/3702675/print-the-full-traceback-in-python-without-halting-the-program) – Nathan Aug 08 '14 at 21:37
  • while @Alexey's answer is helpful, if you want the **full** traceback (i.e., what you normally see on the command line), you can use the one-liner from [my answer](http://stackoverflow.com/a/33723119/1467306) to a similar question. – Edward Newell Nov 15 '15 at 18:43

1 Answers1

3

Use traceback module for this. I.e.

>>> import traceback
>>> def my_func():
...     my_other_func()
... 
>>> def my_other_func():
...     my_third()
... 
>>> def my_third():
...     print "Stack"
...     traceback.print_stack()
...     print "Extracted"
...     print repr(traceback.extract_stack())
... 
>>> 
>>> my_func()
Stack
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in my_func
  File "<stdin>", line 2, in my_other_func
  File "<stdin>", line 3, in my_third
Extracted
[('<stdin>', 1, '<module>', None), 
 ('<stdin>', 2, 'my_func', None), 
 ('<stdin>', 2, 'my_other_func', None), 
 ('<stdin>', 5, 'my_third', None)]
>>> 
Alexey Kachayev
  • 6,106
  • 27
  • 24