Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4d3bc89

Browse files
authored
gh-102011: use sys.exception() instead of sys.exc_info() in docs where possible (#102012)
1 parent36854bb commit4d3bc89

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
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:
8180

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

@@ -444,24 +443,24 @@ exception and traceback:
444443
try:
445444
lumberjack()
446445
except IndexError:
447-
exc_type, exc_value, exc_traceback= sys.exc_info()
446+
exc= sys.exception()
448447
print("*** print_tb:")
449-
traceback.print_tb(exc_traceback, limit=1, file=sys.stdout)
448+
traceback.print_tb(exc.__traceback__, limit=1, file=sys.stdout)
450449
print("*** print_exception:")
451-
traceback.print_exception(exc_value, limit=2, file=sys.stdout)
450+
traceback.print_exception(exc, limit=2, file=sys.stdout)
452451
print("*** print_exc:")
453452
traceback.print_exc(limit=2, file=sys.stdout)
454453
print("*** format_exc, first and last line:")
455454
formatted_lines = traceback.format_exc().splitlines()
456455
print(formatted_lines[0])
457456
print(formatted_lines[-1])
458457
print("*** format_exception:")
459-
print(repr(traceback.format_exception(exc_value)))
458+
print(repr(traceback.format_exception(exc)))
460459
print("*** extract_tb:")
461-
print(repr(traceback.extract_tb(exc_traceback)))
460+
print(repr(traceback.extract_tb(exc.__traceback__)))
462461
print("*** format_tb:")
463-
print(repr(traceback.format_tb(exc_traceback)))
464-
print("*** tb_lineno:",exc_traceback.tb_lineno)
462+
print(repr(traceback.format_tb(exc.__traceback__)))
463+
print("*** tb_lineno:",exc.__traceback__.tb_lineno)
465464

466465
The output for the example would look similar to this:
467466

‎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 errorinformationusing ``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)

[8]ページ先頭

©2009-2025 Movatter.jp