1
2
3
4
5
6
7
8
9
10
11
12
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
58
59
60 if isinstance(output, str):
61 output = open(output, 'w')
62
63 if isinstance(frame, int):
64
65
66 frame = sys._getframe(frame + 1)
67 _printFrame(frame, output)
68
70 if isinstance(output, str):
71 output = open(output, 'w')
72 if isinstance(frame, int):
73
74
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
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
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