Debian Codegraph @

extra_tests/test_dis.py

raw symbols SCIP
1 import dis
2 import io
3 import pytest
4
5 def compare_lines(t1, t2):
6 for l1, l2 in zip(t1.split('\n'), t2.split('\n')):
7 if 'LOAD_CONST' in l1:
8 # some small variation is OK
9 assert 'LOAD_CONST' in l2
10 else:
11 assert l1.strip() == l2.strip()
12
13
14 @pytest.mark.skip("PyPy does not really try to be compatible")
15 def test_asyncFor():
16 co = compile('''
17 async def f():
18 class Iterable:
19 def __aiter__(self):
20 return self
21
22 async def __anext__(self):
23 raise StopAsyncIteration
24
25
26 async for i in Iterable():
27 pass
28 else:
29 print('ok')
30 ''', '<str>', 'exec')
31 # Python does not resursively call dis._disassemble, but we want
32 # the dis of the "async for", which we can only access inside a "async def"
33 result = io.StringIO()
34 dis.dis(co.co_consts[0].co_code, file=result)
35 cpython310 = """0 GEN_START 1
36 2 LOAD_BUILD_CLASS
37 4 LOAD_CONST 1 (1)
38 6 LOAD_CONST 2 (2)
39 8 MAKE_FUNCTION 0
40 10 LOAD_CONST 2 (2)
41 12 CALL_FUNCTION 2
42 14 STORE_FAST 0 (0)
43 16 LOAD_FAST 0 (0)
44 18 CALL_FUNCTION 0
45 20 GET_AITER
46 >> 22 SETUP_FINALLY 6 (to 36)
47 24 GET_ANEXT
48 26 LOAD_CONST 0 (0)
49 28 YIELD_FROM
50 30 POP_BLOCK
51 32 STORE_FAST 1 (1)
52 34 JUMP_ABSOLUTE 11 (to 22)
53 >> 36 END_ASYNC_FOR
54 38 LOAD_GLOBAL 0 (0)
55 40 LOAD_CONST 3 (3)
56 42 CALL_FUNCTION 1
57 44 POP_TOP
58 46 LOAD_CONST 0 (0)
59 48 RETURN_VALUE
60 """
61
62 compare_lines(cpython310, result.getvalue())