PyConstants/pyconstants.py

39 lines
1.2 KiB
Python
Raw Permalink Normal View History

import sys
from collections import defaultdict
from dataclasses import dataclass
2021-05-11 22:15:36 +02:00
from types import FrameType
2021-05-11 22:43:56 +02:00
from typing import Callable, Dict, Final, List
@dataclass
class ConstantValue:
2021-05-11 22:15:36 +02:00
frame: FrameType
name: str
value: object
2021-05-11 22:43:56 +02:00
2021-05-11 22:23:48 +02:00
def _traceFun(frame: FrameType, event: str, arg: object) -> None:
for constant in constMap[frame]:
2021-05-11 22:43:56 +02:00
if (
constant.name in frame.f_locals
and frame.f_locals[constant.name] != constant.value
):
frame.f_locals[constant.name] = constant.value
raise SyntaxError(f"Cannot assign value to constant {repr(constant.name)}")
2021-05-11 22:43:56 +02:00
class _ConstClass:
def __setattr__(self, name, value):
"""This is the method called when a new constant is created"""
targetFrame = sys._getframe(1)
targetFrame.f_trace_lines = False
targetFrame.f_trace_opcodes = True
2021-05-11 22:43:56 +02:00
targetFrame.f_trace = _traceFun
targetFrame.f_locals[name] = value
constMap[targetFrame].append(ConstantValue(targetFrame, name, value))
2021-05-11 22:43:56 +02:00
constMap: Dict[FrameType, List[ConstantValue]] = defaultdict(list) # FIXME
sys.settrace(lambda frame, event, arg: None)
const = _ConstClass()