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

Python code coverage for Lib/traceback.py

#countcontent
1n/a"""Extract, format and print information about Python stack traces."""
2n/a
3n/aimport collections
4n/aimport itertools
5n/aimport linecache
6n/aimport sys
7n/a
8n/a__all__ = ['extract_stack', 'extract_tb', 'format_exception',
9n/a 'format_exception_only', 'format_list', 'format_stack',
10n/a 'format_tb', 'print_exc', 'format_exc', 'print_exception',
11n/a 'print_last', 'print_stack', 'print_tb', 'clear_frames',
12n/a 'FrameSummary', 'StackSummary', 'TracebackException',
13n/a 'walk_stack', 'walk_tb']
14n/a
15n/a#
16n/a# Formatting and printing lists of traceback lines.
17n/a#
18n/a
19n/adef print_list(extracted_list, file=None):
20n/a """Print the list of tuples as returned by extract_tb() or
21n/a extract_stack() as a formatted stack trace to the given file."""
22n/a if file is None:
23n/a file = sys.stderr
24n/a for item in StackSummary.from_list(extracted_list).format():
25n/a print(item, file=file, end="")
26n/a
27n/adef format_list(extracted_list):
28n/a """Format a list of traceback entry tuples for printing.
29n/a
30n/a Given a list of tuples as returned by extract_tb() or
31n/a extract_stack(), return a list of strings ready for printing.
32n/a Each string in the resulting list corresponds to the item with the
33n/a same index in the argument list. Each string ends in a newline;
34n/a the strings may contain internal newlines as well, for those items
35n/a whose source text line is not None.
36n/a """
37n/a return StackSummary.from_list(extracted_list).format()
38n/a
39n/a#
40n/a# Printing and Extracting Tracebacks.
41n/a#
42n/a
43n/adef print_tb(tb, limit=None, file=None):
44n/a """Print up to 'limit' stack trace entries from the traceback 'tb'.
45n/a
46n/a If 'limit' is omitted or None, all entries are printed. If 'file'
47n/a is omitted or None, the output goes to sys.stderr; otherwise
48n/a 'file' should be an open file or file-like object with a write()
49n/a method.
50n/a """
51n/a print_list(extract_tb(tb, limit=limit), file=file)
52n/a
53n/adef format_tb(tb, limit=None):
54n/a """A shorthand for 'format_list(extract_tb(tb, limit))'."""
55n/a return extract_tb(tb, limit=limit).format()
56n/a
57n/adef extract_tb(tb, limit=None):
58n/a """Return list of up to limit pre-processed entries from traceback.
59n/a
60n/a This is useful for alternate formatting of stack traces. If
61n/a 'limit' is omitted or None, all entries are extracted. A
62n/a pre-processed stack trace entry is a quadruple (filename, line
63n/a number, function name, text) representing the information that is
64n/a usually printed for a stack trace. The text is a string with
65n/a leading and trailing whitespace stripped; if the source is not
66n/a available it is None.
67n/a """
68n/a return StackSummary.extract(walk_tb(tb), limit=limit)
69n/a
70n/a#
71n/a# Exception formatting and output.
72n/a#
73n/a
74n/a_cause_message = (
75n/a "\nThe above exception was the direct cause "
76n/a "of the following exception:\n\n")
77n/a
78n/a_context_message = (
79n/a "\nDuring handling of the above exception, "
80n/a "another exception occurred:\n\n")
81n/a
82n/a
83n/adef print_exception(etype, value, tb, limit=None, file=None, chain=True):
84n/a """Print exception up to 'limit' stack trace entries from 'tb' to 'file'.
85n/a
86n/a This differs from print_tb() in the following ways: (1) if
87n/a traceback is not None, it prints a header "Traceback (most recent
88n/a call last):"; (2) it prints the exception type and value after the
89n/a stack trace; (3) if type is SyntaxError and value has the
90n/a appropriate format, it prints the line where the syntax error
91n/a occurred with a caret on the next line indicating the approximate
92n/a position of the error.
93n/a """
94n/a # format_exception has ignored etype for some time, and code such as cgitb
95n/a # passes in bogus values as a result. For compatibility with such code we
96n/a # ignore it here (rather than in the new TracebackException API).
97n/a if file is None:
98n/a file = sys.stderr
99n/a for line in TracebackException(
100n/a type(value), value, tb, limit=limit).format(chain=chain):
101n/a print(line, file=file, end="")
102n/a
103n/a
104n/adef format_exception(etype, value, tb, limit=None, chain=True):
105n/a """Format a stack trace and the exception information.
106n/a
107n/a The arguments have the same meaning as the corresponding arguments
108n/a to print_exception(). The return value is a list of strings, each
109n/a ending in a newline and some containing internal newlines. When
110n/a these lines are concatenated and printed, exactly the same text is
111n/a printed as does print_exception().
112n/a """
113n/a # format_exception has ignored etype for some time, and code such as cgitb
114n/a # passes in bogus values as a result. For compatibility with such code we
115n/a # ignore it here (rather than in the new TracebackException API).
116n/a return list(TracebackException(
117n/a type(value), value, tb, limit=limit).format(chain=chain))
118n/a
119n/a
120n/adef format_exception_only(etype, value):
121n/a """Format the exception part of a traceback.
122n/a
123n/a The arguments are the exception type and value such as given by
124n/a sys.last_type and sys.last_value. The return value is a list of
125n/a strings, each ending in a newline.
126n/a
127n/a Normally, the list contains a single string; however, for
128n/a SyntaxError exceptions, it contains several lines that (when
129n/a printed) display detailed information about where the syntax
130n/a error occurred.
131n/a
132n/a The message indicating which exception occurred is always the last
133n/a string in the list.
134n/a
135n/a """
136n/a return list(TracebackException(etype, value, None).format_exception_only())
137n/a
138n/a
139n/a# -- not official API but folk probably use these two functions.
140n/a
141n/adef _format_final_exc_line(etype, value):
142n/a valuestr = _some_str(value)
143n/a if value is None or not valuestr:
144n/a line = "%s\n" % etype
145n/a else:
146n/a line = "%s: %s\n" % (etype, valuestr)
147n/a return line
148n/a
149n/adef _some_str(value):
150n/a try:
151n/a return str(value)
152n/a except:
153n/a return '<unprintable %s object>' % type(value).__name__
154n/a
155n/a# --
156n/a
157n/adef print_exc(limit=None, file=None, chain=True):
158n/a """Shorthand for 'print_exception(*sys.exc_info(), limit, file)'."""
159n/a print_exception(*sys.exc_info(), limit=limit, file=file, chain=chain)
160n/a
161n/adef format_exc(limit=None, chain=True):
162n/a """Like print_exc() but return a string."""
163n/a return "".join(format_exception(*sys.exc_info(), limit=limit, chain=chain))
164n/a
165n/adef print_last(limit=None, file=None, chain=True):
166n/a """This is a shorthand for 'print_exception(sys.last_type,
167n/a sys.last_value, sys.last_traceback, limit, file)'."""
168n/a if not hasattr(sys, "last_type"):
169n/a raise ValueError("no last exception")
170n/a print_exception(sys.last_type, sys.last_value, sys.last_traceback,
171n/a limit, file, chain)
172n/a
173n/a#
174n/a# Printing and Extracting Stacks.
175n/a#
176n/a
177n/adef print_stack(f=None, limit=None, file=None):
178n/a """Print a stack trace from its invocation point.
179n/a
180n/a The optional 'f' argument can be used to specify an alternate
181n/a stack frame at which to start. The optional 'limit' and 'file'
182n/a arguments have the same meaning as for print_exception().
183n/a """
184n/a if f is None:
185n/a f = sys._getframe().f_back
186n/a print_list(extract_stack(f, limit=limit), file=file)
187n/a
188n/a
189n/adef format_stack(f=None, limit=None):
190n/a """Shorthand for 'format_list(extract_stack(f, limit))'."""
191n/a if f is None:
192n/a f = sys._getframe().f_back
193n/a return format_list(extract_stack(f, limit=limit))
194n/a
195n/a
196n/adef extract_stack(f=None, limit=None):
197n/a """Extract the raw traceback from the current stack frame.
198n/a
199n/a The return value has the same format as for extract_tb(). The
200n/a optional 'f' and 'limit' arguments have the same meaning as for
201n/a print_stack(). Each item in the list is a quadruple (filename,
202n/a line number, function name, text), and the entries are in order
203n/a from oldest to newest stack frame.
204n/a """
205n/a if f is None:
206n/a f = sys._getframe().f_back
207n/a stack = StackSummary.extract(walk_stack(f), limit=limit)
208n/a stack.reverse()
209n/a return stack
210n/a
211n/a
212n/adef clear_frames(tb):
213n/a "Clear all references to local variables in the frames of a traceback."
214n/a while tb is not None:
215n/a try:
216n/a tb.tb_frame.clear()
217n/a except RuntimeError:
218n/a # Ignore the exception raised if the frame is still executing.
219n/a pass
220n/a tb = tb.tb_next
221n/a
222n/a
223n/aclass FrameSummary:
224n/a """A single frame from a traceback.
225n/a
226n/a - :attr:`filename` The filename for the frame.
227n/a - :attr:`lineno` The line within filename for the frame that was
228n/a active when the frame was captured.
229n/a - :attr:`name` The name of the function or method that was executing
230n/a when the frame was captured.
231n/a - :attr:`line` The text from the linecache module for the
232n/a of code that was running when the frame was captured.
233n/a - :attr:`locals` Either None if locals were not supplied, or a dict
234n/a mapping the name to the repr() of the variable.
235n/a """
236n/a
237n/a __slots__ = ('filename', 'lineno', 'name', '_line', 'locals')
238n/a
239n/a def __init__(self, filename, lineno, name, *, lookup_line=True,
240n/a locals=None, line=None):
241n/a """Construct a FrameSummary.
242n/a
243n/a :param lookup_line: If True, `linecache` is consulted for the source
244n/a code line. Otherwise, the line will be looked up when first needed.
245n/a :param locals: If supplied the frame locals, which will be captured as
246n/a object representations.
247n/a :param line: If provided, use this instead of looking up the line in
248n/a the linecache.
249n/a """
250n/a self.filename = filename
251n/a self.lineno = lineno
252n/a self.name = name
253n/a self._line = line
254n/a if lookup_line:
255n/a self.line
256n/a self.locals = \
257n/a dict((k, repr(v)) for k, v in locals.items()) if locals else None
258n/a
259n/a def __eq__(self, other):
260n/a if isinstance(other, FrameSummary):
261n/a return (self.filename == other.filename and
262n/a self.lineno == other.lineno and
263n/a self.name == other.name and
264n/a self.locals == other.locals)
265n/a if isinstance(other, tuple):
266n/a return (self.filename, self.lineno, self.name, self.line) == other
267n/a return NotImplemented
268n/a
269n/a def __getitem__(self, pos):
270n/a return (self.filename, self.lineno, self.name, self.line)[pos]
271n/a
272n/a def __iter__(self):
273n/a return iter([self.filename, self.lineno, self.name, self.line])
274n/a
275n/a def __repr__(self):
276n/a return "<FrameSummary file {filename}, line {lineno} in {name}>".format(
277n/a filename=self.filename, lineno=self.lineno, name=self.name)
278n/a
279n/a @property
280n/a def line(self):
281n/a if self._line is None:
282n/a self._line = linecache.getline(self.filename, self.lineno).strip()
283n/a return self._line
284n/a
285n/a
286n/adef walk_stack(f):
287n/a """Walk a stack yielding the frame and line number for each frame.
288n/a
289n/a This will follow f.f_back from the given frame. If no frame is given, the
290n/a current stack is used. Usually used with StackSummary.extract.
291n/a """
292n/a if f is None:
293n/a f = sys._getframe().f_back.f_back
294n/a while f is not None:
295n/a yield f, f.f_lineno
296n/a f = f.f_back
297n/a
298n/a
299n/adef walk_tb(tb):
300n/a """Walk a traceback yielding the frame and line number for each frame.
301n/a
302n/a This will follow tb.tb_next (and thus is in the opposite order to
303n/a walk_stack). Usually used with StackSummary.extract.
304n/a """
305n/a while tb is not None:
306n/a yield tb.tb_frame, tb.tb_lineno
307n/a tb = tb.tb_next
308n/a
309n/a
310n/aclass StackSummary(list):
311n/a """A stack of frames."""
312n/a
313n/a @classmethod
314n/a def extract(klass, frame_gen, *, limit=None, lookup_lines=True,
315n/a capture_locals=False):
316n/a """Create a StackSummary from a traceback or stack object.
317n/a
318n/a :param frame_gen: A generator that yields (frame, lineno) tuples to
319n/a include in the stack.
320n/a :param limit: None to include all frames or the number of frames to
321n/a include.
322n/a :param lookup_lines: If True, lookup lines for each frame immediately,
323n/a otherwise lookup is deferred until the frame is rendered.
324n/a :param capture_locals: If True, the local variables from each frame will
325n/a be captured as object representations into the FrameSummary.
326n/a """
327n/a if limit is None:
328n/a limit = getattr(sys, 'tracebacklimit', None)
329n/a if limit is not None and limit < 0:
330n/a limit = 0
331n/a if limit is not None:
332n/a if limit >= 0:
333n/a frame_gen = itertools.islice(frame_gen, limit)
334n/a else:
335n/a frame_gen = collections.deque(frame_gen, maxlen=-limit)
336n/a
337n/a result = klass()
338n/a fnames = set()
339n/a for f, lineno in frame_gen:
340n/a co = f.f_code
341n/a filename = co.co_filename
342n/a name = co.co_name
343n/a
344n/a fnames.add(filename)
345n/a linecache.lazycache(filename, f.f_globals)
346n/a # Must defer line lookups until we have called checkcache.
347n/a if capture_locals:
348n/a f_locals = f.f_locals
349n/a else:
350n/a f_locals = None
351n/a result.append(FrameSummary(
352n/a filename, lineno, name, lookup_line=False, locals=f_locals))
353n/a for filename in fnames:
354n/a linecache.checkcache(filename)
355n/a # If immediate lookup was desired, trigger lookups now.
356n/a if lookup_lines:
357n/a for f in result:
358n/a f.line
359n/a return result
360n/a
361n/a @classmethod
362n/a def from_list(klass, a_list):
363n/a """Create a StackSummary from a simple list of tuples.
364n/a
365n/a This method supports the older Python API. Each tuple should be a
366n/a 4-tuple with (filename, lineno, name, line) elements.
367n/a """
368n/a # While doing a fast-path check for isinstance(a_list, StackSummary) is
369n/a # appealing, idlelib.run.cleanup_traceback and other similar code may
370n/a # break this by making arbitrary frames plain tuples, so we need to
371n/a # check on a frame by frame basis.
372n/a result = StackSummary()
373n/a for frame in a_list:
374n/a if isinstance(frame, FrameSummary):
375n/a result.append(frame)
376n/a else:
377n/a filename, lineno, name, line = frame
378n/a result.append(FrameSummary(filename, lineno, name, line=line))
379n/a return result
380n/a
381n/a def format(self):
382n/a """Format the stack ready for printing.
383n/a
384n/a Returns a list of strings ready for printing. Each string in the
385n/a resulting list corresponds to a single frame from the stack.
386n/a Each string ends in a newline; the strings may contain internal
387n/a newlines as well, for those items with source text lines.
388n/a
389n/a For long sequences of the same frame and line, the first few
390n/a repetitions are shown, followed by a summary line stating the exact
391n/a number of further repetitions.
392n/a """
393n/a result = []
394n/a last_file = None
395n/a last_line = None
396n/a last_name = None
397n/a count = 0
398n/a for frame in self:
399n/a if (last_file is not None and last_file == frame.filename and
400n/a last_line is not None and last_line == frame.lineno and
401n/a last_name is not None and last_name == frame.name):
402n/a count += 1
403n/a else:
404n/a if count > 3:
405n/a result.append(f' [Previous line repeated {count-3} more times]\n')
406n/a last_file = frame.filename
407n/a last_line = frame.lineno
408n/a last_name = frame.name
409n/a count = 0
410n/a if count >= 3:
411n/a continue
412n/a row = []
413n/a row.append(' File "{}", line {}, in {}\n'.format(
414n/a frame.filename, frame.lineno, frame.name))
415n/a if frame.line:
416n/a row.append(' {}\n'.format(frame.line.strip()))
417n/a if frame.locals:
418n/a for name, value in sorted(frame.locals.items()):
419n/a row.append(' {name} = {value}\n'.format(name=name, value=value))
420n/a result.append(''.join(row))
421n/a if count > 3:
422n/a result.append(f' [Previous line repeated {count-3} more times]\n')
423n/a return result
424n/a
425n/a
426n/aclass TracebackException:
427n/a """An exception ready for rendering.
428n/a
429n/a The traceback module captures enough attributes from the original exception
430n/a to this intermediary form to ensure that no references are held, while
431n/a still being able to fully print or format it.
432n/a
433n/a Use `from_exception` to create TracebackException instances from exception
434n/a objects, or the constructor to create TracebackException instances from
435n/a individual components.
436n/a
437n/a - :attr:`__cause__` A TracebackException of the original *__cause__*.
438n/a - :attr:`__context__` A TracebackException of the original *__context__*.
439n/a - :attr:`__suppress_context__` The *__suppress_context__* value from the
440n/a original exception.
441n/a - :attr:`stack` A `StackSummary` representing the traceback.
442n/a - :attr:`exc_type` The class of the original traceback.
443n/a - :attr:`filename` For syntax errors - the filename where the error
444n/a occurred.
445n/a - :attr:`lineno` For syntax errors - the linenumber where the error
446n/a occurred.
447n/a - :attr:`text` For syntax errors - the text where the error
448n/a occurred.
449n/a - :attr:`offset` For syntax errors - the offset into the text where the
450n/a error occurred.
451n/a - :attr:`msg` For syntax errors - the compiler error message.
452n/a """
453n/a
454n/a def __init__(self, exc_type, exc_value, exc_traceback, *, limit=None,
455n/a lookup_lines=True, capture_locals=False, _seen=None):
456n/a # NB: we need to accept exc_traceback, exc_value, exc_traceback to
457n/a # permit backwards compat with the existing API, otherwise we
458n/a # need stub thunk objects just to glue it together.
459n/a # Handle loops in __cause__ or __context__.
460n/a if _seen is None:
461n/a _seen = set()
462n/a _seen.add(exc_value)
463n/a # Gracefully handle (the way Python 2.4 and earlier did) the case of
464n/a # being called with no type or value (None, None, None).
465n/a if (exc_value and exc_value.__cause__ is not None
466n/a and exc_value.__cause__ not in _seen):
467n/a cause = TracebackException(
468n/a type(exc_value.__cause__),
469n/a exc_value.__cause__,
470n/a exc_value.__cause__.__traceback__,
471n/a limit=limit,
472n/a lookup_lines=False,
473n/a capture_locals=capture_locals,
474n/a _seen=_seen)
475n/a else:
476n/a cause = None
477n/a if (exc_value and exc_value.__context__ is not None
478n/a and exc_value.__context__ not in _seen):
479n/a context = TracebackException(
480n/a type(exc_value.__context__),
481n/a exc_value.__context__,
482n/a exc_value.__context__.__traceback__,
483n/a limit=limit,
484n/a lookup_lines=False,
485n/a capture_locals=capture_locals,
486n/a _seen=_seen)
487n/a else:
488n/a context = None
489n/a self.exc_traceback = exc_traceback
490n/a self.__cause__ = cause
491n/a self.__context__ = context
492n/a self.__suppress_context__ = \
493n/a exc_value.__suppress_context__ if exc_value else False
494n/a # TODO: locals.
495n/a self.stack = StackSummary.extract(
496n/a walk_tb(exc_traceback), limit=limit, lookup_lines=lookup_lines,
497n/a capture_locals=capture_locals)
498n/a self.exc_type = exc_type
499n/a # Capture now to permit freeing resources: only complication is in the
500n/a # unofficial API _format_final_exc_line
501n/a self._str = _some_str(exc_value)
502n/a if exc_type and issubclass(exc_type, SyntaxError):
503n/a # Handle SyntaxError's specially
504n/a self.filename = exc_value.filename
505n/a self.lineno = str(exc_value.lineno)
506n/a self.text = exc_value.text
507n/a self.offset = exc_value.offset
508n/a self.msg = exc_value.msg
509n/a if lookup_lines:
510n/a self._load_lines()
511n/a
512n/a @classmethod
513n/a def from_exception(cls, exc, *args, **kwargs):
514n/a """Create a TracebackException from an exception."""
515n/a return cls(type(exc), exc, exc.__traceback__, *args, **kwargs)
516n/a
517n/a def _load_lines(self):
518n/a """Private API. force all lines in the stack to be loaded."""
519n/a for frame in self.stack:
520n/a frame.line
521n/a if self.__context__:
522n/a self.__context__._load_lines()
523n/a if self.__cause__:
524n/a self.__cause__._load_lines()
525n/a
526n/a def __eq__(self, other):
527n/a return self.__dict__ == other.__dict__
528n/a
529n/a def __str__(self):
530n/a return self._str
531n/a
532n/a def format_exception_only(self):
533n/a """Format the exception part of the traceback.
534n/a
535n/a The return value is a generator of strings, each ending in a newline.
536n/a
537n/a Normally, the generator emits a single string; however, for
538n/a SyntaxError exceptions, it emites several lines that (when
539n/a printed) display detailed information about where the syntax
540n/a error occurred.
541n/a
542n/a The message indicating which exception occurred is always the last
543n/a string in the output.
544n/a """
545n/a if self.exc_type is None:
546n/a yield _format_final_exc_line(None, self._str)
547n/a return
548n/a
549n/a stype = self.exc_type.__qualname__
550n/a smod = self.exc_type.__module__
551n/a if smod not in ("__main__", "builtins"):
552n/a stype = smod + '.' + stype
553n/a
554n/a if not issubclass(self.exc_type, SyntaxError):
555n/a yield _format_final_exc_line(stype, self._str)
556n/a return
557n/a
558n/a # It was a syntax error; show exactly where the problem was found.
559n/a filename = self.filename or "<string>"
560n/a lineno = str(self.lineno) or '?'
561n/a yield ' File "{}", line {}\n'.format(filename, lineno)
562n/a
563n/a badline = self.text
564n/a offset = self.offset
565n/a if badline is not None:
566n/a yield ' {}\n'.format(badline.strip())
567n/a if offset is not None:
568n/a caretspace = badline.rstrip('\n')
569n/a offset = min(len(caretspace), offset) - 1
570n/a caretspace = caretspace[:offset].lstrip()
571n/a # non-space whitespace (likes tabs) must be kept for alignment
572n/a caretspace = ((c.isspace() and c or ' ') for c in caretspace)
573n/a yield ' {}^\n'.format(''.join(caretspace))
574n/a msg = self.msg or "<no detail available>"
575n/a yield "{}: {}\n".format(stype, msg)
576n/a
577n/a def format(self, *, chain=True):
578n/a """Format the exception.
579n/a
580n/a If chain is not *True*, *__cause__* and *__context__* will not be formatted.
581n/a
582n/a The return value is a generator of strings, each ending in a newline and
583n/a some containing internal newlines. `print_exception` is a wrapper around
584n/a this method which just prints the lines to a file.
585n/a
586n/a The message indicating which exception occurred is always the last
587n/a string in the output.
588n/a """
589n/a if chain:
590n/a if self.__cause__ is not None:
591n/a yield from self.__cause__.format(chain=chain)
592n/a yield _cause_message
593n/a elif (self.__context__ is not None and
594n/a not self.__suppress_context__):
595n/a yield from self.__context__.format(chain=chain)
596n/a yield _context_message
597n/a if self.exc_traceback is not None:
598n/a yield 'Traceback (most recent call last):\n'
599n/a yield from self.stack.format()
600n/a yield from self.format_exception_only()