11#
22# This module was written by Ka-Ping Yee, <[email protected] >. 33#
4- # $Id: cgitb.py,v 1.5 2002-09-10 01:07:05 richard Exp $
4+ # $Id: cgitb.py,v 1.6 2002-09-13 03:31:18 richard Exp $
55
66__doc__ = """
77Extended CGI traceback handler by Ka-Ping Yee, <[email protected] >. 88"""
99
10- import sys , os , types , string , keyword , linecache , tokenize , inspect
10+ import sys , os , types , string , keyword , linecache , tokenize , inspect , cgi
1111import pydoc , traceback
1212
1313from roundup .i18n import _
@@ -20,43 +20,55 @@ def breaker():
2020def niceDict (indent , dict ):
2121 l = []
2222 for k ,v in dict .items ():
23- l .append ('%s%s: %r' % (indent ,k ,v ))
23+ l .append ('<tr><td><strong>%s</strong></td><td>%s</td></tr>' % (k ,
24+ cgi .escape (repr (v ))))
2425 return '\n ' .join (l )
2526
2627def pt_html (context = 5 ):
27- import cgi
28- etype , evalue = sys .exc_type , sys .exc_value
29- if type (etype ) is types .ClassType :
30- etype = etype .__name__
31- pyver = 'Python ' + string .split (sys .version )[0 ] + '<br>' + sys .executable
32- head = pydoc .html .heading (
33- '<font size=+1><strong>%s</strong>: %s</font>' % (etype , evalue ),
34- '#ffffff' , '#777777' , pyver )
35-
36- head = head + _ ('<p>A problem occurred in your template</p><pre>' )
37-
38- l = []
28+ l = ['<h1>Templating Error</h1>'
29+ '<p class="help">Debugging information follows</p>'
30+ '<ol>' ]
31+ from roundup .cgi .PageTemplates .Expressions import TraversalError
3932 for frame , file , lnum , func , lines , index in inspect .trace (context ):
4033 args , varargs , varkw , locals = inspect .getargvalues (frame )
4134 if locals .has_key ('__traceback_info__' ):
4235 ti = locals ['__traceback_info__' ]
43- l .append (str (ti ))
36+ if isinstance (ti , TraversalError ):
37+ s = []
38+ for name , info in ti .path :
39+ s .append ('<li>"%s" (%s)</li>' % (name ,cgi .escape (repr (info ))))
40+ s = '\n ' .join (s )
41+ l .append ('<li>Looking for "%s", current path:<ol>%s</ol></li>' % (
42+ ti .name , s ))
43+ else :
44+ l .append ('<li>In %s</li>' % cgi .escape (str (ti )))
4445 if locals .has_key ('__traceback_supplement__' ):
4546 ts = locals ['__traceback_supplement__' ]
4647 if len (ts ) == 2 :
4748 supp , context = ts
48- l .append ('in template %r' % context .id )
49+ l .append ('<li>A problem occurred in your template "%s"</li>' %
50+ str (context .id ))
4951 elif len (ts ) == 3 :
5052 supp , context , info = ts
51- l .append ('in expression %r\n current variables:\n %s\n %s\n ' % (info ,
52- niceDict (' ' , context .global_vars ),
53- niceDict (' ' , context .local_vars )))
54- # context._scope_stack))
55-
56- l .append ('\n ' )
57- l .append ('' .join (traceback .format_exception (etype , evalue ,
58- sys .exc_traceback )))
59- return head + cgi .escape ('\n ' .join (l )) + '</pre><p> </p>'
53+ l .append ('''
54+ <li>While evaluating the %r expression on line %d
55+ <table class="otherinfo" style="font-size: 90%%">
56+ <tr><th colspan="2" class="header">Current variables:</th></tr>
57+ %s
58+ %s
59+ </table></li>
60+ ''' % (info , context .position [0 ], niceDict (' ' , context .global_vars ),
61+ niceDict (' ' , context .local_vars )))
62+
63+ l .append ('''
64+ </ol>
65+ <table style="font-size: 80%%; color: gray">
66+ <tr><th class="header" align="left">Full traceback:</th></tr>
67+ <tr><td><pre>%s</pre></td></tr>
68+ </table>''' % cgi .escape ('' .join (traceback .format_exception (sys .exc_type ,
69+ sys .exc_value , sys .exc_traceback ))))
70+ l .append ('<p> </p>' )
71+ return '\n ' .join (l )
6072
6173def html (context = 5 ):
6274 etype , evalue = sys .exc_type , sys .exc_value
0 commit comments