Created
April 20, 2019 14:41
-
-
Save hankliu5/1fe1d8ca5aa5ba68d5fbf36bdc45b1c4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ast | |
class CrazyTransformer(ast.NodeTransformer): | |
def visit_BinOp(self, node): | |
print(node.__dict__) | |
node.op = ast.Mult() | |
print(node.__dict__) | |
return node | |
expr = """ | |
def add(arg1, arg2): | |
return arg1 + arg2 | |
print(add(4, 5)) | |
""" | |
expr_ast = ast.parse(expr) | |
transformer = CrazyTransformer() | |
unmodified = ast.parse(expr) | |
exec(compile(unmodified, '<string>', 'exec')) | |
modified = transformer.visit(expr_ast) | |
exec(compile(modified, '<string>', 'exec')) | |
s = "print('hello, world')" | |
s_ast = ast.parse(s) | |
new_ast = ast.parse(s) | |
new_ast.body[0].value.args[0].s = "hello, hank" | |
exec(compile(s_ast, 'string', 'exec')) | |
exec(compile(new_ast, 'string', 'exec')) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from types import CodeType | |
s = "print('hello, world')" | |
c = compile(s, 'test.py', 'exec') | |
exec(c) | |
new_co_consts = ('hello, hank', None) | |
new_c = CodeType(c.co_argcount, c.co_kwonlyargcount, c.co_nlocals, c.co_stacksize, c.co_flags, c.co_code, | |
new_co_consts, c.co_names, c.co_varnames, c.co_filename, c.co_name, c.co_firstlineno, | |
c.co_lnotab, c.co_freevars, c.co_cellvars) | |
exec(new_c) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from types import CodeType | |
expr = """ | |
def add(arg1, arg2): | |
return arg1 + arg2 | |
print(add(4, 5)) | |
""" | |
patch = """ | |
def multi(a, b): | |
return a * b | |
""" | |
expr_code = compile(expr, '<string>', 'exec') | |
patch_code = compile(patch, '<string>', 'exec') | |
new_co_consts = expr_code.co_consts | |
new_co_consts = (patch_code.co_consts[0],) + expr_code.co_consts[1:] | |
new_c = CodeType(expr_code.co_argcount, | |
expr_code.co_kwonlyargcount, | |
expr_code.co_nlocals, | |
expr_code.co_stacksize, | |
expr_code.co_flags, | |
expr_code.co_code, | |
new_co_consts, | |
expr_code.co_names, | |
expr_code.co_varnames, | |
expr_code.co_filename, | |
expr_code.co_name, | |
expr_code.co_firstlineno, | |
expr_code.co_lnotab, | |
expr_code.co_freevars, | |
expr_code.co_cellvars | |
) | |
exec(expr_code) | |
exec(new_c) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment