| 1 |
import dis |
| 2 |
import io |
| 3 |
import textwrap |
| 4 |
import types |
| 5 |
|
| 6 |
from test.support.bytecode_helper import AssemblerTestCase |
| 7 |
|
| 8 |
|
| 9 |
# Tests for the code-object creation stage of the compiler. |
| 10 |
|
| 11 |
class IsolatedAssembleTests(AssemblerTestCase): |
| 12 |
|
| 13 |
def complete_metadata(self, metadata, filename="myfile.py"): |
| 14 |
if metadata is None: |
| 15 |
metadata = {} |
| 16 |
for key in ['name', 'qualname']: |
| 17 |
metadata.setdefault(key, key) |
| 18 |
for key in ['consts']: |
| 19 |
metadata.setdefault(key, []) |
| 20 |
for key in ['names', 'varnames', 'cellvars', 'freevars', 'fasthidden']: |
| 21 |
metadata.setdefault(key, {}) |
| 22 |
for key in ['argcount', 'posonlyargcount', 'kwonlyargcount']: |
| 23 |
metadata.setdefault(key, 0) |
| 24 |
metadata.setdefault('firstlineno', 1) |
| 25 |
metadata.setdefault('filename', filename) |
| 26 |
return metadata |
| 27 |
|
| 28 |
def insts_to_code_object(self, insts, metadata): |
| 29 |
metadata = self.complete_metadata(metadata) |
| 30 |
seq = self.seq_from_insts(insts) |
| 31 |
return self.get_code_object(metadata['filename'], seq, metadata) |
| 32 |
|
| 33 |
def assemble_test(self, insts, metadata, expected): |
| 34 |
co = self.insts_to_code_object(insts, metadata) |
| 35 |
self.assertIsInstance(co, types.CodeType) |
| 36 |
|
| 37 |
expected_metadata = {} |
| 38 |
for key, value in metadata.items(): |
| 39 |
if key == "fasthidden": |
| 40 |
# not exposed on code object |
| 41 |
continue |
| 42 |
if isinstance(value, list): |
| 43 |
expected_metadata[key] = tuple(value) |
| 44 |
elif isinstance(value, dict): |
| 45 |
expected_metadata[key] = tuple(value.keys()) |
| 46 |
else: |
| 47 |
expected_metadata[key] = value |
| 48 |
|
| 49 |
for key, value in expected_metadata.items(): |
| 50 |
self.assertEqual(getattr(co, "co_" + key), value) |
| 51 |
|
| 52 |
f = types.FunctionType(co, {}) |
| 53 |
for args, res in expected.items(): |
| 54 |
self.assertEqual(f(*args), res) |
| 55 |
|
| 56 |
def test_simple_expr(self): |
| 57 |
metadata = { |
| 58 |
'filename' : 'avg.py', |
| 59 |
'name' : 'avg', |
| 60 |
'qualname' : 'stats.avg', |
| 61 |
'consts' : {2 : 0}, |
| 62 |
'argcount' : 2, |
| 63 |
'varnames' : {'x' : 0, 'y' : 1}, |
| 64 |
} |
| 65 |
|
| 66 |
# code for "return (x+y)/2" |
| 67 |
insts = [ |
| 68 |
('RESUME', 0), |
| 69 |
('LOAD_FAST', 0, 1), # 'x' |
| 70 |
('LOAD_FAST', 1, 1), # 'y' |
| 71 |
('BINARY_OP', 0, 1), # '+' |
| 72 |
('LOAD_CONST', 0, 1), # 2 |
| 73 |
('BINARY_OP', 11, 1), # '/' |
| 74 |
('RETURN_VALUE', None, 1), |
| 75 |
] |
| 76 |
expected = {(3, 4) : 3.5, (-100, 200) : 50, (10, 18) : 14} |
| 77 |
self.assemble_test(insts, metadata, expected) |
| 78 |
|
| 79 |
|
| 80 |
def test_expression_with_pseudo_instruction_load_closure(self): |
| 81 |
|
| 82 |
def mod_two(x): |
| 83 |
def inner(): |
| 84 |
return x |
| 85 |
return inner() % 2 |
| 86 |
|
| 87 |
inner_code = mod_two.__code__.co_consts[0] |
| 88 |
assert isinstance(inner_code, types.CodeType) |
| 89 |
|
| 90 |
metadata = { |
| 91 |
'filename' : 'mod_two.py', |
| 92 |
'name' : 'mod_two', |
| 93 |
'qualname' : 'nested.mod_two', |
| 94 |
'cellvars' : {'x' : 0}, |
| 95 |
'consts': {None: 0, inner_code: 1, 2: 2}, |
| 96 |
'argcount' : 1, |
| 97 |
'varnames' : {'x' : 0}, |
| 98 |
} |
| 99 |
|
| 100 |
instructions = [ |
| 101 |
('RESUME', 0,), |
| 102 |
('LOAD_CLOSURE', 0, 1), |
| 103 |
('BUILD_TUPLE', 1, 1), |
| 104 |
('LOAD_CONST', 1, 1), |
| 105 |
('MAKE_FUNCTION', None, 2), |
| 106 |
('SET_FUNCTION_ATTRIBUTE', 8, 2), |
| 107 |
('PUSH_NULL', None, 1), |
| 108 |
('CALL', 0, 2), # (lambda: x)() |
| 109 |
('LOAD_CONST', 2, 2), # 2 |
| 110 |
('BINARY_OP', 6, 2), # % |
| 111 |
('RETURN_VALUE', None, 2) |
| 112 |
] |
| 113 |
|
| 114 |
expected = {(0,): 0, (1,): 1, (2,): 0, (120,): 0, (121,): 1} |
| 115 |
self.assemble_test(instructions, metadata, expected) |
| 116 |
|
| 117 |
|
| 118 |
def test_exception_table(self): |
| 119 |
metadata = { |
| 120 |
'filename' : 'exc.py', |
| 121 |
'name' : 'exc', |
| 122 |
'consts' : {2 : 0}, |
| 123 |
} |
| 124 |
|
| 125 |
# code for "try: pass\n except: pass" |
| 126 |
insts = [ |
| 127 |
('RESUME', 0), |
| 128 |
('SETUP_FINALLY', 4), |
| 129 |
('LOAD_CONST', 0), |
| 130 |
('RETURN_VALUE', None), |
| 131 |
('SETUP_CLEANUP', 10), |
| 132 |
('PUSH_EXC_INFO', None), |
| 133 |
('POP_TOP', None), |
| 134 |
('POP_EXCEPT', None), |
| 135 |
('LOAD_CONST', 0), |
| 136 |
('RETURN_VALUE', None), |
| 137 |
('COPY', 3), |
| 138 |
('POP_EXCEPT', None), |
| 139 |
('RERAISE', 1), |
| 140 |
] |
| 141 |
co = self.insts_to_code_object(insts, metadata) |
| 142 |
output = io.StringIO() |
| 143 |
dis.dis(co, file=output) |
| 144 |
exc_table = textwrap.dedent(""" |
| 145 |
ExceptionTable: |
| 146 |
L1 to L2 -> L2 [0] |
| 147 |
L2 to L3 -> L3 [1] lasti |
| 148 |
""") |
| 149 |
self.assertEndsWith(output.getvalue(), exc_table) |