|
| 1 | +# flake8: noqa |
| 2 | +try: |
| 3 | + from exceptiongroup import ExceptionGroup, BaseExceptionGroup # noqa |
| 4 | +except ImportError: |
| 5 | + # if we're here and 'exceptiongroup' isn't installed, it must mean we're on |
| 6 | + # Python 3.11+ and have support natively |
| 7 | + pass |
| 8 | + |
| 9 | +from .caused_by import exception_with_explicit_cause |
| 10 | + |
| 11 | +# this creates an exception with a very small __traceback__, so it's easier to |
| 12 | +# assert against in tests |
| 13 | +def generate_exception(exception_class, message): |
| 14 | + try: |
| 15 | + raise exception_class(message) |
| 16 | + except BaseException as exception: |
| 17 | + return exception |
| 18 | + |
| 19 | + |
| 20 | +def raise_exception_group_with_no_cause(): |
| 21 | + raise ExceptionGroup('the message of the group', [generate_exception(Exception, 'exception #1'), generate_exception(ArithmeticError, 'exception #2'), generate_exception(NameError, 'exception #3'), generate_exception(AssertionError, 'exception #4')]) |
| 22 | + |
| 23 | + |
| 24 | +try: |
| 25 | + raise_exception_group_with_no_cause() |
| 26 | +except BaseExceptionGroup as exception_group: |
| 27 | + exception_group_with_no_cause = exception_group |
| 28 | + |
| 29 | + |
| 30 | +class MyExceptionGroup(BaseExceptionGroup): |
| 31 | + pass |
| 32 | + |
| 33 | + |
| 34 | +def raise_base_exception_group_subclass_with_no_cause(): |
| 35 | + raise MyExceptionGroup('my very easy method just speeds up (n)making exception groups', [generate_exception(GeneratorExit, 'exception #1'), generate_exception(ReferenceError, 'exception #2'), generate_exception(NotImplementedError, 'exception #3')]) |
| 36 | + |
| 37 | + |
| 38 | +try: |
| 39 | + raise_base_exception_group_subclass_with_no_cause() |
| 40 | +except BaseExceptionGroup as exception_group: |
| 41 | + base_exception_group_subclass = exception_group |
| 42 | + |
| 43 | + |
| 44 | +def raise_exception_group_with_nested_group(): |
| 45 | + raise ExceptionGroup('the message of the group', [generate_exception(Exception, 'exception #1'), exception_group_with_no_cause, generate_exception(ArithmeticError, 'exception #3')]) |
| 46 | + |
| 47 | + |
| 48 | +try: |
| 49 | + raise_exception_group_with_nested_group() |
| 50 | +except BaseExceptionGroup as exception_group: |
| 51 | + exception_group_with_nested_group = exception_group |
| 52 | + |
| 53 | + |
| 54 | +def raise_exception_group_with_implicit_cause(): |
| 55 | + try: |
| 56 | + raise_exception_group_with_nested_group() |
| 57 | + except BaseExceptionGroup as exception_group: |
| 58 | + raise ExceptionGroup('group with implicit cause', [exception_with_explicit_cause, generate_exception(NameError, 'exception #2')]) |
| 59 | + |
| 60 | + |
| 61 | +try: |
| 62 | + raise_exception_group_with_implicit_cause() |
| 63 | +except BaseExceptionGroup as exception_group: |
| 64 | + exception_group_with_implicit_cause = exception_group |
| 65 | + |
| 66 | + |
| 67 | +def raise_exception_group_with_explicit_cause(): |
| 68 | + try: |
| 69 | + raise_exception_group_with_implicit_cause() |
| 70 | + except BaseExceptionGroup as exception_group: |
| 71 | + raise ExceptionGroup('group with explicit cause', [generate_exception(NameError, 'exception #1'), exception_with_explicit_cause]) from exception_group |
| 72 | + |
| 73 | + |
| 74 | +try: |
| 75 | + raise_exception_group_with_explicit_cause() |
| 76 | +except BaseExceptionGroup as exception_group: |
| 77 | + exception_group_with_explicit_cause = exception_group |
0 commit comments