python: traceback.print_stack(): how to colorize and reformat output

  • Last Update :
  • Techknowledgy :

Pygments lists the available lexers. You can do this with Python3TracebackLexer.

from pygments
import highlight
from pygments.lexers
import Python3TracebackLexer
from pygments.formatters
import TerminalTrueColorFormatter

err_str = ''
'
File ".venv/lib/python3.7/site-packages/django/db/models/query.py", line 144, in __iter__
return compiler.results_iter(tuple_expected = True, chunked_fetch = self.chunked_fetch, chunk_size = self.chunk_size)
File ".venv/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1052, in results_iter
results = self.execute_sql(MULTI, chunked_fetch = chunked_fetch, chunk_size = chunk_size)
File ".venv/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1100, in execute_sql
cursor.execute(sql, params)
File ".venv/lib/python3.7/site-packages/django/db/backends/utils.py", line 110, in execute
extra = {
   'duration': duration,
   'sql': sql,
   'params': params
}
File "/usr/lib64/python3.7/logging/__init__.py", line 1371, in debug
self._log(DEBUG, msg, args, ** kwargs)
File "/usr/lib64/python3.7/logging/__init__.py", line 1519, in _log
self.handle(record)
File "/usr/lib64/python3.7/logging/__init__.py", line 1528, in handle
if (not self.disabled) and self.filter(record):
   File "/usr/lib64/python3.7/logging/__init__.py", line 762, in filter
result = f.filter(record)
File "basic_django/settings.py", line 402, in filter
traceback.print_stack()
''
'

print(highlight(err_str, Python3TracebackLexer(), TerminalTrueColorFormatter()))

In order to get err_str, replace print_stack with format_stack as follows than do:

def colorize_traceback(err_str):
   return highlight(err_str, Python3TracebackLexer(), TerminalTrueColorFormatter())

try:
...# Some logic
except Exception: # Or a more narrow exception
# tb.print_stack()
print(colorize_traceback(tb.format_stack()))

With just two lines of code, it will prettify your tracebacks... and then some!

from rich.traceback
import install
install()

Suggestion : 2

This module provides a standard interface to extract, format and print stack traces of Python programs. It exactly mimics the behavior of the Python interpreter when it prints a stack trace. This is useful when you want to print stack traces under program control, such as in a “wrapper” around the interpreter.,The following example demonstrates the different ways to print and format the exception and traceback:,Extract the raw traceback from the current stack frame. The return value has the same format as for extract_tb(). The optional f and limit arguments have the same meaning as for print_stack().,The module uses traceback objects — this is the object type that is stored in the sys.last_traceback variable and returned as the third item from sys.exc_info().

import sys, traceback

def run_user_code(envdir):
   source = input(">>> ")
try:
exec(source, envdir)
except Exception:
   print("Exception in user code:")
print("-" * 60)
traceback.print_exc(file = sys.stdout)
print("-" * 60)

envdir = {}
while True:
   run_user_code(envdir)
import sys, traceback

def lumberjack():
   bright_side_of_death()

def bright_side_of_death():
   return tuple()[0]

try:
lumberjack()
except IndexError:
   exc_type, exc_value, exc_traceback = sys.exc_info()
print("*** print_tb:")
traceback.print_tb(exc_traceback, limit = 1, file = sys.stdout)
print("*** print_exception:")
# exc_type below is ignored on 3.5 and later
traceback.print_exception(exc_type, exc_value, exc_traceback,
   limit = 2, file = sys.stdout)
print("*** print_exc:")
traceback.print_exc(limit = 2, file = sys.stdout)
print("*** format_exc, first and last line:")
formatted_lines = traceback.format_exc().splitlines()
print(formatted_lines[0])
print(formatted_lines[-1])
print("*** format_exception:")
# exc_type below is ignored on 3.5 and later
print(repr(traceback.format_exception(exc_type, exc_value,
   exc_traceback)))
print("*** extract_tb:")
print(repr(traceback.extract_tb(exc_traceback)))
print("*** format_tb:")
print(repr(traceback.format_tb(exc_traceback)))
print("*** tb_lineno:", exc_traceback.tb_lineno)
*** print_tb:
File "<doctest...>", line 10, in <module>
      lumberjack()
      *** print_exception:
      Traceback (most recent call last):
      File "<doctest...>", line 10, in <module>
            lumberjack()
            File "<doctest...>", line 4, in lumberjack
               bright_side_of_death()
               IndexError: tuple index out of range
               *** print_exc:
               Traceback (most recent call last):
               File "<doctest...>", line 10, in <module>
                     lumberjack()
                     File "<doctest...>", line 4, in lumberjack
                        bright_side_of_death()
                        IndexError: tuple index out of range
                        *** format_exc, first and last line:
                        Traceback (most recent call last):
                        IndexError: tuple index out of range
                        *** format_exception:
                        ['Traceback (most recent call last):\n',
                        ' File "<doctest...>", line 10, in <module>\n lumberjack()\n',
                              ' File "<doctest...>", line 4, in lumberjack\n bright_side_of_death()\n',
                                 ' File "<doctest...>", line 7, in bright_side_of_death\n return tuple()[0]\n',
                                    'IndexError: tuple index out of range\n']
                                    *** extract_tb:
                                    [<FrameSummary file <doctest...>, line 10 in <module>>,
                                          <FrameSummary file <doctest...>, line 4 in lumberjack>,
                                             <FrameSummary file <doctest...>, line 7 in bright_side_of_death>]
                                                *** format_tb:
                                                [' File "<doctest...>", line 10, in <module>\n lumberjack()\n',
                                                      ' File "<doctest...>", line 4, in lumberjack\n bright_side_of_death()\n',
                                                         ' File "<doctest...>", line 7, in bright_side_of_death\n return tuple()[0]\n']
                                                            *** tb_lineno: 10
>>> import traceback
>>> def another_function():
... lumberstack()
...
>>> def lumberstack():
... traceback.print_stack()
... print(repr(traceback.extract_stack()))
... print(repr(traceback.format_stack()))
...
>>> another_function()
File "<doctest>", line 10, in <module>
      another_function()
      File "<doctest>", line 3, in another_function
         lumberstack()
         File "<doctest>", line 6, in lumberstack
            traceback.print_stack()
            [('<doctest>', 10, '<module>', 'another_function()'),
                  ('<doctest>', 3, 'another_function', 'lumberstack()'),
                     ('<doctest>', 7, 'lumberstack', 'print(repr(traceback.extract_stack()))')]
                        [' File "<doctest>", line 10, in <module>\n another_function()\n',
                              ' File "<doctest>", line 3, in another_function\n lumberstack()\n',
                                 ' File "<doctest>", line 8, in lumberstack\n print(repr(traceback.format_stack()))\n']
>>> import traceback
>>> traceback.format_list([('spam.py', 3, '<module>', 'spam.eggs()'),
   ... ('eggs.py', 42, 'eggs', 'return "bacon"')])
   [' File "spam.py", line 3, in <module>\n spam.eggs()\n',
      ' File "eggs.py", line 42, in eggs\n return "bacon"\n']
      >>> an_error = IndexError('tuple index out of range')
      >>> traceback.format_exception_only(type(an_error), an_error)
      ['IndexError: tuple index out of range\n']

Suggestion : 3

Adds variables to python traceback. Simple, lightweight, controllable. Debug reasons of exceptions by logging or pretty printing colorful variable contexts for each frame in a stacktrace, showing every value. Dump locals environments after errors to console, files, and loggers. Works with Jupiter and IPython.,Function decorator, used for logging or simple printing of scoped tracebacks with variables. I.e. traceback is shorter as it includes only frames inside the function call. Program exiting due to unhandled exception still prints a usual traceback.,Context manager (i.e. with ...), used for logging or simple printing of scoped tracebacks with variables. I.e. traceback is shorter as it includes only frames inside the with scope. Program exiting due to unhandled exception still prints a usual traceback.,Call once in the beginning of your program, to change how traceback after an uncaught exception looks.

Installation

pip install traceback - with - variables
pip install traceback-with-variables
conda install - c conda - forge traceback - with - variables

Using without code editing, running your script/command/module:

traceback - with - variables tested_script.py...srcipt 's args...

Simplest usage in Jupyter or IPython, for the whole program:

    from traceback_with_variables
    import activate_in_ipython_by_import

Decorator, for a single function:

    @prints_exc
    # def main(): or def some_func(...):

Turn a code totally covered by debug formatting from this:

  def main():
     sizes_str = sys.argv[1]
  h1, w1, h2, w2 = map(int, sizes_str.split()) -
     try:
     return get_avg_ratio([h1, w1], [h2, w2]) -
        except:
        -logger.error(f 'something happened :(, variables = {locals()[:1000]}') -
        raise -
        # or -
        raise MyToolException(f 'something happened :(, variables = {locals()[:1000]}')

  def get_avg_ratio(size1, size2):
     -
     try:
     return mean(get_ratio(h, w) for h, w in [size1, size2]) -
        except:
        -logger.error(f 'something happened :(, size1 = {size1}, size2 = {size2}') -
        raise -
        # or -
        raise MyToolException(f 'something happened :(, size1 = {size1}, size2 = {size2}')

  def get_ratio(height, width):
     -
     try:
     return height / width -
        except:
        -logger.error(f 'something happened :(, width = {width}, height = {height}') -
        raise -
        # or -
        raise MyToolException(f 'something happened :(, width = {width}, height = {height}')

into this (zero debug code):

+from traceback_with_variables
import activate_by_import

def main():
   sizes_str = sys.argv[1]
h1, w1, h2, w2 = map(int, sizes_str.split())
return get_avg_ratio([h1, w1], [h2, w2])

def get_avg_ratio(size1, size2):
   return mean(get_ratio(h, w) for h, w in [size1, size2])

def get_ratio(height, width):
   return height / width

And obtain total debug info spending 0 lines of code:

Traceback with variables (most recent call last):
File "./temp.py", line 7, in main
return get_avg_ratio([h1, w1], [h2, w2])
sizes_str = '300 200 300 0'
h1 = 300
w1 = 200
h2 = 300
w2 = 0
File "./temp.py", line 10, in get_avg_ratio
return mean([get_ratio(h, w) for h, w in [size1, size2]])
size1 = [300, 200]
size2 = [300, 0]
File "./temp.py", line 10, in <listcomp>
   return mean([get_ratio(h, w) for h, w in [size1, size2]])
   .0 = <tuple_iterator object at 0x7ff61e35b820>
      h = 300
      w = 0
      File "./temp.py", line 13, in get_ratio
      return height / width
      height = 300
      width = 0
      builtins.ZeroDivisionError: division by zero

Automate your logging too:

logger = logging.getLogger('main')

def main():
   ...
   with printing_exc(file_ = LoggerAsFile(logger))
   ...
logger = logging.getLogger('main')

def main():
    ...
    with printing_exc(file_=LoggerAsFile(logger))
        ...
2020-03-30 18:24:31 main ERROR Traceback with variables (most recent call last):
2020-03-30 18:24:31 main ERROR File "./temp.py", line 7, in main
2020-03-30 18:24:31 main ERROR return get_avg_ratio([h1, w1], [h2, w2])
2020-03-30 18:24:31 main ERROR sizes_str = '300 200 300 0'
2020-03-30 18:24:31 main ERROR h1 = 300
2020-03-30 18:24:31 main ERROR w1 = 200
2020-03-30 18:24:31 main ERROR h2 = 300
2020-03-30 18:24:31 main ERROR w2 = 0
2020-03-30 18:24:31 main ERROR File "./temp.py", line 10, in get_avg_ratio
2020-03-30 18:24:31 main ERROR return mean([get_ratio(h, w) for h, w in [size1, size2]])
2020-03-30 18:24:31 main ERROR size1 = [300, 200]
2020-03-30 18:24:31 main ERROR size2 = [300, 0]
2020-03-30 18:24:31 main ERROR File "./temp.py", line 10, in <listcomp>
   2020-03-30 18:24:31 main ERROR return mean([get_ratio(h, w) for h, w in [size1, size2]])
   2020-03-30 18:24:31 main ERROR .0 = <tuple_iterator object at 0x7ff412acb820>
      2020-03-30 18:24:31 main ERROR h = 300
      2020-03-30 18:24:31 main ERROR w = 0
      2020-03-30 18:24:31 main ERROR File "./temp.py", line 13, in get_ratio
      2020-03-30 18:24:31 main ERROR return height / width
      2020-03-30 18:24:31 main ERROR height = 300
      2020-03-30 18:24:31 main ERROR width = 0
      2020-03-30 18:24:31 main ERROR builtins.ZeroDivisionError: division by zero

Free your exceptions of unnecessary information load:

def make_a_cake(sugar, eggs, milk, flour, salt, water):
   is_sweet = sugar > salt
is_vegan = not(eggs or milk)
is_huge = (sugar + eggs + milk + flour + salt + water > 10000)
if not(is_sweet or is_vegan or is_huge):
   raise ValueError('This is unacceptable, guess why!')
   ...

Suggestion : 4

1 day ago 2 days ago  · The line is a string with leading and trailing whitespace stripped; if the source is not available it is None. traceback. extract_stack (f=None, limit=None) ¶. Extract the raw traceback from the current stack frame. The return value has the same format as for extract_tb (). , 1 week ago 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.. When your program results in an exception, Python will print the current traceback to help you know what … , 4 days ago Jul 30, 2020  · Traceback (most recent call last): File "C:/Python27/hdg.py", line 5, in value = A [5] IndexError: list index out of range. The module uses traceback objects, this is the object type that is stored in the sys.last_traceback variable and returned as the third item from sys.exc_info (). ,I have a big python script, with multiple files, and I need to know where a method was called. Is there a backtrace function in python like debug_backtrace in php?


import traceback def foo(): bar() def bar(): baz() def baz(): traceback.print_stack() # or trace = traceback.extract_stack() foo()
import traceback def foo(): bar() def bar(): baz() def baz(): traceback.print_stack() # or trace = traceback.extract_stack() foo()
import pdb
pdb.set_trace()

Suggestion : 5

Last Updated : 01 Aug, 2020

General structure of a stack trace for an exception:

Traceback
for most recent call
Location of the program
Line in the program where error was encountered
Name of the error: relevant information about the exception

Example :

Traceback(most recent call last):
   File "C:/Python27/hdg.py", line 5, in
value = A[5]
IndexError: list index out of range

Output :

Traceback(most recent call last):
   File "C:/Python27/van.py", line 8, in
value = A[5]
IndexError: list index out of range
end of program >>>

FrameSummary Class :
FrameSummary objects represent a single frame in a traceback.

class traceback.FrameSummary(filename, lineno, name, lookup_line = True, locals = None, line = None)