ยปCore Development>Code coverage>Lib/cgitb.py

Python code coverage for Lib/cgitb.py

#countcontent
1n/a"""More comprehensive traceback formatting for Python scripts.
2n/a
3n/aTo enable this module, do:
4n/a
5n/a import cgitb; cgitb.enable()
6n/a
7n/aat the top of your script. The optional arguments to enable() are:
8n/a
9n/a display - if true, tracebacks are displayed in the web browser
10n/a logdir - if set, tracebacks are written to files in this directory
11n/a context - number of lines of source code to show for each stack frame
12n/a format - 'text' or 'html' controls the output format
13n/a
14n/aBy default, tracebacks are displayed but not saved, the context is 5 lines
15n/aand the output format is 'html' (for backwards compatibility with the
16n/aoriginal use of this module)
17n/a
18n/aAlternatively, if you have caught an exception and want cgitb to display it
19n/afor you, call cgitb.handler(). The optional argument to handler() is a
20n/a3-item tuple (etype, evalue, etb) just like the value of sys.exc_info().
21n/aThe default handler displays output as HTML.
22n/a
23n/a"""
24n/aimport inspect
25n/aimport keyword
26n/aimport linecache
27n/aimport os
28n/aimport pydoc
29n/aimport sys
30n/aimport tempfile
31n/aimport time
32n/aimport tokenize
33n/aimport traceback
34n/a
35n/adef reset():
36n/a """Return a string that resets the CGI and browser to a known state."""
37n/a return '''<!--: spam
38n/aContent-Type: text/html
39n/a
40n/a<body bgcolor="#f0f0f8"><font color="#f0f0f8" size="-5"> -->
41n/a<body bgcolor="#f0f0f8"><font color="#f0f0f8" size="-5"> --> -->
42n/a</font> </font> </font> </script> </object> </blockquote> </pre>
43n/a</table> </table> </table> </table> </table> </font> </font> </font>'''
44n/a
45n/a__UNDEF__ = [] # a special sentinel object
46n/adef small(text):
47n/a if text:
48n/a return '<small>' + text + '</small>'
49n/a else:
50n/a return ''
51n/a
52n/adef strong(text):
53n/a if text:
54n/a return '<strong>' + text + '</strong>'
55n/a else:
56n/a return ''
57n/a
58n/adef grey(text):
59n/a if text:
60n/a return '<font color="#909090">' + text + '</font>'
61n/a else:
62n/a return ''
63n/a
64n/adef lookup(name, frame, locals):
65n/a """Find the value for a given name in the given environment."""
66n/a if name in locals:
67n/a return 'local', locals[name]
68n/a if name in frame.f_globals:
69n/a return 'global', frame.f_globals[name]
70n/a if '__builtins__' in frame.f_globals:
71n/a builtins = frame.f_globals['__builtins__']
72n/a if type(builtins) is type({}):
73n/a if name in builtins:
74n/a return 'builtin', builtins[name]
75n/a else:
76n/a if hasattr(builtins, name):
77n/a return 'builtin', getattr(builtins, name)
78n/a return None, __UNDEF__
79n/a
80n/adef scanvars(reader, frame, locals):
81n/a """Scan one logical line of Python and look up values of variables used."""
82n/a vars, lasttoken, parent, prefix, value = [], None, None, '', __UNDEF__
83n/a for ttype, token, start, end, line in tokenize.generate_tokens(reader):
84n/a if ttype == tokenize.NEWLINE: break
85n/a if ttype == tokenize.NAME and token not in keyword.kwlist:
86n/a if lasttoken == '.':
87n/a if parent is not __UNDEF__:
88n/a value = getattr(parent, token, __UNDEF__)
89n/a vars.append((prefix + token, prefix, value))
90n/a else:
91n/a where, value = lookup(token, frame, locals)
92n/a vars.append((token, where, value))
93n/a elif token == '.':
94n/a prefix += lasttoken + '.'
95n/a parent = value
96n/a else:
97n/a parent, prefix = None, ''
98n/a lasttoken = token
99n/a return vars
100n/a
101n/adef html(einfo, context=5):
102n/a """Return a nice HTML document describing a given traceback."""
103n/a etype, evalue, etb = einfo
104n/a if isinstance(etype, type):
105n/a etype = etype.__name__
106n/a pyver = 'Python ' + sys.version.split()[0] + ': ' + sys.executable
107n/a date = time.ctime(time.time())
108n/a head = '<body bgcolor="#f0f0f8">' + pydoc.html.heading(
109n/a '<big><big>%s</big></big>' %
110n/a strong(pydoc.html.escape(str(etype))),
111n/a '#ffffff', '#6622aa', pyver + '<br>' + date) + '''
112n/a<p>A problem occurred in a Python script. Here is the sequence of
113n/afunction calls leading up to the error, in the order they occurred.</p>'''
114n/a
115n/a indent = '<tt>' + small('&nbsp;' * 5) + '&nbsp;</tt>'
116n/a frames = []
117n/a records = inspect.getinnerframes(etb, context)
118n/a for frame, file, lnum, func, lines, index in records:
119n/a if file:
120n/a file = os.path.abspath(file)
121n/a link = '<a href="file://%s">%s</a>' % (file, pydoc.html.escape(file))
122n/a else:
123n/a file = link = '?'
124n/a args, varargs, varkw, locals = inspect.getargvalues(frame)
125n/a call = ''
126n/a if func != '?':
127n/a call = 'in ' + strong(func) + \
128n/a inspect.formatargvalues(args, varargs, varkw, locals,
129n/a formatvalue=lambda value: '=' + pydoc.html.repr(value))
130n/a
131n/a highlight = {}
132n/a def reader(lnum=[lnum]):
133n/a highlight[lnum[0]] = 1
134n/a try: return linecache.getline(file, lnum[0])
135n/a finally: lnum[0] += 1
136n/a vars = scanvars(reader, frame, locals)
137n/a
138n/a rows = ['<tr><td bgcolor="#d8bbff">%s%s %s</td></tr>' %
139n/a ('<big>&nbsp;</big>', link, call)]
140n/a if index is not None:
141n/a i = lnum - index
142n/a for line in lines:
143n/a num = small('&nbsp;' * (5-len(str(i))) + str(i)) + '&nbsp;'
144n/a if i in highlight:
145n/a line = '<tt>=&gt;%s%s</tt>' % (num, pydoc.html.preformat(line))
146n/a rows.append('<tr><td bgcolor="#ffccee">%s</td></tr>' % line)
147n/a else:
148n/a line = '<tt>&nbsp;&nbsp;%s%s</tt>' % (num, pydoc.html.preformat(line))
149n/a rows.append('<tr><td>%s</td></tr>' % grey(line))
150n/a i += 1
151n/a
152n/a done, dump = {}, []
153n/a for name, where, value in vars:
154n/a if name in done: continue
155n/a done[name] = 1
156n/a if value is not __UNDEF__:
157n/a if where in ('global', 'builtin'):
158n/a name = ('<em>%s</em> ' % where) + strong(name)
159n/a elif where == 'local':
160n/a name = strong(name)
161n/a else:
162n/a name = where + strong(name.split('.')[-1])
163n/a dump.append('%s&nbsp;= %s' % (name, pydoc.html.repr(value)))
164n/a else:
165n/a dump.append(name + ' <em>undefined</em>')
166n/a
167n/a rows.append('<tr><td>%s</td></tr>' % small(grey(', '.join(dump))))
168n/a frames.append('''
169n/a<table width="100%%" cellspacing=0 cellpadding=0 border=0>
170n/a%s</table>''' % '\n'.join(rows))
171n/a
172n/a exception = ['<p>%s: %s' % (strong(pydoc.html.escape(str(etype))),
173n/a pydoc.html.escape(str(evalue)))]
174n/a for name in dir(evalue):
175n/a if name[:1] == '_': continue
176n/a value = pydoc.html.repr(getattr(evalue, name))
177n/a exception.append('\n<br>%s%s&nbsp;=\n%s' % (indent, name, value))
178n/a
179n/a return head + ''.join(frames) + ''.join(exception) + '''
180n/a
181n/a
182n/a<!-- The above is a description of an error in a Python program, formatted
183n/a for a Web browser because the 'cgitb' module was enabled. In case you
184n/a are not reading this in a Web browser, here is the original traceback:
185n/a
186n/a%s
187n/a-->
188n/a''' % pydoc.html.escape(
189n/a ''.join(traceback.format_exception(etype, evalue, etb)))
190n/a
191n/adef text(einfo, context=5):
192n/a """Return a plain text document describing a given traceback."""
193n/a etype, evalue, etb = einfo
194n/a if isinstance(etype, type):
195n/a etype = etype.__name__
196n/a pyver = 'Python ' + sys.version.split()[0] + ': ' + sys.executable
197n/a date = time.ctime(time.time())
198n/a head = "%s\n%s\n%s\n" % (str(etype), pyver, date) + '''
199n/aA problem occurred in a Python script. Here is the sequence of
200n/afunction calls leading up to the error, in the order they occurred.
201n/a'''
202n/a
203n/a frames = []
204n/a records = inspect.getinnerframes(etb, context)
205n/a for frame, file, lnum, func, lines, index in records:
206n/a file = file and os.path.abspath(file) or '?'
207n/a args, varargs, varkw, locals = inspect.getargvalues(frame)
208n/a call = ''
209n/a if func != '?':
210n/a call = 'in ' + func + \
211n/a inspect.formatargvalues(args, varargs, varkw, locals,
212n/a formatvalue=lambda value: '=' + pydoc.text.repr(value))
213n/a
214n/a highlight = {}
215n/a def reader(lnum=[lnum]):
216n/a highlight[lnum[0]] = 1
217n/a try: return linecache.getline(file, lnum[0])
218n/a finally: lnum[0] += 1
219n/a vars = scanvars(reader, frame, locals)
220n/a
221n/a rows = [' %s %s' % (file, call)]
222n/a if index is not None:
223n/a i = lnum - index
224n/a for line in lines:
225n/a num = '%5d ' % i
226n/a rows.append(num+line.rstrip())
227n/a i += 1
228n/a
229n/a done, dump = {}, []
230n/a for name, where, value in vars:
231n/a if name in done: continue
232n/a done[name] = 1
233n/a if value is not __UNDEF__:
234n/a if where == 'global': name = 'global ' + name
235n/a elif where != 'local': name = where + name.split('.')[-1]
236n/a dump.append('%s = %s' % (name, pydoc.text.repr(value)))
237n/a else:
238n/a dump.append(name + ' undefined')
239n/a
240n/a rows.append('\n'.join(dump))
241n/a frames.append('\n%s\n' % '\n'.join(rows))
242n/a
243n/a exception = ['%s: %s' % (str(etype), str(evalue))]
244n/a for name in dir(evalue):
245n/a value = pydoc.text.repr(getattr(evalue, name))
246n/a exception.append('\n%s%s = %s' % (" "*4, name, value))
247n/a
248n/a return head + ''.join(frames) + ''.join(exception) + '''
249n/a
250n/aThe above is a description of an error in a Python program. Here is
251n/athe original traceback:
252n/a
253n/a%s
254n/a''' % ''.join(traceback.format_exception(etype, evalue, etb))
255n/a
256n/aclass Hook:
257n/a """A hook to replace sys.excepthook that shows tracebacks in HTML."""
258n/a
259n/a def __init__(self, display=1, logdir=None, context=5, file=None,
260n/a format="html"):
261n/a self.display = display # send tracebacks to browser if true
262n/a self.logdir = logdir # log tracebacks to files if not None
263n/a self.context = context # number of source code lines per frame
264n/a self.file = file or sys.stdout # place to send the output
265n/a self.format = format
266n/a
267n/a def __call__(self, etype, evalue, etb):
268n/a self.handle((etype, evalue, etb))
269n/a
270n/a def handle(self, info=None):
271n/a info = info or sys.exc_info()
272n/a if self.format == "html":
273n/a self.file.write(reset())
274n/a
275n/a formatter = (self.format=="html") and html or text
276n/a plain = False
277n/a try:
278n/a doc = formatter(info, self.context)
279n/a except: # just in case something goes wrong
280n/a doc = ''.join(traceback.format_exception(*info))
281n/a plain = True
282n/a
283n/a if self.display:
284n/a if plain:
285n/a doc = doc.replace('&', '&amp;').replace('<', '&lt;')
286n/a self.file.write('<pre>' + doc + '</pre>\n')
287n/a else:
288n/a self.file.write(doc + '\n')
289n/a else:
290n/a self.file.write('<p>A problem occurred in a Python script.\n')
291n/a
292n/a if self.logdir is not None:
293n/a suffix = ['.txt', '.html'][self.format=="html"]
294n/a (fd, path) = tempfile.mkstemp(suffix=suffix, dir=self.logdir)
295n/a
296n/a try:
297n/a with os.fdopen(fd, 'w') as file:
298n/a file.write(doc)
299n/a msg = '%s contains the description of this error.' % path
300n/a except:
301n/a msg = 'Tried to save traceback to %s, but failed.' % path
302n/a
303n/a if self.format == 'html':
304n/a self.file.write('<p>%s</p>\n' % msg)
305n/a else:
306n/a self.file.write(msg + '\n')
307n/a try:
308n/a self.file.flush()
309n/a except: pass
310n/a
311n/ahandler = Hook().handle
312n/adef enable(display=1, logdir=None, context=5, format="html"):
313n/a """Install an exception handler that formats tracebacks as HTML.
314n/a
315n/a The optional argument 'display' can be set to 0 to suppress sending the
316n/a traceback to the browser, and 'logdir' can be set to a directory to cause
317n/a tracebacks to be written to files there."""
318n/a sys.excepthook = Hook(display=display, logdir=logdir,
319n/a context=context, format=format)