generated from Yavook.de/vscode-python3
53 lines
827 B
Python
53 lines
827 B
Python
|
# import new
|
||
|
|
||
|
|
||
|
from types import FunctionType
|
||
|
|
||
|
|
||
|
def my_func(pstr: str) -> None:
|
||
|
|
||
|
def foo() -> None:
|
||
|
print(f"{pstr = }")
|
||
|
|
||
|
foo()
|
||
|
|
||
|
|
||
|
def my_func2(pstr: str) -> None:
|
||
|
foo()
|
||
|
|
||
|
|
||
|
def func_test(f: FunctionType) -> None:
|
||
|
print(f"{f.__name__ } = {f}, {f.__code__.co_name = }")
|
||
|
f("narf")
|
||
|
f("zort")
|
||
|
|
||
|
|
||
|
def main() -> None:
|
||
|
func_test(my_func)
|
||
|
|
||
|
fcode = my_func.__code__.replace(
|
||
|
co_name="foo",
|
||
|
co_qualname="foo",
|
||
|
co_nlocals=3,
|
||
|
co_varnames=("pstr", "foo", "lol")
|
||
|
)
|
||
|
|
||
|
f = FunctionType(
|
||
|
name="foo",
|
||
|
code=fcode,
|
||
|
globals=my_func.__globals__,
|
||
|
)
|
||
|
|
||
|
func_test(f)
|
||
|
|
||
|
import json
|
||
|
|
||
|
print(json.dumps({
|
||
|
name: repr(getattr(f.__code__, name))
|
||
|
for name in dir(f.__code__)
|
||
|
}, indent=2))
|
||
|
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
main()
|