Package conary :: Package lib :: Module stackutil
[hide private]
[frames] | no frames]

Source Code for Module conary.lib.stackutil

  1  # 
  2  # Copyright (c) 2004-2008 rPath, Inc. 
  3  # 
  4  # This program is distributed under the terms of the Common Public License, 
  5  # version 1.0. A copy of this license should have been distributed with this 
  6  # source file in a file called LICENSE. If it is not present, the license 
  7  # is always available at http://www.rpath.com/permanent/licenses/CPL-1.0. 
  8  # 
  9  # This program is distributed in the hope that it will be useful, but 
 10  # without any warranty; without even the implied warranty of merchantability 
 11  # or fitness for a particular purpose. See the Common Public License for 
 12  # full details. 
 13  # 
 14   
 15  """ Tools for printing out extended information about frame variables """ 
 16   
 17  import inspect 
 18  import smtplib 
 19  import sys 
 20  import string 
 21  import tempfile 
 22  import traceback 
 23  import xmlrpclib 
 24   
 25  from repr import Repr 
 26  _repr = Repr() 
 27  _repr.maxstring = 3000 
 28  _saferepr = _repr.repr 
 29   
30 -def printTraceBack(tb=None, output=sys.stderr, exc_type=None, exc_msg=None):
31 if isinstance(output, str): 32 output = open(output, 'w') 33 34 exc_info = sys.exc_info() 35 if tb is None: 36 tb = exc_info[2] 37 38 if exc_type is None: 39 exc_type = exc_info[0] 40 41 if exc_msg is None: 42 exc_msg = exc_info[1] 43 44 if exc_type is not None: 45 output.write('Exception: ') 46 exc_info = '\n'.join(traceback.format_exception_only(exc_type, exc_msg)) 47 output.write(exc_info) 48 output.write('\n\n') 49 50 lines = traceback.format_exception(exc_type, exc_msg, tb) 51 output.write(string.joinfields(lines, "")) 52 53 while tb: 54 _printFrame(tb.tb_frame, output=output) 55 tb = tb.tb_next
56
57 -def printFrame(frame=0, output=sys.stderr):
58 # if output is a path, assume it is a writable one 59 # otherwise, it must be an already opened file 60 if isinstance(output, str): 61 output = open(output, 'w') 62 # skip this frame because who cares about the printFrame func? 63 if isinstance(frame, int): 64 # stack was given in depth form 65 # (skip the current frame when counting depth) 66 frame = sys._getframe(frame + 1) 67 _printFrame(frame, output)
68
69 -def printStack(frame=0, output=sys.stderr):
70 if isinstance(output, str): 71 output = open(output, 'w') 72 if isinstance(frame, int): 73 # stack was given in depth form 74 # (skip the current frame when counting depth) 75 frame = sys._getframe(frame + 1) 76 while(frame): 77 output.write("*************************************\n") 78 _printFrame(frame, output) 79 frame = frame.f_back
80
81 -def mailStack(frame, recips, sender, subject, extracontent=None):
82 file = tempfile.TemporaryFile() 83 file.write('Subject: ' + subject + '\n\n') 84 if extracontent: 85 file.write(extracontent) 86 printStack(frame, file) 87 server = smtplib.SMTP('localhost') 88 file.seek(0) 89 server.sendmail(sender, 90 recips, 91 file.read()) 92 server.close() 93 file.close()
94
95 -def _printFrame(f, output=sys.stderr):
96 c = f.f_code 97 argcount = c.co_argcount 98 varnames = c.co_varnames 99 args = varnames[:argcount] 100 locals = f.f_locals 101 globals = f.f_globals 102 output.write(">> %s:%s: %s.%s(%s)\n" % ( c.co_filename, f.f_lineno, globals['__name__'], c.co_name, ', '.join(args) )) 103 104 localkeys = [ l for l in f.f_locals.keys() if not inspect.ismodule(locals[l] ) ] 105 if argcount > 0: 106 output.write(" Params: \n") 107 for var in varnames[:argcount]: 108 if var in locals: 109 val = locals[var] 110 val = _getStringValue(val) 111 localkeys.remove(var) 112 else: 113 val = '<Unknown>' 114 115 output.write(" %s = %s\n" % (var, _saferepr(val))) 116 for hidden in ('__file__', '__name__', '__doc__'): 117 if hidden in localkeys: 118 localkeys.remove(hidden) 119 localkeys.sort() 120 if localkeys: 121 output.write(" Locals: \n") 122 for key in localkeys: 123 if key in locals: 124 val = locals[key] 125 val = _getStringValue(val) 126 else: 127 val = '<Unknown>' 128 output.write(" %s = %r\n" % (key, _saferepr(val)))
129
130 -def _getStringValue(val):
131 try: 132 if isinstance(val, xmlrpclib.ServerProxy): 133 rval = "<Server Proxy>" 134 elif hasattr(val, 'asString'): 135 rval = val.asString() 136 elif inspect.isclass(val): 137 rval = '<Class %s.%s>' % (val.__module__, val.__name__) 138 elif not hasattr(val, '__str__'): 139 if hasattr(val, '__class__'): 140 rval = '<unprintable of class %s>' % val.__class__ 141 else: 142 rval = '<unprintable>' 143 else: 144 rval = val 145 return rval 146 except Exception, e: 147 try: 148 return '<Exception occurred while converting %s to string: %s' %(repr(val), e) 149 except Exception, e: 150 return '<Exception occurred while converting to repr: %s' %(e)
151