File: apptest_operator.py

package info (click to toggle)
pypy3 7.3.19%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 212,236 kB
  • sloc: python: 2,098,316; ansic: 540,565; sh: 21,462; asm: 14,419; cpp: 4,451; makefile: 4,209; objc: 761; xml: 530; exp: 499; javascript: 314; pascal: 244; lisp: 45; csh: 12; awk: 4
file content (51 lines) | stat: -rw-r--r-- 1,282 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import pytest

def run_async(coro):
    buffer = []
    result = None
    while True:
        try:
            buffer.append(coro.send(None))
        except StopIteration as ex:
            result = ex.args[0] if ex.args else None
            break
    return buffer, result

def test_error_aiter_anext():
    with pytest.raises(TypeError) as info:
        aiter(1)
    assert "'int' object is not an async iterable" in str(info.value)

    with pytest.raises(TypeError) as info:
        anext(1)
    assert "'int' object is not an async iterator" in str(info.value)

    class BadAsyncIterable:
        def __aiter__(self):
            return 'abc'

    with pytest.raises(TypeError) as info:
        aiter(BadAsyncIterable())
    assert "aiter() returned not an async iterator of type 'str'" in str(info.value)


def test_aiter_anext():
    async def foo():
        yield 1
        yield 2

    async def run():
        it = aiter(foo())
        val1 = await anext(it)
        assert val1 == 1
        val2 = await anext(it)
        assert val2 == 2

    run_async(run())

def test_getattr_etc_error():
    with raises(TypeError) as info:
        getattr(test_getattr_etc_error, b'__code__')
    assert "getattr(): attribute name must be string, not 'bytes'" in str(info.value)