Teks sumber — English
Terjemahan — Bahasa Indonesia
This module provides a standard interface to extract, format and print stack traces of Python programs. It is more flexible than the interpreter's default traceback display, and therefore makes it possible to configure certain aspects of the output. Finally, it contains a utility for capturing enough information about an exception to print it later, without the need to save a reference to the actual exception. Since exceptions can be the roots of large objects graph, this utility can significantly improve memory management.
The module uses :ref:`traceback objects <traceback-objects>` --- these are objects of type :class:`types.TracebackType`, which are assigned to the :attr:`~BaseException.__traceback__` field of :class:`BaseException` instances.
:class:`TracebackException` class and its helper classes :class:`StackSummary` and :class:`FrameSummary`. These offer both more flexibility in the output generated and the ability to store the information necessary for later formatting without holding references to actual exception and traceback objects.
Print up to *limit* stack trace entries from :ref:`traceback object <traceback-objects>` *tb* (starting from the caller's frame) if *limit* is positive. Otherwise, print the last ``abs(limit)`` entries. If *limit* is omitted or ``None``, all entries are printed. If *file* is omitted or ``None``, the output goes to :data:`sys.stderr`; otherwise it should be an open :term:`file <file object>` or :term:`file-like object` to receive the output.
Print exception information and stack trace entries from :ref:`traceback object <traceback-objects>` *tb* to *file*. This differs from :func:`print_tb` in the following ways:
Since Python 3.10, instead of passing *value* and *tb*, an exception object can be passed as the first argument. If *value* and *tb* are provided, the first argument is ignored in order to provide backwards compatibility.
Print up to *limit* stack trace entries (starting from the invocation point) if *limit* is positive. Otherwise, print the last ``abs(limit)`` entries. If *limit* is omitted or ``None``, all entries are printed. The optional *f* argument can be used to specify an alternate :ref:`stack frame <frame-objects>` to start. The optional *file* argument has the same meaning as for :func:`print_tb`.
Return a :class:`StackSummary` object representing a list of "pre-processed" stack trace entries extracted from the :ref:`traceback object <traceback-objects>` *tb*. It is useful for alternate formatting of stack traces. The optional *limit* argument has the same meaning as for :func:`print_tb`. A "pre-processed" stack trace entry is a :class:`FrameSummary` object with attributes representing the information that is usually printed for a stack trace.
Extract the raw traceback from the current :ref:`stack frame <frame-objects>`. The return value has the same format as for :func:`extract_tb`. The optional *f* and *limit* arguments have the same meaning as for :func:`print_stack`.
Print a list of :class:`FrameSummary` objects as returned by :func:`extract_tb` or :func:`extract_stack`, or a list of old-style tuples (see :meth:`StackSummary.from_list`), as a formatted stack trace to the given file. If *file* is ``None``, the output is written to :data:`sys.stderr`.
Given a list of :class:`FrameSummary` objects as returned by :func:`extract_tb` or :func:`extract_stack`, or a list of old-style tuples (see :meth:`StackSummary.from_list`), return a list of strings ready for printing. Each string in the resulting list corresponds to the item with the same index in the argument list. Each string ends in a newline; the strings may contain internal newlines as well, for those items whose source text line is not ``None``.
Since Python 3.10, instead of passing *value*, an exception object can be passed as the first argument. If *value* is provided, the first argument is ignored in order to provide backwards compatibility.
Clears the local variables of all the stack frames in a :ref:`traceback <traceback-objects>` *tb* by calling the :meth:`~frame.clear` method of each :ref:`frame object <frame-objects>`.
:class:`!TracebackException` Objects
:class:`!TracebackException` objects are created from actual exceptions to capture data for later printing. They offer a more lightweight method of storing this information by avoiding holding references to :ref:`traceback<traceback-objects>` and :ref:`frame<frame-objects>` objects. In addition, they expose more options to configure the output compared to the module-level functions described above.
:class:`!StackSummary` Objects
:class:`!StackSummary` objects represent a call stack ready for formatting.
Construct a :class:`!StackSummary` object from a frame generator (such as is returned by :func:`~traceback.walk_stack` or :func:`~traceback.walk_tb`).
If *limit* is supplied, only this many frames are taken from *frame_gen*. If *lookup_lines* is ``False``, the returned :class:`FrameSummary` objects will not have read their lines in yet, making the cost of creating the :class:`!StackSummary` cheaper (which may be valuable if it may not actually get formatted). If *capture_locals* is ``True`` the local variables in each :class:`!FrameSummary` are captured as object representations.
Construct a :class:`!StackSummary` object from a supplied list of :class:`FrameSummary` objects or old-style list of tuples. Each tuple should be a 4-tuple with *filename*, *lineno*, *name*, *line* as the elements.
Returns a list of strings ready for printing. Each string in the resulting list corresponds to a single :ref:`frame <frame-objects>` from the stack. Each string ends in a newline; the strings may contain internal newlines as well, for those items with source text lines.
Returns a string for printing one of the :ref:`frames <frame-objects>` involved in the stack. This method is called for each :class:`FrameSummary` object to be printed by :meth:`StackSummary.format`. If it returns ``None``, the frame is omitted from the output.
:class:`!FrameSummary` Objects
A :class:`!FrameSummary` object represents a single :ref:`frame <frame-objects>` in a :ref:`traceback <traceback-objects>`.
Represents a single :ref:`frame <frame-objects>` in the :ref:`traceback <traceback-objects>` or stack that is being formatted or printed. It may optionally have a stringified version of the frame's locals included in it. If *lookup_line* is ``False``, the source code is not looked up until the :class:`!FrameSummary` has the :attr:`~FrameSummary.line` attribute accessed (which also happens when casting it to a :class:`tuple`). :attr:`~FrameSummary.line` may be directly provided, and will prevent line lookups happening at all. *locals* is an optional local variable mapping, and if supplied the variable representations are stored in the summary for later display.
The filename of the source code for this frame. Equivalent to accessing :attr:`f.f_code.co_filename <codeobject.co_filename>` on a :ref:`frame object <frame-objects>` *f*.
Equivalent to accessing :attr:`f.f_code.co_name <codeobject.co_name>` on a :ref:`frame object <frame-objects>` *f*.
object
objek