def test_to_code(self): # test resolution of jump labels bytecode = ControlFlowGraph() bytecode.first_lineno = 3 bytecode.argcount = 3 bytecode.kwonlyargcount = 2 bytecode._stacksize = 1 bytecode.name = 'func' bytecode.filename = 'hello.py' bytecode.flags = 0x43 bytecode.argnames = ('arg', 'arg2', 'arg3', 'kwonly', 'kwonly2') bytecode.docstring = None block0 = bytecode[0] block1 = bytecode.add_block() block2 = bytecode.add_block() block0.extend([ Instr('LOAD_FAST', 'x', lineno=4), Instr('POP_JUMP_IF_FALSE', block2, lineno=4) ]) block1.extend([ Instr('LOAD_FAST', 'arg', lineno=5), Instr('STORE_FAST', 'x', lineno=5) ]) block2.extend([ Instr('LOAD_CONST', 3, lineno=6), Instr('STORE_FAST', 'x', lineno=6), Instr('LOAD_FAST', 'x', lineno=7), Instr('RETURN_VALUE', lineno=7) ]) expected = (b'|\x05\x00' b'r\x0c\x00' b'|\x00\x00' b'}\x05\x00' b'd\x01\x00' b'}\x05\x00' b'|\x05\x00' b'S') code = bytecode.to_bytecode().to_code() self.assertEqual(code.co_consts, (None, 3)) self.assertEqual(code.co_argcount, 3) self.assertEqual(code.co_kwonlyargcount, 2) self.assertEqual(code.co_nlocals, 6) self.assertEqual(code.co_stacksize, 1) # FIXME: don't use hardcoded constants self.assertEqual(code.co_flags, 0x43) self.assertEqual(code.co_code, expected) self.assertEqual(code.co_names, ()) self.assertEqual(code.co_varnames, ('arg', 'arg2', 'arg3', 'kwonly', 'kwonly2', 'x')) self.assertEqual(code.co_filename, 'hello.py') self.assertEqual(code.co_name, 'func') self.assertEqual(code.co_firstlineno, 3)
def test_to_code(self): # test resolution of jump labels bytecode = ControlFlowGraph() bytecode.first_lineno = 3 bytecode.argcount = 3 bytecode.kwonlyargcount = 2 bytecode._stacksize = 1 bytecode.name = 'func' bytecode.filename = 'hello.py' bytecode.flags = 0x43 bytecode.argnames = ('arg', 'arg2', 'arg3', 'kwonly', 'kwonly2') bytecode.docstring = None block0 = bytecode[0] block1 = bytecode.add_block() block2 = bytecode.add_block() block0.extend([Instr('LOAD_FAST', 'x', lineno=4), Instr('POP_JUMP_IF_FALSE', block2, lineno=4)]) block1.extend([Instr('LOAD_FAST', 'arg', lineno=5), Instr('STORE_FAST', 'x', lineno=5)]) block2.extend([Instr('LOAD_CONST', 3, lineno=6), Instr('STORE_FAST', 'x', lineno=6), Instr('LOAD_FAST', 'x', lineno=7), Instr('RETURN_VALUE', lineno=7)]) expected = (b'|\x05\x00' b'r\x0c\x00' b'|\x00\x00' b'}\x05\x00' b'd\x01\x00' b'}\x05\x00' b'|\x05\x00' b'S') code = bytecode.to_bytecode().to_code() self.assertEqual(code.co_consts, (None, 3)) self.assertEqual(code.co_argcount, 3) self.assertEqual(code.co_kwonlyargcount, 2) self.assertEqual(code.co_nlocals, 6) self.assertEqual(code.co_stacksize, 1) # FIXME: don't use hardcoded constants self.assertEqual(code.co_flags, 0x43) self.assertEqual(code.co_code, expected) self.assertEqual(code.co_names, ()) self.assertEqual(code.co_varnames, ('arg', 'arg2', 'arg3', 'kwonly', 'kwonly2', 'x')) self.assertEqual(code.co_filename, 'hello.py') self.assertEqual(code.co_name, 'func') self.assertEqual(code.co_firstlineno, 3)
def test_to_code(self): # test resolution of jump labels bytecode = ControlFlowGraph() bytecode.first_lineno = 3 bytecode.argcount = 3 if sys.version_info > (3, 8): bytecode.posonlyargcount = 0 bytecode.kwonlyargcount = 2 bytecode.name = 'func' bytecode.filename = 'hello.py' bytecode.flags = 0x43 bytecode.argnames = ('arg', 'arg2', 'arg3', 'kwonly', 'kwonly2') bytecode.docstring = None block0 = bytecode[0] block1 = bytecode.add_block() block2 = bytecode.add_block() block0.extend([Instr('LOAD_FAST', 'x', lineno=4), Instr('POP_JUMP_IF_FALSE', block2, lineno=4)]) block1.extend([Instr('LOAD_FAST', 'arg', lineno=5), Instr('STORE_FAST', 'x', lineno=5)]) block2.extend([Instr('LOAD_CONST', 3, lineno=6), Instr('STORE_FAST', 'x', lineno=6), Instr('LOAD_FAST', 'x', lineno=7), Instr('RETURN_VALUE', lineno=7)]) if WORDCODE: expected = (b'|\x05' b'r\x08' b'|\x00' b'}\x05' b'd\x01' b'}\x05' b'|\x05' b'S\x00') else: expected = (b'|\x05\x00' b'r\x0c\x00' b'|\x00\x00' b'}\x05\x00' b'd\x01\x00' b'}\x05\x00' b'|\x05\x00' b'S') code = bytecode.to_code() self.assertEqual(code.co_consts, (None, 3)) self.assertEqual(code.co_argcount, 3) if sys.version_info > (3, 8): self.assertEqual(code.co_posonlyargcount, 0) self.assertEqual(code.co_kwonlyargcount, 2) self.assertEqual(code.co_nlocals, 6) self.assertEqual(code.co_stacksize, 1) # FIXME: don't use hardcoded constants self.assertEqual(code.co_flags, 0x43) self.assertEqual(code.co_code, expected) self.assertEqual(code.co_names, ()) self.assertEqual(code.co_varnames, ('arg', 'arg2', 'arg3', 'kwonly', 'kwonly2', 'x')) self.assertEqual(code.co_filename, 'hello.py') self.assertEqual(code.co_name, 'func') self.assertEqual(code.co_firstlineno, 3) # verify stacksize argument is honored explicit_stacksize = code.co_stacksize + 42 code = bytecode.to_code(stacksize=explicit_stacksize) self.assertEqual(code.co_stacksize, explicit_stacksize)
def test_to_code(self): # test resolution of jump labels bytecode = ControlFlowGraph() bytecode.first_lineno = 3 bytecode.argcount = 3 if sys.version_info > (3, 8): bytecode.posonlyargcount = 0 bytecode.kwonlyargcount = 2 bytecode.name = "func" bytecode.filename = "hello.py" bytecode.flags = 0x43 bytecode.argnames = ("arg", "arg2", "arg3", "kwonly", "kwonly2") bytecode.docstring = None block0 = bytecode[0] block1 = bytecode.add_block() block2 = bytecode.add_block() block0.extend( [ Instr("LOAD_FAST", "x", lineno=4), Instr("POP_JUMP_IF_FALSE", block2, lineno=4), ] ) block1.extend( [Instr("LOAD_FAST", "arg", lineno=5), Instr("STORE_FAST", "x", lineno=5)] ) block2.extend( [ Instr("LOAD_CONST", 3, lineno=6), Instr("STORE_FAST", "x", lineno=6), Instr("LOAD_FAST", "x", lineno=7), Instr("RETURN_VALUE", lineno=7), ] ) if WORDCODE: expected = ( b"|\x05" b"r\x08" b"|\x00" b"}\x05" b"d\x01" b"}\x05" b"|\x05" b"S\x00" ) else: expected = ( b"|\x05\x00" b"r\x0c\x00" b"|\x00\x00" b"}\x05\x00" b"d\x01\x00" b"}\x05\x00" b"|\x05\x00" b"S" ) code = bytecode.to_code() self.assertEqual(code.co_consts, (None, 3)) self.assertEqual(code.co_argcount, 3) if sys.version_info > (3, 8): self.assertEqual(code.co_posonlyargcount, 0) self.assertEqual(code.co_kwonlyargcount, 2) self.assertEqual(code.co_nlocals, 6) self.assertEqual(code.co_stacksize, 1) # FIXME: don't use hardcoded constants self.assertEqual(code.co_flags, 0x43) self.assertEqual(code.co_code, expected) self.assertEqual(code.co_names, ()) self.assertEqual( code.co_varnames, ("arg", "arg2", "arg3", "kwonly", "kwonly2", "x") ) self.assertEqual(code.co_filename, "hello.py") self.assertEqual(code.co_name, "func") self.assertEqual(code.co_firstlineno, 3) # verify stacksize argument is honored explicit_stacksize = code.co_stacksize + 42 code = bytecode.to_code(stacksize=explicit_stacksize) self.assertEqual(code.co_stacksize, explicit_stacksize)