Added small proof of concept
This commit is contained in:
parent
5f478cb15c
commit
3f656c1c7a
|
@ -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)
|
|
@ -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
|
Loading…
Reference in New Issue