Super Verbose python debugging

Written by John Benson on .

traceit
import sys

def
traceit(frame, event, arg):

"""
used by sys.settrace() to provide trace dumps for python apps
this function returns itself, per expectation of sys.settrace()
from Andrew Dalke's blog at http://www.dalkescientific.com/writings/diary/archive/2005/04/20/tracing_python_code.html
 
This env var TDNUKE_TRACE_DEBUG can take on the values defined in
http://docs.python.org/lib/debugger-hooks.html, ie:
'call', 'line', 'return', 'exception', 'c_call', 'c_return' and 'c_exception'.
Empty string equivalent to 'call'.
setenv TDNUKE_TRACE_DEBUG call
setenv TDNUKE_TRACE_DEBUG line
setenv TDNUKE_TRACE_DEBUG return
setenv TDNUKE_TRACE_DEBUG exception
setenv TDNUKE_TRACE_DEBUG c_call
setenv TDNUKE_TRACE_DEBUG c_return
setenv TDNUKE_TRACE_DEBUG c_exception
unsetenv TDNUKE_TRACE_DEBUG to stop printing
"""

if event == os.environ['TDNUKE_TRACE_DEBUG']:
lineno = frame.f_lineno
filename = ''
if frame.f_globals.has_key("__file__"):
filename = frame.f_globals["__file__"]
if (filename.endswith(".pyc") or
filename.endswith(".pyo")):
filename = filename[:-1]
name = frame.f_globals["__name__"]
line = linecache.getline(filename, lineno)
if 'nuke' in name.lower():
sys.stderr.write("%s:%s: %s\n" % (name, lineno, line.rstrip()))
sys.stderr.flush()
return traceit
 
 
if os.environ.has_key('TDNUKE_TRACE_DEBUG'):
import linecache
if os.environ['TDNUKE_TRACE_DEBUG'] == '':
os.environ['TDNUKE_TRACE_DEBUG'] = 'call' # returns function called
sys.settrace(traceit)
 
 

You have no rights to post comments

We have 245 guests and 13 members online