Skip to content

Feature: Support for colorizing Python trace output #1955

Description

@zachriggle

The Python stdlib module trace module is very useful for tracing what code gets executed.

It would be nice to get bat-colorized output for this.

I can work around this to some extent, by manually invoking bat -r a:b for each line that gets printed, but this is incredibly non-performant.

Alternatively, are there resources that I can look at for adding new languages? Simple regular expressions should work for the 99% use-case.

Example Script

#!/usr/bin/env python3
# simple.py
import time

def add(x=0, y=0):
    return x + y

add(13, 1)

add(-1, 33)

add(-1,
    1 << 12)

def subtract(x=0,
             y=0,
             flag=False):

    result = x - y
    if flag:
        print(f'{result} == {x} - {y}')
    return result

subtract(13, 1)

time.sleep(1)

subtract(13, 1, flag=True)

Output

$ python -m trace --timing --trace ./simple.py
 --- modulename: simple, funcname: <module>
0.00 simple.py(2): import time
0.00 simple.py(4): def add(x=0, y=0):
0.00 simple.py(7): add(13, 1)
 --- modulename: simple, funcname: add
0.00 simple.py(5):     return x + y
0.00 simple.py(9): add(-1, 33)
 --- modulename: simple, funcname: add
0.00 simple.py(5):     return x + y
0.00 simple.py(11): add(-1,
0.00 simple.py(12):     1 << 12)
0.00 simple.py(11): add(-1,
 --- modulename: simple, funcname: add
0.00 simple.py(5):     return x + y
0.00 simple.py(14): def subtract(x=0,
0.00 simple.py(23): subtract(13, 1)
 --- modulename: simple, funcname: subtract
0.00 simple.py(18):     result = x - y
0.00 simple.py(19):     if flag:
0.00 simple.py(21):     return result
0.00 simple.py(25): time.sleep(1)
1.01 simple.py(27): subtract(13, 1, flag=True)
 --- modulename: simple, funcname: subtract
1.01 simple.py(18):     result = x - y
1.01 simple.py(19):     if flag:
1.01 simple.py(20):         print(f'{result} == {x} - {y}')
12 == 13 - 1
1.01 simple.py(21):     return result

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions