I have a traceback object that I want to show in the nice format I get when calling traceback.format_exc()
.
Is there a builtin function for this? Or a few lines of code?
What Is a Python Traceback? A traceback is a report containing the function calls made in your code at a specific point. Tracebacks are known by many names, including stack trace, stack traceback, backtrace, and maybe others. In Python, the term used is traceback.
There's no documented way to create traceback objects. None of the functions in the traceback module create them. You can of course access the type as types. TracebackType , but if you call its constructor you just get a TypeError: cannot create 'traceback' instances .
print_tb(tb, limit = None, file = None) : If limit is positive it prints upto limit stack trace entries from traceback object tb. Otherwise, print the last abs(limit) entries. If limit is omitted or None, all entries are printed. If file is omitted or None, the output goes to sys.
If there is no variable defined with that name you will get a NameError exception. To fix the problem, in Python 2, you can use raw_input() . This returns the string entered by the user and does not attempt to evaluate it. Note that if you were using Python 3, input() behaves the same as raw_input() does in Python 2.
format_exc is really just
etype, value, tb = sys.exc_info() return ''.join(format_exception(etype, value, tb, limit))
So if you have the exception type, value, and traceback ready, it should be easy. If you have just the exception, notice that format_exception
is essentially.
list = ['Traceback (most recent call last):\n'] list = list + format_tb(tb, limit)
where limit defaults to None.
Have you tried traceback.print_tb or traceback.format_tb?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With