generated from Yavook.de/vscode-python3
namespace-inject cleanup
This commit is contained in:
parent
5ffd522209
commit
b43613b457
1 changed files with 26 additions and 33 deletions
|
@ -1,52 +1,45 @@
|
|||
# import new
|
||||
|
||||
|
||||
from types import FunctionType
|
||||
from typing import Callable
|
||||
|
||||
|
||||
def my_func(pstr: str) -> None:
|
||||
|
||||
def func1(pstr: str) -> None:
|
||||
def foo() -> None:
|
||||
print(f"{pstr = }")
|
||||
|
||||
foo()
|
||||
|
||||
|
||||
def my_func2(pstr: str) -> None:
|
||||
def shoehorn(f: Callable) -> Callable:
|
||||
code = f.__code__.replace(
|
||||
co_nlocals=2,
|
||||
co_varnames=("pstr", "foo"),
|
||||
)
|
||||
|
||||
return FunctionType(
|
||||
name=f.__name__,
|
||||
code=code,
|
||||
globals=func1.__globals__,
|
||||
)
|
||||
|
||||
|
||||
@shoehorn
|
||||
def 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 func_info(f: Callable) -> None:
|
||||
print(f"{f.__name__ } = {f}, {f.__code__.co_varnames = }")
|
||||
f("bar")
|
||||
f("baz")
|
||||
|
||||
|
||||
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)
|
||||
if __name__ == "__main__":
|
||||
func_info(func1)
|
||||
func_info(func2)
|
||||
|
||||
import json
|
||||
|
||||
print(json.dumps({
|
||||
name: repr(getattr(f.__code__, name))
|
||||
for name in dir(f.__code__)
|
||||
name: repr(getattr(func2.__code__, name))
|
||||
for name in dir(func2.__code__)
|
||||
}, indent=2))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
|
Loading…
Reference in a new issue