Skip to content

Commit 53374cc

Browse files
bpo-30579: Docs for dynamic traceback creation (GH-5653)
(cherry picked from commit aec7532) Co-authored-by: Nick Coghlan <ncoghlan@gmail.com>
1 parent 09819ef commit 53374cc

File tree

3 files changed

+48
-13
lines changed

3 files changed

+48
-13
lines changed

Doc/library/types.rst

+8-1
Original file line numberDiff line numberDiff line change
@@ -198,16 +198,23 @@ Standard names are defined for the following types:
198198
Defaults to ``None``. Previously the attribute was optional.
199199

200200

201-
.. data:: TracebackType
201+
.. class:: TracebackType(tb_next, tb_frame, tb_lasti, tb_lineno)
202202

203203
The type of traceback objects such as found in ``sys.exc_info()[2]``.
204204

205+
See :ref:`the language reference <traceback-objects>` for details of the
206+
available attributes and operations, and guidance on creating tracebacks
207+
dynamically.
208+
205209

206210
.. data:: FrameType
207211

208212
The type of frame objects such as found in ``tb.tb_frame`` if ``tb`` is a
209213
traceback object.
210214

215+
See :ref:`the language reference <frame-objects>` for details of the
216+
available attributes and operations.
217+
211218

212219
.. data:: GetSetDescriptorType
213220

Doc/reference/datamodel.rst

+34-12
Original file line numberDiff line numberDiff line change
@@ -950,7 +950,7 @@ Internal types
950950
.. index:: object: frame
951951

952952
Frame objects represent execution frames. They may occur in traceback objects
953-
(see below).
953+
(see below), and are also passed to registered trace functions.
954954

955955
.. index::
956956
single: f_back (frame attribute)
@@ -1003,6 +1003,8 @@ Internal types
10031003

10041004
.. versionadded:: 3.4
10051005

1006+
.. _traceback-objects:
1007+
10061008
Traceback objects
10071009
.. index::
10081010
object: traceback
@@ -1015,31 +1017,51 @@ Internal types
10151017
single: sys.last_traceback
10161018

10171019
Traceback objects represent a stack trace of an exception. A traceback object
1018-
is created when an exception occurs. When the search for an exception handler
1020+
is implicitly created when an exception occurs, and may also be explicitly
1021+
created by calling :class:`types.TracebackType`.
1022+
1023+
For implicitly created tracebacks, when the search for an exception handler
10191024
unwinds the execution stack, at each unwound level a traceback object is
10201025
inserted in front of the current traceback. When an exception handler is
10211026
entered, the stack trace is made available to the program. (See section
10221027
:ref:`try`.) It is accessible as the third item of the
1023-
tuple returned by ``sys.exc_info()``. When the program contains no suitable
1028+
tuple returned by ``sys.exc_info()``, and as the ``__traceback__`` attribute
1029+
of the caught exception.
1030+
1031+
When the program contains no suitable
10241032
handler, the stack trace is written (nicely formatted) to the standard error
10251033
stream; if the interpreter is interactive, it is also made available to the user
10261034
as ``sys.last_traceback``.
10271035

1036+
For explicitly created tracebacks, it is up to the creator of the traceback
1037+
to determine how the ``tb_next`` attributes should be linked to form a
1038+
full stack trace.
1039+
10281040
.. index::
1029-
single: tb_next (traceback attribute)
10301041
single: tb_frame (traceback attribute)
10311042
single: tb_lineno (traceback attribute)
10321043
single: tb_lasti (traceback attribute)
10331044
statement: try
10341045

1035-
Special read-only attributes: :attr:`tb_next` is the next level in the stack
1036-
trace (towards the frame where the exception occurred), or ``None`` if there is
1037-
no next level; :attr:`tb_frame` points to the execution frame of the current
1038-
level; :attr:`tb_lineno` gives the line number where the exception occurred;
1039-
:attr:`tb_lasti` indicates the precise instruction. The line number and last
1040-
instruction in the traceback may differ from the line number of its frame object
1041-
if the exception occurred in a :keyword:`try` statement with no matching except
1042-
clause or with a finally clause.
1046+
Special read-only attributes:
1047+
:attr:`tb_frame` points to the execution frame of the current level;
1048+
:attr:`tb_lineno` gives the line number where the exception occurred;
1049+
:attr:`tb_lasti` indicates the precise instruction.
1050+
The line number and last instruction in the traceback may differ from the
1051+
line number of its frame object if the exception occurred in a
1052+
:keyword:`try` statement with no matching except clause or with a
1053+
finally clause.
1054+
1055+
.. index::
1056+
single: tb_next (traceback attribute)
1057+
1058+
Special writable attribute: :attr:`tb_next` is the next level in the stack
1059+
trace (towards the frame where the exception occurred), or ``None`` if
1060+
there is no next level.
1061+
1062+
.. versionchanged:: 3.7
1063+
Traceback objects can now be explicitly instantiated from Python code,
1064+
and the ``tb_next`` attribute of existing instances can be updated.
10431065

10441066
Slice objects
10451067
.. index:: builtin: slice

Doc/whatsnew/3.7.rst

+6
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,12 @@ Other Language Changes
398398
``format(str(self), '')``.
399399
(Contributed by Serhiy Storchaka in :issue:`28974`.)
400400

401+
* In order to better support dynamic creation of stack traces,
402+
:class:`types.TracebackType` can now be instantiated from Python code, and
403+
the ``tb_next`` attribute on :ref:`tracebacks <traceback-objects>` is now
404+
writable.
405+
(Contributed by Nathaniel J. Smith in :issue:`30579`.)
406+
401407

402408
New Modules
403409
===========

0 commit comments

Comments
 (0)