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::
|
.. code-block::
|
||||||
|
|
||||||
>>> def f(v):
|
>>> def f(v):
|
||||||
... try:
|
... try:
|
||||||
... raise ValueError(v)
|
... raise ValueError(v)
|
||||||
... except ValueError as e:
|
... except ValueError as e:
|
||||||
... return e
|
... return e
|
||||||
...
|
...
|
||||||
>>> try:
|
>>> try:
|
||||||
... raise ExceptionGroup("one", [f(1)])
|
... raise ExceptionGroup("one", [f(1)])
|
||||||
... except ExceptionGroup as e:
|
... except ExceptionGroup as e:
|
||||||
... eg1 = e
|
... eg = e
|
||||||
...
|
...
|
||||||
>>> try:
|
>>> raise ExceptionGroup("two", [f(2), eg])
|
||||||
... raise ExceptionGroup("two", [f(2), eg1])
|
| Traceback (most recent call last):
|
||||||
... except ExceptionGroup as e:
|
| File "<stdin>", line 1, in <module>
|
||||||
... eg2 = e
|
| ExceptionGroup: two
|
||||||
...
|
| with 2 sub-exceptions:
|
||||||
>>> import traceback
|
+-+---------------- 1 ----------------
|
||||||
>>> traceback.print_exception(eg2)
|
| Traceback (most recent call last):
|
||||||
Traceback (most recent call last):
|
| File "<stdin>", line 3, in f
|
||||||
File "<stdin>", line 2, in <module>
|
| ValueError: 2
|
||||||
ExceptionGroup: two
|
+---------------- 2 ----------------
|
||||||
------------------------------------------------------------
|
| Traceback (most recent call last):
|
||||||
Traceback (most recent call last):
|
| File "<stdin>", line 2, in <module>
|
||||||
File "<stdin>", line 3, in f
|
| ExceptionGroup: one
|
||||||
ValueError: 2
|
| with one sub-exception:
|
||||||
------------------------------------------------------------
|
+-+---------------- 2.1 ----------------
|
||||||
Traceback (most recent call last):
|
| Traceback (most recent call last):
|
||||||
File "<stdin>", line 2, in <module>
|
| File "<stdin>", line 3, in f
|
||||||
ExceptionGroup: one
|
| ValueError: 1
|
||||||
------------------------------------------------------------
|
+------------------------------------
|
||||||
Traceback (most recent call last):
|
>>>
|
||||||
File "<stdin>", line 3, in f
|
|
||||||
ValueError: 1
|
|
||||||
>>>
|
|
||||||
|
|
||||||
Handling Exception Groups
|
Handling Exception Groups
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
@ -702,59 +699,61 @@ merged with the unhandled ``TypeErrors``.
|
||||||
|
|
||||||
.. code-block::
|
.. code-block::
|
||||||
|
|
||||||
>>> try:
|
>>> try:
|
||||||
... try:
|
... raise ExceptionGroup(
|
||||||
... raise ExceptionGroup(
|
... "eg",
|
||||||
... "eg",
|
... [
|
||||||
... [
|
... ValueError(1),
|
||||||
... ValueError(1),
|
... TypeError(2),
|
||||||
... TypeError(2),
|
... OSError(3),
|
||||||
... OSError(3),
|
... ExceptionGroup(
|
||||||
... ExceptionGroup(
|
... "nested",
|
||||||
... "nested",
|
... [OSError(4), TypeError(5), ValueError(6)])
|
||||||
... [OSError(4), TypeError(5), ValueError(6)])
|
... ]
|
||||||
... ]
|
... )
|
||||||
... )
|
... except *ValueError as e:
|
||||||
... except *ValueError as e:
|
... print(f'*ValueError: {e!r}')
|
||||||
... print(f'*ValueError: {e!r}')
|
... raise e
|
||||||
... raise e
|
... except *OSError as e:
|
||||||
... except *OSError as e:
|
... print(f'*OSError: {e!r}')
|
||||||
... print(f'*OSError: {e!r}')
|
... raise
|
||||||
... raise
|
...
|
||||||
... except ExceptionGroup as e:
|
*ValueError: ExceptionGroup('eg', [ValueError(1), ExceptionGroup('nested', [ValueError(6)])])
|
||||||
... traceback.print_exception(e)
|
*OSError: ExceptionGroup('eg', [OSError(3), ExceptionGroup('nested', [OSError(4)])])
|
||||||
...
|
| ExceptionGroup
|
||||||
*ValueError: ExceptionGroup('eg', [ValueError(1), ExceptionGroup('nested', [ValueError(6)])])
|
| with 2 sub-exceptions:
|
||||||
*OSError: ExceptionGroup('eg', [OSError(3), ExceptionGroup('nested', [OSError(4)])])
|
+-+---------------- 1 ----------------
|
||||||
Traceback (most recent call last):
|
| Traceback (most recent call last):
|
||||||
File "<stdin>", line 3, in <module>
|
| File "<stdin>", line 15, in <module>
|
||||||
ExceptionGroup
|
| File "<stdin>", line 2, in <module>
|
||||||
------------------------------------------------------------
|
| ExceptionGroup: eg
|
||||||
Traceback (most recent call last):
|
| with 2 sub-exceptions:
|
||||||
File "<stdin>", line 12, in <module>
|
+-+---------------- 1.1 ----------------
|
||||||
File "<stdin>", line 3, in <module>
|
| ValueError: 1
|
||||||
ExceptionGroup: eg
|
+---------------- 1.2 ----------------
|
||||||
------------------------------------------------------------
|
| ExceptionGroup: nested
|
||||||
ValueError: 1
|
| with one sub-exception:
|
||||||
------------------------------------------------------------
|
+-+---------------- 1.2.1 ----------------
|
||||||
ExceptionGroup: nested
|
| ValueError: 6
|
||||||
------------------------------------------------------------
|
+------------------------------------
|
||||||
ValueError: 6
|
+---------------- 2 ----------------
|
||||||
------------------------------------------------------------
|
| Traceback (most recent call last):
|
||||||
Traceback (most recent call last):
|
| File "<stdin>", line 2, in <module>
|
||||||
File "<stdin>", line 3, in <module>
|
| ExceptionGroup: eg
|
||||||
ExceptionGroup: eg
|
| with 3 sub-exceptions:
|
||||||
------------------------------------------------------------
|
+-+---------------- 2.1 ----------------
|
||||||
TypeError: 2
|
| TypeError: 2
|
||||||
------------------------------------------------------------
|
+---------------- 2.2 ----------------
|
||||||
OSError: 3
|
| OSError: 3
|
||||||
------------------------------------------------------------
|
+---------------- 2.3 ----------------
|
||||||
ExceptionGroup: nested
|
| ExceptionGroup: nested
|
||||||
------------------------------------------------------------
|
| with 2 sub-exceptions:
|
||||||
OSError: 4
|
+-+---------------- 2.3.1 ----------------
|
||||||
------------------------------------------------------------
|
| OSError: 4
|
||||||
TypeError: 5
|
+---------------- 2.3.2 ----------------
|
||||||
>>>
|
| TypeError: 5
|
||||||
|
+------------------------------------
|
||||||
|
>>>
|
||||||
|
|
||||||
|
|
||||||
Chaining
|
Chaining
|
||||||
|
@ -767,40 +766,42 @@ it into the new ``ExceptionGroup``.
|
||||||
|
|
||||||
.. code-block::
|
.. code-block::
|
||||||
|
|
||||||
>>> try:
|
>>> try:
|
||||||
... try:
|
... raise ExceptionGroup("one", [ValueError('a'), TypeError('b')])
|
||||||
... raise ExceptionGroup("one", [ValueError('a'), TypeError('b')])
|
... except *ValueError:
|
||||||
... except *ValueError:
|
... raise ExceptionGroup("two", [KeyError('x'), KeyError('y')])
|
||||||
... raise ExceptionGroup("two", [KeyError('x'), KeyError('y')])
|
...
|
||||||
... except BaseException as e:
|
| ExceptionGroup
|
||||||
... traceback.print_exception(e)
|
| with 2 sub-exceptions:
|
||||||
...
|
+-+---------------- 1 ----------------
|
||||||
Traceback (most recent call last):
|
| Traceback (most recent call last):
|
||||||
File "<stdin>", line 3, in <module>
|
| File "<stdin>", line 2, in <module>
|
||||||
ExceptionGroup
|
| ExceptionGroup: one
|
||||||
------------------------------------------------------------
|
| with one sub-exception:
|
||||||
Traceback (most recent call last):
|
+-+---------------- 1.context.1 ----------------
|
||||||
File "<stdin>", line 3, in <module>
|
| ValueError: a
|
||||||
ExceptionGroup: one
|
+------------------------------------
|
||||||
------------------------------------------------------------
|
|
|
||||||
ValueError: a
|
| During handling of the above exception, another exception occurred:
|
||||||
|
|
|
||||||
During handling of the above exception, another exception occurred:
|
| Traceback (most recent call last):
|
||||||
|
| File "<stdin>", line 4, in <module>
|
||||||
Traceback (most recent call last):
|
| ExceptionGroup: two
|
||||||
File "<stdin>", line 5, in <module>
|
| with 2 sub-exceptions:
|
||||||
ExceptionGroup: two
|
+-+---------------- 1.1 ----------------
|
||||||
------------------------------------------------------------
|
| KeyError: 'x'
|
||||||
KeyError: 'x'
|
+---------------- 1.2 ----------------
|
||||||
------------------------------------------------------------
|
| KeyError: 'y'
|
||||||
KeyError: 'y'
|
+------------------------------------
|
||||||
|
+---------------- 2 ----------------
|
||||||
------------------------------------------------------------
|
| Traceback (most recent call last):
|
||||||
Traceback (most recent call last):
|
| File "<stdin>", line 2, in <module>
|
||||||
File "<stdin>", line 3, in <module>
|
| ExceptionGroup: one
|
||||||
ExceptionGroup: one
|
| with one sub-exception:
|
||||||
------------------------------------------------------------
|
+-+---------------- 2.1 ----------------
|
||||||
TypeError: b
|
| TypeError: b
|
||||||
|
+------------------------------------
|
||||||
|
>>>
|
||||||
|
|
||||||
|
|
||||||
Raising New Exceptions
|
Raising New Exceptions
|
||||||
|
@ -812,30 +813,29 @@ chaining:
|
||||||
|
|
||||||
.. code-block::
|
.. code-block::
|
||||||
|
|
||||||
>>> try:
|
>>> try:
|
||||||
... try:
|
... raise TypeError('bad type')
|
||||||
... raise TypeError('bad type')
|
... except *TypeError as e:
|
||||||
... except *TypeError as e:
|
... raise ValueError('bad value') from e
|
||||||
... raise ValueError('bad value') from e
|
...
|
||||||
... except ExceptionGroup as e:
|
| ExceptionGroup
|
||||||
... traceback.print_exception(e)
|
| with one sub-exception:
|
||||||
...
|
+-+---------------- 1 ----------------
|
||||||
Traceback (most recent call last):
|
| ExceptionGroup
|
||||||
File "<stdin>", line 3, in <module>
|
| with one sub-exception:
|
||||||
ExceptionGroup
|
+-+---------------- 1.cause.1 ----------------
|
||||||
------------------------------------------------------------
|
| Traceback (most recent call last):
|
||||||
ExceptionGroup
|
| File "<stdin>", line 2, in <module>
|
||||||
------------------------------------------------------------
|
| TypeError: bad type
|
||||||
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:
|
||||||
|
|
|
||||||
The above exception was the direct cause of the following exception:
|
| Traceback (most recent call last):
|
||||||
|
| File "<stdin>", line 4, in <module>
|
||||||
Traceback (most recent call last):
|
| ValueError: bad value
|
||||||
File "<stdin>", line 5, in <module>
|
+------------------------------------
|
||||||
ValueError: bad value
|
>>>
|
||||||
>>>
|
|
||||||
|
|
||||||
|
|
||||||
Note that exceptions raised in one ``except*`` clause are not eligible to match
|
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::
|
.. code-block::
|
||||||
|
|
||||||
>>> try:
|
>>> try:
|
||||||
... try:
|
... raise TypeError(1)
|
||||||
... raise TypeError(1)
|
... except *TypeError:
|
||||||
... except *TypeError:
|
... raise ValueError(2) from None # <- not caught in the next clause
|
||||||
... raise ValueError(2) # <- not caught in the next clause
|
... except *ValueError:
|
||||||
... except *ValueError:
|
... print('never')
|
||||||
... print('never')
|
...
|
||||||
... except ExceptionGroup as e:
|
| ExceptionGroup
|
||||||
... traceback.print_exception(e)
|
| with one sub-exception:
|
||||||
...
|
+-+---------------- 1 ----------------
|
||||||
Traceback (most recent call last):
|
| Traceback (most recent call last):
|
||||||
File "<stdin>", line 3, in <module>
|
| File "<stdin>", line 4, in <module>
|
||||||
ExceptionGroup
|
| ValueError: 2
|
||||||
------------------------------------------------------------
|
+----------------------------------------
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
Raising a new instance of a naked exception does not cause this exception to
|
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::
|
.. code-block::
|
||||||
|
|
||||||
>>> try:
|
>>> try:
|
||||||
... try:
|
... raise ExceptionGroup("eg", [ValueError('a')])
|
||||||
... raise ExceptionGroup("eg", [ValueError('a')])
|
... except *ValueError:
|
||||||
... except *ValueError:
|
... raise KeyError('x')
|
||||||
... raise KeyError('x')
|
|
||||||
... except BaseException as e:
|
|
||||||
... traceback.print_exception(e)
|
|
||||||
...
|
...
|
||||||
Traceback (most recent call last):
|
| ExceptionGroup
|
||||||
File "<stdin>", line 3, in <module>
|
| with one sub-exception:
|
||||||
ExceptionGroup
|
+-+---------------- 1 ----------------
|
||||||
------------------------------------------------------------
|
| Traceback (most recent call last):
|
||||||
Traceback (most recent call last):
|
| File "<stdin>", line 2, in <module>
|
||||||
File "<stdin>", line 3, in <module>
|
| ExceptionGroup: eg
|
||||||
ExceptionGroup: eg
|
| with one sub-exception:
|
||||||
------------------------------------------------------------
|
+-+---------------- 1.context.1 ----------------
|
||||||
ValueError: a
|
| ValueError: a
|
||||||
|
+------------------------------------
|
||||||
During handling of the above exception, another exception occurred:
|
|
|
||||||
|
| During handling of the above exception, another exception occurred:
|
||||||
Traceback (most recent call last):
|
|
|
||||||
File "<stdin>", line 5, in <module>
|
| Traceback (most recent call last):
|
||||||
KeyError: 'x'
|
| File "<stdin>", line 4, in <module>
|
||||||
|
| KeyError: 'x'
|
||||||
|
+------------------------------------
|
||||||
>>>
|
>>>
|
||||||
>>> try:
|
>>> try:
|
||||||
... try:
|
... raise ExceptionGroup("eg", [ValueError('a'), TypeError('b')])
|
||||||
... raise ExceptionGroup("eg", [ValueError('a'), TypeError('b')])
|
... except *ValueError:
|
||||||
... except *ValueError:
|
... raise KeyError('x')
|
||||||
... raise KeyError('x')
|
|
||||||
... except BaseException as e:
|
|
||||||
... traceback.print_exception(e)
|
|
||||||
...
|
...
|
||||||
Traceback (most recent call last):
|
| ExceptionGroup
|
||||||
File "<stdin>", line 3, in <module>
|
| with 2 sub-exceptions:
|
||||||
ExceptionGroup
|
+-+---------------- 1 ----------------
|
||||||
------------------------------------------------------------
|
| Traceback (most recent call last):
|
||||||
Traceback (most recent call last):
|
| File "<stdin>", line 2, in <module>
|
||||||
File "<stdin>", line 3, in <module>
|
| ExceptionGroup: eg
|
||||||
ExceptionGroup: eg
|
| with one sub-exception:
|
||||||
------------------------------------------------------------
|
+-+---------------- 1.context.1 ----------------
|
||||||
ValueError: a
|
| ValueError: a
|
||||||
|
+------------------------------------
|
||||||
During handling of the above exception, another exception occurred:
|
|
|
||||||
|
| During handling of the above exception, another exception occurred:
|
||||||
Traceback (most recent call last):
|
|
|
||||||
File "<stdin>", line 5, in <module>
|
| Traceback (most recent call last):
|
||||||
KeyError: 'x'
|
| File "<stdin>", line 4, in <module>
|
||||||
|
| KeyError: 'x'
|
||||||
------------------------------------------------------------
|
+---------------- 2 ----------------
|
||||||
Traceback (most recent call last):
|
| Traceback (most recent call last):
|
||||||
File "<stdin>", line 3, in <module>
|
| File "<stdin>", line 2, in <module>
|
||||||
ExceptionGroup: eg
|
| ExceptionGroup: eg
|
||||||
------------------------------------------------------------
|
| with one sub-exception:
|
||||||
TypeError: b
|
+-+---------------- 2.1 ----------------
|
||||||
|
| TypeError: b
|
||||||
|
+------------------------------------
|
||||||
>>>
|
>>>
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue