@@ -950,7 +950,7 @@ Internal types
950
950
.. index :: object: frame
951
951
952
952
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 .
954
954
955
955
.. index ::
956
956
single: f_back (frame attribute)
@@ -1003,6 +1003,8 @@ Internal types
1003
1003
1004
1004
.. versionadded :: 3.4
1005
1005
1006
+ .. _traceback-objects :
1007
+
1006
1008
Traceback objects
1007
1009
.. index ::
1008
1010
object: traceback
@@ -1015,31 +1017,51 @@ Internal types
1015
1017
single: sys.last_traceback
1016
1018
1017
1019
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
1019
1024
unwinds the execution stack, at each unwound level a traceback object is
1020
1025
inserted in front of the current traceback. When an exception handler is
1021
1026
entered, the stack trace is made available to the program. (See section
1022
1027
: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
1024
1032
handler, the stack trace is written (nicely formatted) to the standard error
1025
1033
stream; if the interpreter is interactive, it is also made available to the user
1026
1034
as ``sys.last_traceback ``.
1027
1035
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
+
1028
1040
.. index ::
1029
- single: tb_next (traceback attribute)
1030
1041
single: tb_frame (traceback attribute)
1031
1042
single: tb_lineno (traceback attribute)
1032
1043
single: tb_lasti (traceback attribute)
1033
1044
statement: try
1034
1045
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.
1043
1065
1044
1066
Slice objects
1045
1067
.. index :: builtin: slice
0 commit comments