pypy / pypy

PyPy is a very fast and compliant implementation of the Python language.
https://pypy.org
Other
790 stars 38 forks source link

Partially Implemented ExceptionGroups (from PEP654) #4912

Closed nirit100 closed 3 months ago

nirit100 commented 3 months ago

Only implemented the BaseExceptionGroup and ExceptionGroup classes. No integration rn.

Based on this implementation: ExceptionGroup backport

Also, there are still one FIXME and two TODOs:

cfbolz commented 3 months ago

excellent, thank you!

I just tested on cpy3.11, none of __notes__, __cause__, __traceback__ are copied by BaseExceptionGroup.derive there:

>>> eg = ExceptionGroup("abc", [TypeError()])
>>> raise eg
  + Exception Group Traceback (most recent call last):
  |   File "<stdin>", line 1, in <module>
  | ExceptionGroup: abc (1 sub-exception)
  +-+---------------- 1 ----------------
    | TypeError
    +------------------------------------
>>> eg.__traceback__
<traceback object at 0x7f8f21dae5c0>
>>> eg.derive([TypeError()]).__traceback__
>>> eg.add_note('hello')
>>> eg
ExceptionGroup('abc', [TypeError()])
>>> raise eg
  + Exception Group Traceback (most recent call last):
  |   File "<stdin>", line 1, in <module>
  |   File "<stdin>", line 1, in <module>
  | ExceptionGroup: abc (1 sub-exception)
  | hello
  +-+---------------- 1 ----------------
    | TypeError
    +------------------------------------
>>> eg.derive([TypeError()]).__notes__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'ExceptionGroup' object has no attribute '__notes__'. Did you mean: '__ne__'?
>>> 
cfbolz commented 3 months ago

here's the source code of derive: https://github.com/python/cpython/blob/3.11/Objects/exceptions.c#L880

and of subset: https://github.com/python/cpython/blob/3.11/Objects/exceptions.c#L920