| 1 | import pytest |
| 2 | |
| 3 | def run_async(coro): |
| 4 | buffer = [] |
| 5 | result = None |
| 6 | while True: |
| 7 | try: |
| 8 | buffer.append(coro.send(None)) |
| 9 | except StopIteration as ex: |
| 10 | result = ex.args[0] if ex.args else None |
| 11 | break |
| 12 | return buffer, result |
| 13 | |
| 14 | def test_error_aiter_anext(): |
| 15 | with pytest.raises(TypeError) as info: |
| 16 | aiter(1) |
| 17 | assert "'int' object is not an async iterable" in str(info.value) |
| 18 | |
| 19 | with pytest.raises(TypeError) as info: |
| 20 | anext(1) |
| 21 | assert "'int' object is not an async iterator" in str(info.value) |
| 22 | |
| 23 | class BadAsyncIterable: |
| 24 | def __aiter__(self): |
| 25 | return 'abc' |
| 26 | |
| 27 | with pytest.raises(TypeError) as info: |
| 28 | aiter(BadAsyncIterable()) |
| 29 | assert "aiter() returned not an async iterator of type 'str'" in str(info.value) |
| 30 | |
| 31 | |
| 32 | def test_aiter_anext(): |
| 33 | async def foo(): |
| 34 | yield 1 |
| 35 | yield 2 |
| 36 | |
| 37 | async def run(): |
| 38 | it = aiter(foo()) |
| 39 | val1 = await anext(it) |
| 40 | assert val1 == 1 |
| 41 | val2 = await anext(it) |
| 42 | assert val2 == 2 |
| 43 | |
| 44 | run_async(run()) |
| 45 | |
| 46 | def test_getattr_etc_error(): |
| 47 | with raises(TypeError) as info: |
| 48 | getattr(test_getattr_etc_error, b'__code__') |
| 49 | assert "attribute name must be string, not 'bytes'" in str(info.value) |
| 50 | |
| 51 |