-
Notifications
You must be signed in to change notification settings - Fork 1
/
methodproperty.py
66 lines (62 loc) · 1.87 KB
/
methodproperty.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from opcode import (EXTENDED_ARG, HAVE_ARGUMENT as HAS_ARG,
_inline_cache_entries as _ICE, hasjrel,
opmap, opname, stack_effect)
from sys import _getframe as _f, version_info
if version_info < (3, 13):
get_caches = lambda i: _ICE[i]
else:
get_caches = lambda i: _ICE.get(opname[i], 0)
hasjrel = {*hasjrel}
def has_call(frame=None):
if frame is None:
frame = _f(2)
effect = 0
has_ext = False
oparg = 0
i = frame.f_lasti
instr_skipn = 0
len_code = len(cods := frame.f_code.co_code)
while i < len_code:
if instr_skipn:
instr_skipn -= 1
i += 2
continue
instr = cods[i]
instr_name = opname[instr]
arg = cods[i + 1]
i += 2
if num := get_caches(instr):
i += num * 2
oparg |= arg
if instr is EXTENDED_ARG:
oparg <<= 8
has_ext = True
continue
elif has_ext:
has_ext = False
else:
oparg = arg
effect += stack_effect(instr,
oparg if instr >= HAS_ARG else None,
jump=True)
if effect < 0:
break
if instr in hasjrel:
if "BACKWARD" in instr_name:
raise RuntimeError("what-?")
instr_skipn = oparg - 1
continue
if ("CALL" in instr_name
and "INTRINSIC" not in instr_name
and not effect):
return True
return False
class methodproperty(property):
def callgetter(self, f):
self.fcall = f
def __get__(self, obj, objtype=None):
if obj is None:
return self
if has_call(_f(1)) and hasattr(self, "fcall"):
return lambda *a, **k: self.fcall(obj, *a, **k)
return super().__get__(obj, objtype)