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