Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
 All Data Structures Namespaces Files Functions Variables
cgitb Namespace Reference

Data Structures

class  Hook
 

Functions

def reset
 
def small
 
def strong
 
def grey
 
def lookup
 
def scanvars
 
def html
 
def enable
 

Variables

string __author__ = 'Ka-Ping Yee'
 
string __version__ = '$Revision: 5816 $'
 
list __UNDEF__ = []
 
tuple handler = Hook()
 

Detailed Description

Handle exceptions in CGI scripts by formatting tracebacks into nice HTML.

To enable this module, do:

    import cgitb; cgitb.enable()

at the top of your CGI script.  The optional arguments to enable() are:

    display     - if true, tracebacks are displayed in the web browser
    logdir      - if set, tracebacks are written to files in this directory
    context     - number of lines of source code to show for each stack frame

By default, tracebacks are displayed but not saved, and context is 5.

Alternatively, if you have caught an exception and want cgitb to display it
for you, call cgitb.handler().  The optional argument to handler() is a 3-item
tuple (etype, evalue, etb) just like the value of sys.exc_info().

Function Documentation

def cgitb.enable (   display = 1,
  logdir = None,
  context = 5 
)
Install an exception handler that formats tracebacks as HTML.

The optional argument 'display' can be set to 0 to suppress sending the
traceback to the browser, and 'logdir' can be set to a directory to cause
tracebacks to be written to files there.

Definition at line 199 of file cgitb.py.

200 def enable(display=1, logdir=None, context=5):
201  """Install an exception handler that formats tracebacks as HTML.
202 
203  The optional argument 'display' can be set to 0 to suppress sending the
204  traceback to the browser, and 'logdir' can be set to a directory to cause
205  tracebacks to be written to files there."""
206  sys.excepthook = Hook(display, logdir, context)
def cgitb.grey (   text)

Definition at line 37 of file cgitb.py.

37 
38 def grey(text): return '<font color="#909090">' + text + '</font>'
def cgitb.html (   etype,
  evalue,
  etb,
  context = 5 
)
Return a nice HTML document describing a given traceback.

Definition at line 69 of file cgitb.py.

References sre_parse.dir, traceback.format_exception(), inspect.formatargvalues(), inspect.getargvalues(), inspect.getinnerframes(), linecache.getline(), grey(), dospath.join(), scanvars(), small(), locale.str(), and strong().

69 
70 def html((etype, evalue, etb), context=5):
71  """Return a nice HTML document describing a given traceback."""
72  import os, types, time, traceback, linecache, inspect, pydoc
73 
74  if type(etype) is types.ClassType:
75  etype = etype.__name__
76  pyver = 'Python ' + sys.version.split()[0] + ': ' + sys.executable
77  date = time.ctime(time.time())
78  head = '<body bgcolor="#f0f0f8">' + pydoc.html.heading(
79  '<big><big><strong>%s</strong></big></big>' % str(etype),
80  '#ffffff', '#6622aa', pyver + '<br>' + date) + '''
81 <p>A problem occurred in a Python script. Here is the sequence of
82 function calls leading up to the error, in the order they occurred.'''
83 
84  indent = '<tt>' + small('&nbsp;' * 5) + '&nbsp;</tt>'
85  frames = []
86  records = inspect.getinnerframes(etb, context)
87  for frame, file, lnum, func, lines, index in records:
88  file = file and os.path.abspath(file) or '?'
89  link = '<a href="file://%s">%s</a>' % (file, pydoc.html.escape(file))
90  args, varargs, varkw, locals = inspect.getargvalues(frame)
91  call = ''
92  if func != '?':
93  call = 'in ' + strong(func) + \
94  inspect.formatargvalues(args, varargs, varkw, locals,
95  formatvalue=lambda value: '=' + pydoc.html.repr(value))
96 
97  highlight = {}
98  def reader(lnum=[lnum]):
99  highlight[lnum[0]] = 1
100  try: return linecache.getline(file, lnum[0])
101  finally: lnum[0] += 1
102  vars = scanvars(reader, frame, locals)
103 
104  rows = ['<tr><td bgcolor="#d8bbff">%s%s %s</td></tr>' %
105  ('<big>&nbsp;</big>', link, call)]
106  if index is not None:
107  i = lnum - index
108  for line in lines:
109  num = small('&nbsp;' * (5-len(str(i))) + str(i)) + '&nbsp;'
110  line = '<tt>%s%s</tt>' % (num, pydoc.html.preformat(line))
111  if i in highlight:
112  rows.append('<tr><td bgcolor="#ffccee">%s</td></tr>' % line)
113  else:
114  rows.append('<tr><td>%s</td></tr>' % grey(line))
115  i += 1
116 
117  done, dump = {}, []
118  for name, where, value in vars:
119  if name in done: continue
120  done[name] = 1
121  if value is not __UNDEF__:
122  if where == 'global': name = '<em>global</em> ' + strong(name)
123  elif where == 'local': name = strong(name)
124  else: name = where + strong(name.split('.')[-1])
125  dump.append('%s&nbsp;= %s' % (name, pydoc.html.repr(value)))
126  else:
127  dump.append(name + ' <em>undefined</em>')
128 
129  rows.append('<tr><td>%s</td></tr>' % small(grey(', '.join(dump))))
130  frames.append('''<p>
131 <table width="100%%" cellspacing=0 cellpadding=0 border=0>
132 %s</table>''' % '\n'.join(rows))
133 
134  exception = ['<p>%s: %s' % (strong(str(etype)), str(evalue))]
135  if type(evalue) is types.InstanceType:
136  for name in dir(evalue):
137  value = pydoc.html.repr(getattr(evalue, name))
138  exception.append('\n<br>%s%s&nbsp;=\n%s' % (indent, name, value))
139 
140  import traceback
141  return head + ''.join(frames) + ''.join(exception) + '''
142 
143 
144 <!-- The above is a description of an error in a Python program, formatted
145  for a Web browser because the 'cgitb' module was enabled. In case you
146  are not reading this in a Web browser, here is the original traceback:
147 
148 %s
149 -->
150 ''' % ''.join(traceback.format_exception(etype, evalue, etb))
def cgitb.lookup (   name,
  frame,
  locals 
)
Find the value for a given name in the given environment.

Definition at line 39 of file cgitb.py.

39 
40 def lookup(name, frame, locals):
41  """Find the value for a given name in the given environment."""
42  if name in locals:
43  return 'local', locals[name]
44  if name in frame.f_globals:
45  return 'global', frame.f_globals[name]
46  return None, __UNDEF__
def cgitb.reset ( )
Return a string that resets the CGI and browser to a known state.

Definition at line 24 of file cgitb.py.

24 
25 def reset():
26  """Return a string that resets the CGI and browser to a known state."""
27  return '''<!--: spam
28 Content-Type: text/html
29 
30 <body bgcolor="#f0f0f8"><font color="#f0f0f8" size="-5"> -->
31 <body bgcolor="#f0f0f8"><font color="#f0f0f8" size="-5"> --> -->
32 </font> </font> </font> </script> </object> </blockquote> </pre>
33 </table> </table> </table> </table> </table> </font> </font> </font>'''
def cgitb.scanvars (   reader,
  frame,
  locals 
)
Scan one logical line of Python and look up values of variables used.

Definition at line 47 of file cgitb.py.

References lookup().

47 
48 def scanvars(reader, frame, locals):
49  """Scan one logical line of Python and look up values of variables used."""
50  import tokenize, keyword
51  vars, lasttoken, parent, prefix = [], None, None, ''
52  for ttype, token, start, end, line in tokenize.generate_tokens(reader):
53  if ttype == tokenize.NEWLINE: break
54  if ttype == tokenize.NAME and token not in keyword.kwlist:
55  if lasttoken == '.':
56  if parent is not __UNDEF__:
57  value = getattr(parent, token, __UNDEF__)
58  vars.append((prefix + token, prefix, value))
59  else:
60  where, value = lookup(token, frame, locals)
61  vars.append((token, where, value))
62  elif token == '.':
63  prefix += lasttoken + '.'
64  parent = value
65  else:
66  parent, prefix = None, ''
67  lasttoken = token
68  return vars
def cgitb.small (   text)

Definition at line 35 of file cgitb.py.

35 
def small(text): return '<small>' + text + '</small>'
def cgitb.strong (   text)

Definition at line 36 of file cgitb.py.

36 
def strong(text): return '<strong>' + text + '</strong>'

Variable Documentation

string __author__ = 'Ka-Ping Yee'

Definition at line 19 of file cgitb.py.

list __UNDEF__ = []

Definition at line 34 of file cgitb.py.

string __version__ = '$Revision: 5816 $'

Definition at line 20 of file cgitb.py.

tuple handler = Hook()

Definition at line 198 of file cgitb.py.