You can play with the bytecode.
byteplay.py
# -*- coding: utf-8 -*-
from byteplay import Code
from pprint import pprint
def f(a, b):
print (a, b)
f(3, 5)
# (3, 5)
c = Code.from_code(f.func_code)
pprint(c.code)
'''
[(SetLineno, 7),
(LOAD_FAST, 'a'),
(LOAD_FAST, 'b'),
(BUILD_TUPLE, 2),
(PRINT_ITEM, None),
(PRINT_NEWLINE, None),
(LOAD_CONST, None),
(RETURN_VALUE, None)]
'''
Because I want to manipulate bytecode
c.code[3:3] = [(ROT_TWO, None)]
Was added to the program,
NameError: name 'ROT_TWO' is not defined
I get an error ...
Recommended Posts