traceback

How to get a complete exception stack trace in Python

How to get a complete exception stack trace in Python Question: The following snippet: import traceback def a(): b() def b(): try: c() except: traceback.print_exc() def c(): assert False a() Produces this output: Traceback (most recent call last): File “test.py”, line 8, in b c() File “test.py”, line 13, in c assert False AssertionError What …

Total answers: 4

Determine function name from within that function (without using traceback)

Determine function name from within that function (without using traceback) Question: In Python, without using the traceback module, is there a way to determine a function’s name from within that function? Say I have a module foo with a function bar. When executing foo.bar(), is there a way for bar to know bar‘s name? Or …

Total answers: 27

How do I use Django's logger to log a traceback when I tell it to?

How do I use Django's logger to log a traceback when I tell it to? Question: try: print blah except KeyError: traceback.print_exc() I used to debug like this. I’d print to the console. Now, I want to log everything instead of print, since Apache doesn’t allow printing. So, how do I log this entire traceback? …

Total answers: 2

Exception traceback is hidden if not re-raised immediately

Exception traceback is hidden if not re-raised immediately Question: I’ve got a piece of code similar to this: import sys def func1(): func2() def func2(): raise Exception(‘test error’) def main(): err = None try: func1() except: err = sys.exc_info()[1] pass # some extra processing, involving checking err details (if err is not None) # need …

Total answers: 6

How to catch and print the full exception traceback without halting/exiting the program?

How to catch and print the full exception traceback without halting/exiting the program? Question: I want to catch and log exceptions without exiting, e.g., try: do_stuff() except Exception as err: print(Exception, err) # I want to print the entire traceback here, # not just the exception name and details I want to print the exact …

Total answers: 18

How can I modify a Python traceback object when raising an exception?

How can I modify a Python traceback object when raising an exception? Question: I’m working on a Python library used by third-party developers to write extensions for our core application. I’d like to know if it’s possible to modify the traceback when raising exceptions, so the last stack frame is the call to the library …

Total answers: 7

When I catch an exception, how do I get the type, file, and line number?

When I catch an exception, how do I get the type, file, and line number? Question: Catching an exception that would print like this: Traceback (most recent call last): File “c:/tmp.py”, line 1, in <module> 4 / 0 ZeroDivisionError: integer division or modulo by zero I want to format it into: ZeroDivisonError, tmp.py, 1 Asked …

Total answers: 7

How to exit from Python without traceback?

How to exit from Python without traceback? Question: I would like to know how to I exit from Python without having an traceback dump on the output. I still want want to be able to return an error code but I do not want to display the traceback log. I want to be able to …

Total answers: 10

Showing the stack trace from a running Python application

Showing the stack trace from a running Python application Question: I have this Python application that gets stuck from time to time and I can’t find out where. Is there any way to signal Python interpreter to show you the exact code that’s running? Some kind of on-the-fly stacktrace? Related questions: Print current call stack …

Total answers: 29