PEP 654: Use C traceback for some examples (#1866)
This commit is contained in:
parent
2aec45c8b8
commit
d4d4fe7b6f
426
pep-0654.rst
426
pep-0654.rst
|
@ -295,40 +295,37 @@ in the following example:
|
|||
|
||||
.. code-block::
|
||||
|
||||
>>> def f(v):
|
||||
... try:
|
||||
... raise ValueError(v)
|
||||
... except ValueError as e:
|
||||
... return e
|
||||
...
|
||||
>>> try:
|
||||
... raise ExceptionGroup("one", [f(1)])
|
||||
... except ExceptionGroup as e:
|
||||
... eg1 = e
|
||||
...
|
||||
>>> try:
|
||||
... raise ExceptionGroup("two", [f(2), eg1])
|
||||
... except ExceptionGroup as e:
|
||||
... eg2 = e
|
||||
...
|
||||
>>> import traceback
|
||||
>>> traceback.print_exception(eg2)
|
||||
Traceback (most recent call last):
|
||||
File "<stdin>", line 2, in <module>
|
||||
ExceptionGroup: two
|
||||
------------------------------------------------------------
|
||||
Traceback (most recent call last):
|
||||
File "<stdin>", line 3, in f
|
||||
ValueError: 2
|
||||
------------------------------------------------------------
|
||||
Traceback (most recent call last):
|
||||
File "<stdin>", line 2, in <module>
|
||||
ExceptionGroup: one
|
||||
------------------------------------------------------------
|
||||
Traceback (most recent call last):
|
||||
File "<stdin>", line 3, in f
|
||||
ValueError: 1
|
||||
>>>
|
||||
>>> def f(v):
|
||||
... try:
|
||||
... raise ValueError(v)
|
||||
... except ValueError as e:
|
||||
... return e
|
||||
...
|
||||
>>> try:
|
||||
... raise ExceptionGroup("one", [f(1)])
|
||||
... except ExceptionGroup as e:
|
||||
... eg = e
|
||||
...
|
||||
>>> raise ExceptionGroup("two", [f(2), eg])
|
||||
| Traceback (most recent call last):
|
||||
| File "<stdin>", line 1, in <module>
|
||||
| ExceptionGroup: two
|
||||
| with 2 sub-exceptions:
|
||||
+-+---------------- 1 ----------------
|
||||
| Traceback (most recent call last):
|
||||
| File "<stdin>", line 3, in f
|
||||
| ValueError: 2
|
||||
+---------------- 2 ----------------
|
||||
| Traceback (most recent call last):
|
||||
| File "<stdin>", line 2, in <module>
|
||||
| ExceptionGroup: one
|
||||
| with one sub-exception:
|
||||
+-+---------------- 2.1 ----------------
|
||||
| Traceback (most recent call last):
|
||||
| File "<stdin>", line 3, in f
|
||||
| ValueError: 1
|
||||
+------------------------------------
|
||||
>>>
|
||||
|
||||
Handling Exception Groups
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
@ -702,59 +699,61 @@ merged with the unhandled ``TypeErrors``.
|
|||
|
||||
.. code-block::
|
||||
|
||||
>>> try:
|
||||
... try:
|
||||
... raise ExceptionGroup(
|
||||
... "eg",
|
||||
... [
|
||||
... ValueError(1),
|
||||
... TypeError(2),
|
||||
... OSError(3),
|
||||
... ExceptionGroup(
|
||||
... "nested",
|
||||
... [OSError(4), TypeError(5), ValueError(6)])
|
||||
... ]
|
||||
... )
|
||||
... except *ValueError as e:
|
||||
... print(f'*ValueError: {e!r}')
|
||||
... raise e
|
||||
... except *OSError as e:
|
||||
... print(f'*OSError: {e!r}')
|
||||
... raise
|
||||
... except ExceptionGroup as e:
|
||||
... traceback.print_exception(e)
|
||||
...
|
||||
*ValueError: ExceptionGroup('eg', [ValueError(1), ExceptionGroup('nested', [ValueError(6)])])
|
||||
*OSError: ExceptionGroup('eg', [OSError(3), ExceptionGroup('nested', [OSError(4)])])
|
||||
Traceback (most recent call last):
|
||||
File "<stdin>", line 3, in <module>
|
||||
ExceptionGroup
|
||||
------------------------------------------------------------
|
||||
Traceback (most recent call last):
|
||||
File "<stdin>", line 12, in <module>
|
||||
File "<stdin>", line 3, in <module>
|
||||
ExceptionGroup: eg
|
||||
------------------------------------------------------------
|
||||
ValueError: 1
|
||||
------------------------------------------------------------
|
||||
ExceptionGroup: nested
|
||||
------------------------------------------------------------
|
||||
ValueError: 6
|
||||
------------------------------------------------------------
|
||||
Traceback (most recent call last):
|
||||
File "<stdin>", line 3, in <module>
|
||||
ExceptionGroup: eg
|
||||
------------------------------------------------------------
|
||||
TypeError: 2
|
||||
------------------------------------------------------------
|
||||
OSError: 3
|
||||
------------------------------------------------------------
|
||||
ExceptionGroup: nested
|
||||
------------------------------------------------------------
|
||||
OSError: 4
|
||||
------------------------------------------------------------
|
||||
TypeError: 5
|
||||
>>>
|
||||
>>> try:
|
||||
... raise ExceptionGroup(
|
||||
... "eg",
|
||||
... [
|
||||
... ValueError(1),
|
||||
... TypeError(2),
|
||||
... OSError(3),
|
||||
... ExceptionGroup(
|
||||
... "nested",
|
||||
... [OSError(4), TypeError(5), ValueError(6)])
|
||||
... ]
|
||||
... )
|
||||
... except *ValueError as e:
|
||||
... print(f'*ValueError: {e!r}')
|
||||
... raise e
|
||||
... except *OSError as e:
|
||||
... print(f'*OSError: {e!r}')
|
||||
... raise
|
||||
...
|
||||
*ValueError: ExceptionGroup('eg', [ValueError(1), ExceptionGroup('nested', [ValueError(6)])])
|
||||
*OSError: ExceptionGroup('eg', [OSError(3), ExceptionGroup('nested', [OSError(4)])])
|
||||
| ExceptionGroup
|
||||
| with 2 sub-exceptions:
|
||||
+-+---------------- 1 ----------------
|
||||
| Traceback (most recent call last):
|
||||
| File "<stdin>", line 15, in <module>
|
||||
| File "<stdin>", line 2, in <module>
|
||||
| ExceptionGroup: eg
|
||||
| with 2 sub-exceptions:
|
||||
+-+---------------- 1.1 ----------------
|
||||
| ValueError: 1
|
||||
+---------------- 1.2 ----------------
|
||||
| ExceptionGroup: nested
|
||||
| with one sub-exception:
|
||||
+-+---------------- 1.2.1 ----------------
|
||||
| ValueError: 6
|
||||
+------------------------------------
|
||||
+---------------- 2 ----------------
|
||||
| Traceback (most recent call last):
|
||||
| File "<stdin>", line 2, in <module>
|
||||
| ExceptionGroup: eg
|
||||
| with 3 sub-exceptions:
|
||||
+-+---------------- 2.1 ----------------
|
||||
| TypeError: 2
|
||||
+---------------- 2.2 ----------------
|
||||
| OSError: 3
|
||||
+---------------- 2.3 ----------------
|
||||
| ExceptionGroup: nested
|
||||
| with 2 sub-exceptions:
|
||||
+-+---------------- 2.3.1 ----------------
|
||||
| OSError: 4
|
||||
+---------------- 2.3.2 ----------------
|
||||
| TypeError: 5
|
||||
+------------------------------------
|
||||
>>>
|
||||
|
||||
|
||||
Chaining
|
||||
|
@ -767,40 +766,42 @@ it into the new ``ExceptionGroup``.
|
|||
|
||||
.. code-block::
|
||||
|
||||
>>> try:
|
||||
... try:
|
||||
... raise ExceptionGroup("one", [ValueError('a'), TypeError('b')])
|
||||
... except *ValueError:
|
||||
... raise ExceptionGroup("two", [KeyError('x'), KeyError('y')])
|
||||
... except BaseException as e:
|
||||
... traceback.print_exception(e)
|
||||
...
|
||||
Traceback (most recent call last):
|
||||
File "<stdin>", line 3, in <module>
|
||||
ExceptionGroup
|
||||
------------------------------------------------------------
|
||||
Traceback (most recent call last):
|
||||
File "<stdin>", line 3, in <module>
|
||||
ExceptionGroup: one
|
||||
------------------------------------------------------------
|
||||
ValueError: a
|
||||
|
||||
During handling of the above exception, another exception occurred:
|
||||
|
||||
Traceback (most recent call last):
|
||||
File "<stdin>", line 5, in <module>
|
||||
ExceptionGroup: two
|
||||
------------------------------------------------------------
|
||||
KeyError: 'x'
|
||||
------------------------------------------------------------
|
||||
KeyError: 'y'
|
||||
|
||||
------------------------------------------------------------
|
||||
Traceback (most recent call last):
|
||||
File "<stdin>", line 3, in <module>
|
||||
ExceptionGroup: one
|
||||
------------------------------------------------------------
|
||||
TypeError: b
|
||||
>>> try:
|
||||
... raise ExceptionGroup("one", [ValueError('a'), TypeError('b')])
|
||||
... except *ValueError:
|
||||
... raise ExceptionGroup("two", [KeyError('x'), KeyError('y')])
|
||||
...
|
||||
| ExceptionGroup
|
||||
| with 2 sub-exceptions:
|
||||
+-+---------------- 1 ----------------
|
||||
| Traceback (most recent call last):
|
||||
| File "<stdin>", line 2, in <module>
|
||||
| ExceptionGroup: one
|
||||
| with one sub-exception:
|
||||
+-+---------------- 1.context.1 ----------------
|
||||
| ValueError: a
|
||||
+------------------------------------
|
||||
|
|
||||
| During handling of the above exception, another exception occurred:
|
||||
|
|
||||
| Traceback (most recent call last):
|
||||
| File "<stdin>", line 4, in <module>
|
||||
| ExceptionGroup: two
|
||||
| with 2 sub-exceptions:
|
||||
+-+---------------- 1.1 ----------------
|
||||
| KeyError: 'x'
|
||||
+---------------- 1.2 ----------------
|
||||
| KeyError: 'y'
|
||||
+------------------------------------
|
||||
+---------------- 2 ----------------
|
||||
| Traceback (most recent call last):
|
||||
| File "<stdin>", line 2, in <module>
|
||||
| ExceptionGroup: one
|
||||
| with one sub-exception:
|
||||
+-+---------------- 2.1 ----------------
|
||||
| TypeError: b
|
||||
+------------------------------------
|
||||
>>>
|
||||
|
||||
|
||||
Raising New Exceptions
|
||||
|
@ -812,30 +813,29 @@ chaining:
|
|||
|
||||
.. code-block::
|
||||
|
||||
>>> try:
|
||||
... try:
|
||||
... raise TypeError('bad type')
|
||||
... except *TypeError as e:
|
||||
... raise ValueError('bad value') from e
|
||||
... except ExceptionGroup as e:
|
||||
... traceback.print_exception(e)
|
||||
...
|
||||
Traceback (most recent call last):
|
||||
File "<stdin>", line 3, in <module>
|
||||
ExceptionGroup
|
||||
------------------------------------------------------------
|
||||
ExceptionGroup
|
||||
------------------------------------------------------------
|
||||
Traceback (most recent call last):
|
||||
File "<stdin>", line 3, in <module>
|
||||
TypeError: bad type
|
||||
|
||||
The above exception was the direct cause of the following exception:
|
||||
|
||||
Traceback (most recent call last):
|
||||
File "<stdin>", line 5, in <module>
|
||||
ValueError: bad value
|
||||
>>>
|
||||
>>> try:
|
||||
... raise TypeError('bad type')
|
||||
... except *TypeError as e:
|
||||
... raise ValueError('bad value') from e
|
||||
...
|
||||
| ExceptionGroup
|
||||
| with one sub-exception:
|
||||
+-+---------------- 1 ----------------
|
||||
| ExceptionGroup
|
||||
| with one sub-exception:
|
||||
+-+---------------- 1.cause.1 ----------------
|
||||
| Traceback (most recent call last):
|
||||
| File "<stdin>", line 2, in <module>
|
||||
| TypeError: bad type
|
||||
+------------------------------------
|
||||
|
|
||||
| The above exception was the direct cause of the following exception:
|
||||
|
|
||||
| Traceback (most recent call last):
|
||||
| File "<stdin>", line 4, in <module>
|
||||
| ValueError: bad value
|
||||
+------------------------------------
|
||||
>>>
|
||||
|
||||
|
||||
Note that exceptions raised in one ``except*`` clause are not eligible to match
|
||||
|
@ -843,31 +843,21 @@ other clauses from the same ``try`` statement:
|
|||
|
||||
.. code-block::
|
||||
|
||||
>>> try:
|
||||
... try:
|
||||
... raise TypeError(1)
|
||||
... except *TypeError:
|
||||
... raise ValueError(2) # <- not caught in the next clause
|
||||
... except *ValueError:
|
||||
... print('never')
|
||||
... except ExceptionGroup as e:
|
||||
... traceback.print_exception(e)
|
||||
...
|
||||
Traceback (most recent call last):
|
||||
File "<stdin>", line 3, in <module>
|
||||
ExceptionGroup
|
||||
------------------------------------------------------------
|
||||
ExceptionGroup
|
||||
------------------------------------------------------------
|
||||
Traceback (most recent call last):
|
||||
File "<stdin>", line 3, in <module>
|
||||
TypeError: 1
|
||||
|
||||
During handling of the above exception, another exception occurred:
|
||||
|
||||
Traceback (most recent call last):
|
||||
File "<stdin>", line 5, in <module>
|
||||
ValueError: 2
|
||||
>>> try:
|
||||
... raise TypeError(1)
|
||||
... except *TypeError:
|
||||
... raise ValueError(2) from None # <- not caught in the next clause
|
||||
... except *ValueError:
|
||||
... print('never')
|
||||
...
|
||||
| ExceptionGroup
|
||||
| with one sub-exception:
|
||||
+-+---------------- 1 ----------------
|
||||
| Traceback (most recent call last):
|
||||
| File "<stdin>", line 4, in <module>
|
||||
| ValueError: 2
|
||||
+----------------------------------------
|
||||
>>>
|
||||
|
||||
|
||||
Raising a new instance of a naked exception does not cause this exception to
|
||||
|
@ -879,59 +869,57 @@ direct child of the new exception group created for that:
|
|||
.. code-block::
|
||||
|
||||
>>> try:
|
||||
... try:
|
||||
... raise ExceptionGroup("eg", [ValueError('a')])
|
||||
... except *ValueError:
|
||||
... raise KeyError('x')
|
||||
... except BaseException as e:
|
||||
... traceback.print_exception(e)
|
||||
... raise ExceptionGroup("eg", [ValueError('a')])
|
||||
... except *ValueError:
|
||||
... raise KeyError('x')
|
||||
...
|
||||
Traceback (most recent call last):
|
||||
File "<stdin>", line 3, in <module>
|
||||
ExceptionGroup
|
||||
------------------------------------------------------------
|
||||
Traceback (most recent call last):
|
||||
File "<stdin>", line 3, in <module>
|
||||
ExceptionGroup: eg
|
||||
------------------------------------------------------------
|
||||
ValueError: a
|
||||
|
||||
During handling of the above exception, another exception occurred:
|
||||
|
||||
Traceback (most recent call last):
|
||||
File "<stdin>", line 5, in <module>
|
||||
KeyError: 'x'
|
||||
| ExceptionGroup
|
||||
| with one sub-exception:
|
||||
+-+---------------- 1 ----------------
|
||||
| Traceback (most recent call last):
|
||||
| File "<stdin>", line 2, in <module>
|
||||
| ExceptionGroup: eg
|
||||
| with one sub-exception:
|
||||
+-+---------------- 1.context.1 ----------------
|
||||
| ValueError: a
|
||||
+------------------------------------
|
||||
|
|
||||
| During handling of the above exception, another exception occurred:
|
||||
|
|
||||
| Traceback (most recent call last):
|
||||
| File "<stdin>", line 4, in <module>
|
||||
| KeyError: 'x'
|
||||
+------------------------------------
|
||||
>>>
|
||||
>>> try:
|
||||
... try:
|
||||
... raise ExceptionGroup("eg", [ValueError('a'), TypeError('b')])
|
||||
... except *ValueError:
|
||||
... raise KeyError('x')
|
||||
... except BaseException as e:
|
||||
... traceback.print_exception(e)
|
||||
... raise ExceptionGroup("eg", [ValueError('a'), TypeError('b')])
|
||||
... except *ValueError:
|
||||
... raise KeyError('x')
|
||||
...
|
||||
Traceback (most recent call last):
|
||||
File "<stdin>", line 3, in <module>
|
||||
ExceptionGroup
|
||||
------------------------------------------------------------
|
||||
Traceback (most recent call last):
|
||||
File "<stdin>", line 3, in <module>
|
||||
ExceptionGroup: eg
|
||||
------------------------------------------------------------
|
||||
ValueError: a
|
||||
|
||||
During handling of the above exception, another exception occurred:
|
||||
|
||||
Traceback (most recent call last):
|
||||
File "<stdin>", line 5, in <module>
|
||||
KeyError: 'x'
|
||||
|
||||
------------------------------------------------------------
|
||||
Traceback (most recent call last):
|
||||
File "<stdin>", line 3, in <module>
|
||||
ExceptionGroup: eg
|
||||
------------------------------------------------------------
|
||||
TypeError: b
|
||||
| ExceptionGroup
|
||||
| with 2 sub-exceptions:
|
||||
+-+---------------- 1 ----------------
|
||||
| Traceback (most recent call last):
|
||||
| File "<stdin>", line 2, in <module>
|
||||
| ExceptionGroup: eg
|
||||
| with one sub-exception:
|
||||
+-+---------------- 1.context.1 ----------------
|
||||
| ValueError: a
|
||||
+------------------------------------
|
||||
|
|
||||
| During handling of the above exception, another exception occurred:
|
||||
|
|
||||
| Traceback (most recent call last):
|
||||
| File "<stdin>", line 4, in <module>
|
||||
| KeyError: 'x'
|
||||
+---------------- 2 ----------------
|
||||
| Traceback (most recent call last):
|
||||
| File "<stdin>", line 2, in <module>
|
||||
| ExceptionGroup: eg
|
||||
| with one sub-exception:
|
||||
+-+---------------- 2.1 ----------------
|
||||
| TypeError: b
|
||||
+------------------------------------
|
||||
>>>
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue