Skip to content

Instantly share code, notes, and snippets.

@nod
Created March 16, 2011 16:24
Show Gist options
  • Save nod/872761 to your computer and use it in GitHub Desktop.
Save nod/872761 to your computer and use it in GitHub Desktop.
syntax highlighting for pdb source listings
# put the following in your usercustomize.py file.
# see https://33ad.org/blog/pycon-2011-and-colorized-pdb-source-listings
# for more info and explanation.
# colorize pdb source output
import pdb
import StringIO
from pygments import highlight
from pygments.lexers import PythonLexer, PythonTracebackLexer
from pygments.formatters import TerminalFormatter
def colorize(obj, temp_f, lexer):
old_stdout = obj.stdout
obj.stdout = StringIO.StringIO()
temp_f()
output = highlight(
obj.stdout.getvalue(),
lexer,
TerminalFormatter())
obj.stdout = old_stdout
print >>obj.stdout, output
pdb.Pdb.old_do_list = pdb.Pdb.do_list
instancemethod = type(pdb.Pdb.do_list)
def wrap_do_list(self, arg):
f = lambda: self.old_do_list(arg)
colorize(self, f, PythonLexer())
pdb.Pdb.do_list = instancemethod(wrap_do_list, None, pdb.Pdb)
pdb.Pdb.do_l = instancemethod(wrap_do_list, None, pdb.Pdb)
pdb.Pdb.old_print_stack_entry = pdb.Pdb.print_stack_entry
instancemethod = type(pdb.Pdb.print_stack_entry)
def wrap_print_stack_entry(self, *a, **ka):
f = lambda: self.old_print_stack_entry(*a, **ka)
colorize(self, f, PythonLexer())
pdb.Pdb.print_stack_entry = instancemethod(wrap_print_stack_entry, None, pdb.Pdb)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment