1
0
Fork 0

namespace injection experiments

This commit is contained in:
Jörn-Michael Miehe 2023-04-03 18:50:34 +02:00
parent b997ee61ef
commit 5ffd522209
3 changed files with 75 additions and 0 deletions

52
namespace-inject.py Normal file
View file

@ -0,0 +1,52 @@
# 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()

7
poetry.lock generated Normal file
View file

@ -0,0 +1,7 @@
# This file is automatically @generated by Poetry 1.4.2 and should not be changed by hand.
package = []
[metadata]
lock-version = "2.0"
python-versions = "^3.10"
content-hash = "53f2eabc9c26446fbcc00d348c47878e118afc2054778c3c803a0a8028af27d9"

16
pyproject.toml Normal file
View file

@ -0,0 +1,16 @@
[tool.poetry]
name = "cursed-snakes"
version = "0.1.0"
description = ""
authors = ["Jörn-Michael Miehe <jmm@yavook.de>"]
license = "MIT"
readme = "README.md"
packages = [{include = "cursed_snakes"}]
[tool.poetry.dependencies]
python = "^3.10"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"