|
1 | 1 | import pytest
|
2 | 2 |
|
3 |
| -from . import aclosing, async_generator, yield_ |
| 3 | +from .. import aclosing, async_generator, yield_, asynccontextmanager |
4 | 4 |
|
5 | 5 |
|
6 | 6 | @async_generator
|
@@ -34,3 +34,158 @@ async def test_aclosing():
|
34 | 34 | except ValueError:
|
35 | 35 | pass
|
36 | 36 | assert closed_slot[0]
|
| 37 | + |
| 38 | + |
| 39 | +@pytest.mark.asyncio |
| 40 | +async def test_contextmanager_do_not_unchain_non_stopiteration_exceptions(): |
| 41 | + @asynccontextmanager |
| 42 | + @async_generator |
| 43 | + async def manager_issue29692(): |
| 44 | + try: |
| 45 | + await yield_() |
| 46 | + except Exception as exc: |
| 47 | + raise RuntimeError('issue29692:Chained') from exc |
| 48 | + |
| 49 | + with pytest.raises(RuntimeError) as excinfo: |
| 50 | + async with manager_issue29692(): |
| 51 | + raise ZeroDivisionError |
| 52 | + assert excinfo.value.args[0] == 'issue29692:Chained' |
| 53 | + assert isinstance(excinfo.value.__cause__, ZeroDivisionError) |
| 54 | + |
| 55 | + # This is a little funky because of implementation details in |
| 56 | + # async_generator It can all go away once we stop supporting Python3.5 |
| 57 | + with pytest.raises(RuntimeError) as excinfo: |
| 58 | + async with manager_issue29692(): |
| 59 | + exc = StopIteration('issue29692:Unchained') |
| 60 | + raise exc |
| 61 | + assert excinfo.value.args[0] == 'issue29692:Chained' |
| 62 | + cause = excinfo.value.__cause__ |
| 63 | + assert cause.args[0] == 'generator raised StopIteration' |
| 64 | + assert cause.__cause__ is exc |
| 65 | + |
| 66 | + with pytest.raises(StopAsyncIteration) as excinfo: |
| 67 | + async with manager_issue29692(): |
| 68 | + raise StopAsyncIteration('issue29692:Unchained') |
| 69 | + assert excinfo.value.args[0] == 'issue29692:Unchained' |
| 70 | + assert excinfo.value.__cause__ is None |
| 71 | + |
| 72 | + @asynccontextmanager |
| 73 | + @async_generator |
| 74 | + async def noop_async_context_manager(): |
| 75 | + await yield_() |
| 76 | + |
| 77 | + with pytest.raises(StopIteration): |
| 78 | + async with noop_async_context_manager(): |
| 79 | + raise StopIteration |
| 80 | + |
| 81 | + |
| 82 | +# Native async generators are only available from Python 3.6 and onwards |
| 83 | +nativeasyncgenerators = True |
| 84 | +try: |
| 85 | + exec( |
| 86 | + """ |
| 87 | +@asynccontextmanager |
| 88 | +async def manager_issue29692_2(): |
| 89 | + try: |
| 90 | + yield |
| 91 | + except Exception as exc: |
| 92 | + raise RuntimeError('issue29692:Chained') from exc |
| 93 | +""" |
| 94 | + ) |
| 95 | +except SyntaxError: |
| 96 | + nativeasyncgenerators = False |
| 97 | + |
| 98 | + |
| 99 | +@pytest.mark.skipif( |
| 100 | + not nativeasyncgenerators, |
| 101 | + reason="Python < 3.6 doesn't have native async generators" |
| 102 | +) |
| 103 | +@pytest.mark.asyncio |
| 104 | +async def test_native_contextmanager_do_not_unchain_non_stopiteration_exceptions( |
| 105 | +): |
| 106 | + |
| 107 | + with pytest.raises(RuntimeError) as excinfo: |
| 108 | + async with manager_issue29692_2(): |
| 109 | + raise ZeroDivisionError |
| 110 | + assert excinfo.value.args[0] == 'issue29692:Chained' |
| 111 | + assert isinstance(excinfo.value.__cause__, ZeroDivisionError) |
| 112 | + |
| 113 | + for cls in [StopIteration, StopAsyncIteration]: |
| 114 | + with pytest.raises(cls) as excinfo: |
| 115 | + async with manager_issue29692_2(): |
| 116 | + raise cls('issue29692:Unchained') |
| 117 | + assert excinfo.value.args[0] == 'issue29692:Unchained' |
| 118 | + assert excinfo.value.__cause__ is None |
| 119 | + |
| 120 | + |
| 121 | +@pytest.mark.asyncio |
| 122 | +async def test_asynccontextmanager_exception_passthrough(): |
| 123 | + # This was the cause of annoying coverage flapping, see gh-140 |
| 124 | + @asynccontextmanager |
| 125 | + @async_generator |
| 126 | + async def noop_async_context_manager(): |
| 127 | + await yield_() |
| 128 | + |
| 129 | + for exc_type in [StopAsyncIteration, RuntimeError, ValueError]: |
| 130 | + with pytest.raises(exc_type): |
| 131 | + async with noop_async_context_manager(): |
| 132 | + raise exc_type |
| 133 | + |
| 134 | + |
| 135 | +@pytest.mark.asyncio |
| 136 | +async def test_asynccontextmanager_catches_exception(): |
| 137 | + @asynccontextmanager |
| 138 | + @async_generator |
| 139 | + async def catch_it(): |
| 140 | + with pytest.raises(ValueError): |
| 141 | + await yield_() |
| 142 | + |
| 143 | + async with catch_it(): |
| 144 | + raise ValueError |
| 145 | + |
| 146 | + |
| 147 | +@pytest.mark.asyncio |
| 148 | +async def test_asynccontextmanager_no_yield(): |
| 149 | + @asynccontextmanager |
| 150 | + @async_generator |
| 151 | + async def yeehaw(): |
| 152 | + pass |
| 153 | + |
| 154 | + with pytest.raises(RuntimeError) as excinfo: |
| 155 | + async with yeehaw(): |
| 156 | + assert False # pragma: no cover |
| 157 | + |
| 158 | + assert "didn't yield" in str(excinfo.value) |
| 159 | + |
| 160 | + |
| 161 | +@pytest.mark.asyncio |
| 162 | +async def test_asynccontextmanager_too_many_yields(): |
| 163 | + @asynccontextmanager |
| 164 | + @async_generator |
| 165 | + async def doubleyield(): |
| 166 | + try: |
| 167 | + await yield_() |
| 168 | + except Exception: |
| 169 | + pass |
| 170 | + await yield_() |
| 171 | + |
| 172 | + with pytest.raises(RuntimeError) as excinfo: |
| 173 | + async with doubleyield(): |
| 174 | + pass |
| 175 | + |
| 176 | + assert "didn't stop" in str(excinfo.value) |
| 177 | + |
| 178 | + with pytest.raises(RuntimeError) as excinfo: |
| 179 | + async with doubleyield(): |
| 180 | + raise ValueError |
| 181 | + |
| 182 | + assert "didn't stop after athrow" in str(excinfo.value) |
| 183 | + |
| 184 | + |
| 185 | +@pytest.mark.asyncio |
| 186 | +async def test_asynccontextmanager_requires_asyncgenfunction(): |
| 187 | + with pytest.raises(TypeError): |
| 188 | + |
| 189 | + @asynccontextmanager |
| 190 | + def syncgen(): # pragma: no cover |
| 191 | + yield |
0 commit comments