|
| 1 | +import copy |
1 | 2 | import pytest
|
2 | 3 |
|
3 | 4 | from exceptiongroup import ExceptionGroup
|
4 | 5 |
|
5 | 6 |
|
| 7 | +def raise_group(): |
| 8 | + try: |
| 9 | + 1 / 0 |
| 10 | + except Exception as e: |
| 11 | + raise ExceptionGroup("ManyError", [e], [str(e)]) from e |
| 12 | + |
| 13 | + |
6 | 14 | def test_exception_group_init():
|
7 | 15 | memberA = ValueError("A")
|
8 | 16 | memberB = RuntimeError("B")
|
@@ -47,3 +55,30 @@ def test_exception_group_str():
|
47 | 55 | assert "ExceptionGroup: " in repr(group)
|
48 | 56 | assert "memberA" in repr(group)
|
49 | 57 | assert "memberB" in repr(group)
|
| 58 | + |
| 59 | + |
| 60 | +def test_exception_group_copy(): |
| 61 | + try: |
| 62 | + raise_group() # the exception is raise by `raise...from..` |
| 63 | + except ExceptionGroup as e: |
| 64 | + group = e |
| 65 | + |
| 66 | + another_group = copy.copy(group) |
| 67 | + assert another_group.message == group.message |
| 68 | + assert another_group.exceptions == group.exceptions |
| 69 | + assert another_group.sources == group.sources |
| 70 | + assert another_group.__traceback__ is group.__traceback__ |
| 71 | + assert another_group.__cause__ is group.__cause__ |
| 72 | + assert another_group.__context__ is group.__context__ |
| 73 | + assert another_group.__suppress_context__ is group.__suppress_context__ |
| 74 | + assert another_group.__cause__ is not None |
| 75 | + assert another_group.__context__ is not None |
| 76 | + assert another_group.__suppress_context__ is True |
| 77 | + |
| 78 | + # doing copy when __suppress_context__ is False |
| 79 | + group.__suppress_context__ = False |
| 80 | + another_group = copy.copy(group) |
| 81 | + assert another_group.__cause__ is group.__cause__ |
| 82 | + assert another_group.__context__ is group.__context__ |
| 83 | + assert another_group.__suppress_context__ is group.__suppress_context__ |
| 84 | + assert another_group.__suppress_context__ is False |
0 commit comments