1
0
Fork 0
cursed-snakes/namespace-inject.py

93 lines
2 KiB
Python
Raw Normal View History

2023-04-03 23:21:01 +00:00
import ast
import inspect
from textwrap import dedent
from typing import Any, Callable
2023-04-04 19:20:21 +00:00
#################
# MAGIC SECTION #
#################
2023-04-03 23:21:01 +00:00
def ast_dump(sobject) -> None:
2023-04-04 19:20:21 +00:00
print(f"Dumping AST of {sobject}")
2023-04-03 23:21:01 +00:00
print(ast.dump(ast.parse(
2023-04-04 19:20:21 +00:00
dedent(inspect.getsource(sobject)),
2023-04-03 23:21:01 +00:00
), indent=4))
2023-04-03 22:23:23 +00:00
def shoehorn(f: Callable) -> Callable:
2023-04-04 19:20:21 +00:00
# ast_dump(f)
2023-04-03 23:21:01 +00:00
def foo() -> None:
print(f"{pstr = }") # type:ignore # noqa: F821
2023-04-04 19:20:21 +00:00
# ast_dump(foo)
foo_ast = ast.parse(dedent(inspect.getsource(foo)))
foo_fn = next(
x
for x in ast.walk(foo_ast)
if isinstance(x, ast.FunctionDef) and x.name == "foo"
)
2023-04-03 23:21:01 +00:00
class Shoehorn(ast.NodeTransformer):
def visit_FunctionDef(self, node: ast.FunctionDef) -> Any:
2023-04-04 19:20:21 +00:00
if node.name == f.__name__:
node.body.insert(0, foo_fn)
node.decorator_list = [
decorator
for decorator in node.decorator_list
if not (
isinstance(decorator, ast.Name)
and decorator.id == shoehorn.__name__
)
]
2023-04-03 23:21:01 +00:00
return node
2023-04-04 19:20:21 +00:00
f_ast = ast.parse(dedent(inspect.getsource(f)))
2023-04-03 23:21:01 +00:00
new_f_ast = ast.fix_missing_locations(Shoehorn().visit(f_ast))
2023-04-04 19:20:21 +00:00
# print(ast.dump(new_f_ast, indent=4))
2023-04-03 23:21:01 +00:00
2023-04-04 19:20:21 +00:00
new_f_scope = {}
exec(compile(new_f_ast, "<string>", "exec"), new_f_scope)
2023-04-03 16:50:34 +00:00
2023-04-04 19:20:21 +00:00
return new_f_scope[f.__name__]
##################
# NORMAL SECTION #
##################
def foo():
raise NotImplementedError()
def func1(pstr: str) -> None:
def foo() -> None:
print(f"{pstr = }")
foo()
2023-04-03 16:50:34 +00:00
2023-04-03 22:23:23 +00:00
def func2(pstr: str) -> None:
2023-04-04 19:20:21 +00:00
foo()
@shoehorn
def func3(pstr: str) -> None:
foo()
def func_info(f: Callable) -> None:
try:
print(f"{f.__name__} = {f}, {f.__code__.co_varnames = }")
f("bar")
f("baz")
except Exception:
print(f"Function {f.__name__} is broken.")
2023-04-03 16:50:34 +00:00
2023-04-03 22:23:23 +00:00
if __name__ == "__main__":
func_info(func1)
func_info(func2)
2023-04-04 19:20:21 +00:00
func_info(func3)