Package pype :: Module pypedebug
[frames] | no frames]

Source Code for Module pype.pypedebug

  1  # -*- Mode: Python; tab-width: 4; py-indent-offset: 4; -*- 
  2   
  3  """Debugging tools for python and pype 
  4   
  5  Author -- James A. Mazer (mazerj@gmail.com) 
  6   
  7  """ 
  8   
  9  import sys 
 10  import code 
 11  import traceback 
 12  import types 
 13  import string 
 14  import os 
 15  from guitools import warn 
 16   
17 -def trace():
18 # useful little tool: prints current file & lineno 19 import inspect 20 print 'TRACE %s:%d' % ( 21 inspect.getfile(inspect.currentframe().f_back), 22 inspect.currentframe().f_back.f_lineno, 23 )
24
25 -def remotedebug():
26 # see: http://code.google.com/p/winpdb/ 27 try: 28 import rpdb2 29 p = os.popen('echo debug | winpdb -a pypedebug.py', 'w') 30 rpdb2.start_embedded_debugger('debug') 31 p.close() 32 except ImportError: 33 pass
34
35 -def keyboard(banner='Type EOF/^D to continue', builtin=0):
36 """Clone of the matlab keyboard() function. 37 38 Drop down into interactive shell for debugging 39 Use it like the matlab keyboard command -- dumps you into 40 interactive shell where you can poke around and look at 41 variables in the current stack frame 42 43 The idea and code are stolen from something Fredrick 44 Lundh posted on the web a while back. 45 46 """ 47 48 if builtin: 49 import pdb 50 print '[->pdb]', banner 51 pdb.set_trace() 52 else: 53 # use exception trick to pick up the current frame 54 try: 55 raise None 56 except: 57 frame = sys.exc_info()[2].tb_frame.f_back 58 59 # evaluate commands in current namespace 60 namespace = frame.f_globals.copy() 61 namespace.update(frame.f_locals) 62 63 code.interact(banner=banner, local=namespace)
64
65 -def get_exception(show=None):
66 """Get info about where exception came from""" 67 68 type, value, tb = sys.exc_info() 69 if show: 70 traceback.print_exception(type, value, tb) 71 return string.join(traceback.format_exception(type, value, tb))
72
73 -def get_traceback(show=None):
74 """Stack dump to stdout. 75 76 Collect the current exception information (after catching 77 the exception) as a string so it can be reported to the 78 user or logged. 79 This is an internal function, don't call it directly, use 80 the reporterror() function instead. 81 82 Stolen from the Pmw source code. 83 """ 84 85 # Fetch current exception values. 86 exc_type, exc_value, tb = sys.exc_info() 87 88 # Give basic information about the callback exception. 89 if type(exc_type) == types.ClassType: 90 # Handle python 1.5 class exceptions. 91 exc_type = exc_type.__name__ 92 93 # in python-2.5 exc_type is not a string, must be coerced into one.. 94 msg = [] 95 msg.append('Exception: %s (%s)\n' % (exc_value, exc_type)) 96 97 # Add the traceback. 98 stack = traceback.extract_tb(tb) 99 100 depth = 1 101 for frame in stack: 102 (file, line, fn, text) = frame 103 prefix = '>' * depth 104 msg.append('%s File "%s", line %s, in %s:\n' % (prefix, file, line, fn)) 105 msg.append('%s %s\n' % (prefix, text)) 106 depth = depth + 1 107 msg = string.join(msg) 108 if show: 109 sys.stderr.write(msg) 110 return msg
111 112
113 -def reporterror(gui=True, dbug=False):
114 """Pretty printer for error messages. 115 116 Pretty print a timestamped error message on the console 117 or popup a dialog window based on the current exception 118 state in the current stack frame. This is really just 119 for debugging. 120 """ 121 emsg = get_traceback() 122 sys.stderr.write(emsg) 123 if gui: 124 warn('reporterror', emsg, wait=0, astext=1) 125 if dbug: 126 keyboard()
127
128 -def ppDict(d):
129 """Pretty print a dictionary 130 """ 131 ks = d.keys() 132 ks.sort() 133 for k in ks: 134 print '%-20s %15s=<%s>' % (type(d[k]), k, d[k])
135
136 -def debug(set=None):
137 global _DEBUGFLAG 138 139 if set is not None: 140 _DEBUGFLAG = set 141 else: 142 # if DEBUGFLAG's not set, set it to 0... 143 try: 144 x = _DEBUGFLAG 145 except NameError: 146 _DEBUGFLAG = 0 147 return _DEBUGFLAG
148