Added small proof of concept

This commit is contained in:
GodSaveTheDoge 2021-05-11 21:01:56 +02:00
parent 5f478cb15c
commit 3f656c1c7a
2 changed files with 38 additions and 0 deletions

30
ProofOfConcept/const.py Normal file
View File

@ -0,0 +1,30 @@
import sys
from dataclasses import dataclass
from typing import Any
from IPython import embed
@dataclass
class ConstVar:
frame: Any # too lazy to type hint correctly. Doesn't matter
name: str
value: Any
ConstList= []
def checkConsts(*a):
for cvar in ConstList:
if cvar.frame.f_locals[cvar.name] != cvar.value:
cvar.frame.f_locals[cvar.name] = cvar.value
raise SyntaxError('Cannot assign value to constant')
class ConstSetter:
def __setattr__(self, key, value):
target_frame = sys._getframe(1)
target_frame.f_locals[key] = value
target_frame.f_trace = checkConsts
target_frame.f_trace_opcodes = True
target_frame.f_trace_lines = False
ConstList.append(ConstVar(target_frame, key, value))
const = ConstSetter()
sys.settrace(lambda *a: None)

8
ProofOfConcept/runme.py Normal file
View File

@ -0,0 +1,8 @@
from const import const
# Create a new constant
const .a = 5
print(a)
# Let's try to assign something to it
a = 6