8000 gh-102011: use sys.exception() instead of sys.exc_info() in docs wher… · python/cpython@78eee76 · GitHub
[go: up one dir, main page]

Skip to content

Commit 78eee76

Browse files
gh-102011: use sys.exception() instead of sys.exc_info() in docs where possible (GH-102012)
(cherry picked from commit 4d3bc89) Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
1 parent a94f3ad commit 78eee76

File tree

6 files changed

+29
-32
lines changed

6 files changed

+29
-32
lines changed

Doc/library/exceptions.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ The following exceptions are used mostly as base classes for other exceptions.
123123
try:
124124
...
125125
except SomeException:
126-
tb = sys.exc_info()[2]
126+
tb = sys.exception().__traceback__
127127
raise OtherException(...).with_traceback(tb)
128128

129129
.. method:: add_note(note)

Doc/library/traceback.rst

Lines changed: 10 additions & 11 deletions
81
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@ interpreter.
1616

1717
.. index:: object: traceback
1818

19-
The module uses traceback objects --- this is the object type that is stored in
20-
the :data:`sys.last_traceback` variable and returned as the third item from
21-
:func:`sys.exc_info`.
19+
The module uses traceback objects --- these are objects of type :class:`types.TracebackType`,
20+
which are assigned to the ``__traceback__`` field of :class:`BaseException` instances.
2221

2322
.. seealso::
2423

@@ -81,7 +80,7 @@ The module defines the following functions:
80

8281
.. function:: print_exc(limit=None, file=None, chain=True)
8382

84-
This is a shorthand for ``print_exception(*sys.exc_info(), limit, file,
83+
This is a shorthand for ``print_exception(sys.exception(), limit, file,
8584
chain)``.
8685

8786

@@ -440,24 +439,24 @@ exception and traceback:
440439
try:
441440
lumberjack()
442441
except IndexError:
443-
exc_type, exc_value, exc_traceback = sys.exc_info()
442+
exc = sys.exception()
444443
print("*** print_tb:")
445-
traceback.print_tb(exc_traceback, limit=1, file=sys.stdout)
444+
traceback.print_tb(exc.__traceback__, limit=1, file=sys.stdout)
446445
print("*** print_exception:")
447-
traceback.print_exception(exc_value, limit=2, file=sys.stdout)
446+
traceback.print_exception(exc, limit=2, file=sys.stdout)
448447
print("*** print_exc:")
449448
traceback.print_exc(limit=2, file=sys.stdout)
450449
print("*** format_exc, first and last line:")
451450
formatted_lines = traceback.format_exc().splitlines()
452451
print(formatted_lines[0])
453452
print(formatted_lines[-1])
454453
print("*** format_exception:")
455-
print(repr(traceback.format_exception(exc_value)))
454+
print(repr(traceback.format_exception(exc)))
456455
print("*** extract_tb:")
457-
print(repr(traceback.extract_tb(exc_traceback)))
456+
print(repr(traceback.extract_tb(exc.__traceback__)))
458457
print("*** format_tb:")
459-
print(repr(traceback.format_tb(exc_traceback)))
460-
print("*** tb_lineno:", exc_traceback.tb_lineno)
458+
print(repr(traceback.format_tb(exc.__traceback__)))
459+
print("*** tb_lineno:", exc.__traceback__.tb_lineno)
461460

462461
The output for the example would look similar to this:
463462

Doc/library/types.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ Standard names are defined for the following types:
320320

321321
.. class:: TracebackType(tb_next, tb_frame, tb_lasti, tb_lineno)
322322

323-
The type of traceback objects such as found in ``sys.exc_info()[2]``.
323+
The type of traceback objects such as found in ``sys.exception().__traceback__``.
324324

325325
See :ref:`the language reference <traceback-objects>` for details of the
326326
available attributes and operations, and guidance on creating tracebacks

Doc/library/wsgiref.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ input, output, and error streams.
674674
This method is a WSGI application to generate an error page for the user. It is
675675
only invoked if an error occurs before headers are sent to the client.
676676

677-
This method can access the current error information using ``sys.exc_info()``,
677+
This method can access the current error using ``sys.exception()``,
678678
and should pass that information to *start_response* when calling it (as
679679
described in the "Error Handling" section of :pep:`3333`).
680680

Doc/reference/compound_stmts.rst

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -301,31 +301,28 @@ keeping all locals in that frame alive until the next garbage collection occurs.
301301
object: traceback
302302

303303
Before an :keyword:`!except` clause's suite is executed,
304-
details about the exception are
305-
stored in the :mod:`sys` module and can be accessed via :func:`sys.exc_info`.
306-
:func:`sys.exc_info` returns a 3-tuple consisting of the exception class, the
307-
exception instance and a traceback object (see section :ref:`types`) identifying
308-
the point in the program where the exception occurred. The details about the
309-
exception accessed via :func:`sys.exc_info` are restored to their previous values
310-
when leaving an exception handler::
311-
312-
>>> print(sys.exc_info())
313-
(None, None, None)
304+
the exception is stored in the :mod:`sys` module, where it can be accessed
305+
from within the body of the :keyword:`!except` clause by calling
306+
:func:`sys.exception`. When leaving an exception handler, the exception
307+
stored in the :mod:`sys` module is reset to its previous value::
308+
309+
>>> print(sys.exception())
310+
None
314311
>>> try:
315312
... raise TypeError
316313
... except:
317-
... print(sys.exc_info())
314+
... print(repr(sys.exception()))
318315
... try:
319316
... raise ValueError
320317
... except:
321-
... print(sys.exc_info())
322-
... print(sys.exc_info())
318+
... print(repr(sys.exception()))
319+
... print(repr(sys.exception()))
323320
...
324-
(<class 'TypeError'>, TypeError(), <traceback object at 0x10efad080>)
325-
(<class 'ValueError'>, ValueError(), <traceback object at 0x10efad040>)
326-
(<class 'TypeError'>, TypeError(), <traceback object at 0x10efad080>)
327-
>>> print(sys.exc_info())
328-
(None, None, None)
321+
TypeError()
322+
ValueError()
323+
TypeError()
324+
>>> print(sys.exception())
325+
None
329326

330327

331328
.. index::

Doc/reference/datamodel.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,6 +1122,7 @@ Internal types
11221122
single: exc_info (in module sys)
11231123
single: last_traceback (in module sys)
11241124
single: sys.exc_info
1125+
single: sys.exception
11251126
single: sys.last_traceback
11261127

11271128
Traceback objects represent a stack trace of an exception. A traceback object

0 commit comments

Comments
 (0)
0