Skip to content

Commit 96f64a2

Browse files
authored
pythongh-101100: Improve documentation of TracebackType attributes (python#112884)
1 parent 54410e6 commit 96f64a2

File tree

6 files changed

+52
-33
lines changed

6 files changed

+52
-33
lines changed

Doc/library/stdtypes.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5461,8 +5461,9 @@ It is written as ``NotImplemented``.
54615461
Internal Objects
54625462
----------------
54635463

5464-
See :ref:`types` for this information. It describes stack frame objects,
5465-
traceback objects, and slice objects.
5464+
See :ref:`types` for this information. It describes
5465+
:ref:`stack frame objects <frame-objects>`,
5466+
:ref:`traceback objects <traceback-objects>`, and slice objects.
54665467

54675468

54685469
.. _specialattrs:

Doc/library/traceback.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ interpreter.
1616

1717
.. index:: pair: object; traceback
1818

19-
The module uses traceback objects --- these are objects of type :class:`types.TracebackType`,
19+
The module uses :ref:`traceback objects <traceback-objects>` --- these are
20+
objects of type :class:`types.TracebackType`,
2021
which are assigned to the ``__traceback__`` field of :class:`BaseException` instances.
2122

2223
.. seealso::
@@ -212,7 +213,8 @@ The module defines the following functions:
212213

213214
.. function:: walk_tb(tb)
214215

215-
Walk a traceback following ``tb_next`` yielding the frame and line number
216+
Walk a traceback following :attr:`~traceback.tb_next` yielding the frame and
217+
line number
216218
for each frame. This helper is used with :meth:`StackSummary.extract`.
217219

218220
.. versionadded:: 3.5

Doc/library/types.rst

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -378,11 +378,8 @@ Standard names are defined for the following types:
378378

379379
.. data:: FrameType
380380

381-
The type of frame objects such as found in ``tb.tb_frame`` if ``tb`` is a
382-
traceback object.
383-
384-
See :ref:`the language reference <frame-objects>` for details of the
385-
available attributes and operations.
381+
The type of :ref:`frame objects <frame-objects>` such as found in
382+
:attr:`tb.tb_frame <traceback.tb_frame>` if ``tb`` is a traceback object.
386383

387384

388385
.. data:: GetSetDescriptorType

Doc/reference/datamodel.rst

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,8 +1244,9 @@ Frame objects
12441244

12451245
.. index:: pair: object; frame
12461246

1247-
Frame objects represent execution frames. They may occur in traceback objects
1248-
(see below), and are also passed to registered trace functions.
1247+
Frame objects represent execution frames. They may occur in
1248+
:ref:`traceback objects <traceback-objects>`,
1249+
and are also passed to registered trace functions.
12491250

12501251
.. index::
12511252
single: f_back (frame attribute)
@@ -1357,26 +1358,30 @@ Traceback objects
13571358
single: sys.exception
13581359
single: sys.last_traceback
13591360

1360-
Traceback objects represent a stack trace of an exception. A traceback object
1361+
Traceback objects represent the stack trace of an :ref:`exception <tut-errors>`.
1362+
A traceback object
13611363
is implicitly created when an exception occurs, and may also be explicitly
13621364
created by calling :class:`types.TracebackType`.
13631365

1366+
.. versionchanged:: 3.7
1367+
Traceback objects can now be explicitly instantiated from Python code.
1368+
13641369
For implicitly created tracebacks, when the search for an exception handler
13651370
unwinds the execution stack, at each unwound level a traceback object is
13661371
inserted in front of the current traceback. When an exception handler is
13671372
entered, the stack trace is made available to the program. (See section
13681373
:ref:`try`.) It is accessible as the third item of the
1369-
tuple returned by ``sys.exc_info()``, and as the ``__traceback__`` attribute
1374+
tuple returned by :func:`sys.exc_info`, and as the ``__traceback__`` attribute
13701375
of the caught exception.
13711376

13721377
When the program contains no suitable
13731378
handler, the stack trace is written (nicely formatted) to the standard error
13741379
stream; if the interpreter is interactive, it is also made available to the user
1375-
as ``sys.last_traceback``.
1380+
as :data:`sys.last_traceback`.
13761381

13771382
For explicitly created tracebacks, it is up to the creator of the traceback
1378-
to determine how the ``tb_next`` attributes should be linked to form a
1379-
full stack trace.
1383+
to determine how the :attr:`~traceback.tb_next` attributes should be linked to
1384+
form a full stack trace.
13801385

13811386
.. index::
13821387
single: tb_frame (traceback attribute)
@@ -1385,27 +1390,40 @@ full stack trace.
13851390
pair: statement; try
13861391

13871392
Special read-only attributes:
1388-
:attr:`tb_frame` points to the execution frame of the current level;
1389-
:attr:`tb_lineno` gives the line number where the exception occurred;
1390-
:attr:`tb_lasti` indicates the precise instruction.
1393+
1394+
.. list-table::
1395+
1396+
* - .. attribute:: traceback.tb_frame
1397+
- Points to the execution :ref:`frame <frame-objects>` of the current
1398+
level.
1399+
1400+
Accessing this attribute raises an
1401+
:ref:`auditing event <auditing>` ``object.__getattr__`` with arguments
1402+
``obj`` and ``"tb_frame"``.
1403+
1404+
* - .. attribute:: traceback.tb_lineno
1405+
- Gives the line number where the exception occurred
1406+
1407+
* - .. attribute:: traceback.tb_lasti
1408+
- Indicates the "precise instruction".
1409+
13911410
The line number and last instruction in the traceback may differ from the
1392-
line number of its frame object if the exception occurred in a
1411+
line number of its :ref:`frame object <frame-objects>` if the exception
1412+
occurred in a
13931413
:keyword:`try` statement with no matching except clause or with a
1394-
finally clause.
1395-
1396-
Accessing ``tb_frame`` raises an :ref:`auditing event <auditing>`
1397-
``object.__getattr__`` with arguments ``obj`` and ``"tb_frame"``.
1414+
:keyword:`finally` clause.
13981415

13991416
.. index::
14001417
single: tb_next (traceback attribute)
14011418

1402-
Special writable attribute: :attr:`tb_next` is the next level in the stack
1403-
trace (towards the frame where the exception occurred), or ``None`` if
1404-
there is no next level.
1419+
.. attribute:: traceback.tb_next
14051420

1406-
.. versionchanged:: 3.7
1407-
Traceback objects can now be explicitly instantiated from Python code,
1408-
and the ``tb_next`` attribute of existing instances can be updated.
1421+
The special writable attribute :attr:`!tb_next` is the next level in the
1422+
stack trace (towards the frame where the exception occurred), or ``None`` if
1423+
there is no next level.
1424+
1425+
.. versionchanged:: 3.7
1426+
This attribute is now writable
14091427

14101428

14111429
Slice objects

Doc/whatsnew/3.5.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1947,7 +1947,8 @@ traceback
19471947
---------
19481948

19491949
New :func:`~traceback.walk_stack` and :func:`~traceback.walk_tb`
1950-
functions to conveniently traverse frame and traceback objects.
1950+
functions to conveniently traverse frame and
1951+
:ref:`traceback objects <traceback-objects>`.
19511952
(Contributed by Robert Collins in :issue:`17911`.)
19521953

19531954
New lightweight classes: :class:`~traceback.TracebackException`,

Doc/whatsnew/3.7.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -525,8 +525,8 @@ Other Language Changes
525525

526526
* In order to better support dynamic creation of stack traces,
527527
:class:`types.TracebackType` can now be instantiated from Python code, and
528-
the ``tb_next`` attribute on :ref:`tracebacks <traceback-objects>` is now
529-
writable.
528+
the :attr:`~traceback.tb_next` attribute on
529+
:ref:`tracebacks <traceback-objects>` is now writable.
530530
(Contributed by Nathaniel J. Smith in :issue:`30579`.)
531531

532532
* When using the :option:`-m` switch, ``sys.path[0]`` is now eagerly expanded

0 commit comments

Comments
 (0)