|
| 1 | +# Regression: exception chaining — __cause__ (explicit, via `raise X from Y`), |
| 2 | +# implicit __context__ (the exception being handled when a new one is raised), |
| 3 | +# and __suppress_context__. Also covers the exception-stack hygiene that makes |
| 4 | +# these reliable: internally-consumed StopIterations no longer linger, so a bare |
| 5 | +# `raise` outside a handler is a RuntimeError and __context__ isn't spuriously |
| 6 | +# populated. |
| 7 | + |
| 8 | + |
| 9 | +# `raise X from Y` sets __cause__ to the instance and suppresses context. |
| 10 | +try: |
| 11 | + try: |
| 12 | + raise ValueError("inner") |
| 13 | + except ValueError as e: |
| 14 | + raise KeyError("outer") from e |
| 15 | +except KeyError as k: |
| 16 | + assert isinstance(k.__cause__, ValueError), k.__cause__ |
| 17 | + assert str(k.__cause__) == "inner", str(k.__cause__) |
| 18 | + assert k.__suppress_context__ is True, k.__suppress_context__ |
| 19 | + # __context__ is still set implicitly (suppress only affects display). |
| 20 | + assert isinstance(k.__context__, ValueError), k.__context__ |
| 21 | + |
| 22 | + |
| 23 | +# Implicit chaining without `from`: __context__ is the handled exception. |
| 24 | +try: |
| 25 | + try: |
| 26 | + raise ValueError("v1") |
| 27 | + except ValueError: |
| 28 | + raise KeyError("k1") |
| 29 | +except KeyError as k: |
| 30 | + assert k.__cause__ is None, k.__cause__ |
| 31 | + assert isinstance(k.__context__, ValueError), k.__context__ |
| 32 | + assert str(k.__context__) == "v1", str(k.__context__) |
| 33 | + assert k.__suppress_context__ is False, k.__suppress_context__ |
| 34 | + |
| 35 | + |
| 36 | +# `raise X from None` -> cause None, still suppressed. |
| 37 | +try: |
| 38 | + raise KeyError("k") from None |
| 39 | +except KeyError as k: |
| 40 | + assert k.__cause__ is None, k.__cause__ |
| 41 | + assert k.__suppress_context__ is True, k.__suppress_context__ |
| 42 | + |
| 43 | + |
| 44 | +# Plain exception raised outside any handler: no cause, no context. |
| 45 | +try: |
| 46 | + raise ValueError("plain") |
| 47 | +except ValueError as e: |
| 48 | + assert e.__cause__ is None, e.__cause__ |
| 49 | + assert e.__context__ is None, e.__context__ |
| 50 | + assert e.__suppress_context__ is False, e.__suppress_context__ |
| 51 | + |
| 52 | + |
| 53 | +# A bare `raise` with no active exception is a RuntimeError (not an abort, and |
| 54 | +# not a stale leftover exception). |
| 55 | +try: |
| 56 | + raise |
| 57 | +except RuntimeError as e: |
| 58 | + assert str(e) == "No active exception to reraise", str(e) |
| 59 | + |
| 60 | + |
| 61 | +# Iterating a generator / comprehensions while handling an exception must not |
| 62 | +# disturb the active exception (exception-stack hygiene). |
| 63 | +def gen(): |
| 64 | + yield 1 |
| 65 | + yield 2 |
| 66 | + yield 3 |
| 67 | + |
| 68 | + |
| 69 | +try: |
| 70 | + raise ValueError("active") |
| 71 | +except ValueError as e: |
| 72 | + assert set(gen()) == {1, 2, 3} |
| 73 | + assert [x for x in range(4)] == [0, 1, 2, 3] |
| 74 | + assert {k: k * k for k in range(3)} == {0: 0, 1: 1, 2: 4} |
| 75 | + assert isinstance(e, ValueError) and str(e) == "active" |
| 76 | + |
| 77 | + |
| 78 | +# Chaining attributes are writable; setting __cause__ also suppresses context. |
| 79 | +try: |
| 80 | + raise ValueError("x") |
| 81 | +except ValueError as e: |
| 82 | + ctx = RuntimeError("ctx") |
| 83 | + e.__context__ = ctx |
| 84 | + assert e.__context__ is ctx |
| 85 | + e.__cause__ = ctx |
| 86 | + assert e.__cause__ is ctx |
| 87 | + assert e.__suppress_context__ is True |
| 88 | + e.__suppress_context__ = False |
| 89 | + assert e.__suppress_context__ is False |
| 90 | + |
| 91 | + |
| 92 | +print("EXCEPTION_CHAINING_OK") |
0 commit comments