-
Notifications
You must be signed in to change notification settings - Fork 0
/
structs.py
50 lines (37 loc) · 1.16 KB
/
structs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from ast import Str
from dataclasses import dataclass, field
from types import CellType, CodeType, FunctionType
from typing import Mapping, Any, Tuple
from .helpers import make_cell
@dataclass(slots=True)
class Frame:
"""Realistic frame class."""
code: CodeType
local_env: Mapping[Str, Any]
# global_env: Mapping[Str, Any] = field(default={})
@dataclass(slots=True)
class Block:
"""Realistic code block."""
type: Str
handler: Any
stack_height: int
@dataclass
class Function:
"""Realistic function class."""
name: Str
code: CodeType
argdefs: Tuple[Any] = field(default=())
closure: Tuple[CellType] = field(default=())
def __post_init__(self) -> None:
if self.closure:
self.closure = tuple(map(make_cell, self.closure))
# self.__dict__: Mapping[Str, Any] = {}
self.__doc__: Str = self.code.co_consts[0] if self.code.co_consts else None
def __call__(self, *args: Any, **kwds: Any) -> Any:
return FunctionType(
self.code,
globals(),
name=self.name,
argdefs=self.argdefs,
closure=self.closure,
)(*args, **kwds)