Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Highlighting python stack traces

I'm working on quite complex project and time after time I have to narrow down problems looking at stack traces. They happen to be very long and involve “my” code, standard library code and 3rd party libraries code at same time. Most of time the real problem is in “my” code and locating it instantly in a stack trace is a bit hard for eyes. Under “my” code I mean the code that is under current working directory.

So I realized that I want something that will colorize stack traces and highlight lines that are mine. Compare original to highlighted.

I could write a python script that I could use this way:

nosetests | colorize_stack_trace.py 

But I believe there is a quicker and more elegant way to do this using Linux toolset. Any ideas?

UPD:

Using supercat suggested by Dennis Williamson, the intermediate result is following bash function:

pyst() {     rc=/tmp/spcrc;     echo '#################### ### # # # ########################################' > $rc;     echo '                     blk   0 r ^(.*)$' >> $rc;     echo '                     mag b 0 r ^\s*File "'`pwd`'/(.*)"' >> $rc;     spc -c $rc; } 

Now I can do:

nosetests 2>&1 | pyst 

Not too elegant, but works at some degree. There are two problems left:

  1. I can't see any output before nosetests completes. I.e. I don't see the progress.
  2. I have to write 2>&1 over and over again.

UPD 2:

Asking this question I had mainly nosetests in mind. And I just found great solution: rednose nose plugin. It highlights paths that are local plus do many more handy readability things.

Returning to original question: problems that I noted with supercat don't relate to it completely but its a problem of Unix shell streaming, flushing, piping, redirecting. So as as an answer to the question asked I accept an answer that suggests supercat.

like image 227
nkrkv Avatar asked Feb 19 '10 14:02

nkrkv


People also ask

How do I fix Python traceback error?

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.

What is a stack trace in Python?

The Python stack trace is a valuable piece of information that you can use to debug your code. It contains information about the call stack and points out where things have gone wrong. At the end of a stack trace, you can always find the exact exception type and a detailed message of what's gone wrong.

How do you get a stack trace in Python?

Print Stack Trace in Python Using traceback Module The traceback. format_exc() method returns a string that contains the information about exception and stack trace entries from the traceback object. We can use the format_exc() method to print the stack trace with the try and except statements.


2 Answers

Actually, there is a great Python syntax highlighting library called Pygments, which is also able to highlight tracebacks.

So, all you have to do is:

$ easy_install pygments # downloads and installs pygments $ cat traceback.txt | pygmentize -l pytb 

"pytb" is the shortcut for the PythonTracebackLexer. There is also a special lexer for Python 3 Tracebacks included, which is called "py3tb".

You can format the output in various formats (including html, latex, svg, several image formats and so on). But there is also a terminal formatter available (and if you are wondering... of course there are different color themes available!).

You can use -f html to select another formatter (in that case, the HTML formatter).

like image 120
tux21b Avatar answered Oct 09 '22 16:10

tux21b


Take a look at Supercat (spc). It does both ANSI and HTML highlighting and can be configured for your particular output. It comes with some configuration files for source code files in C and Python, for example and log files, Changelogs, diffs and others.

Based on Dave Kirby's suggestion for vim, this does something similar:

less -p regex file_name 

Or

some_command | less -p regex 
like image 21
Dennis Williamson Avatar answered Oct 09 '22 17:10

Dennis Williamson