forked from zrax/pycdc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyc_sequence.py
More file actions
116 lines (96 loc) · 3.75 KB
/
pyc_sequence.py
File metadata and controls
116 lines (96 loc) · 3.75 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import sys
from typing import List, Tuple, Optional, Any, TYPE_CHECKING
from pyc_object import PycObject, LoadObject, PycObjectType
if TYPE_CHECKING:
from pyc_module import PycModule
from data import PycData
class PycSequence(PycObject):
def __init__(self, type_id: int):
super().__init__(type_id)
self.m_size = 0
@property
def size(self) -> int:
return self.m_size
def get(self, idx: int) -> 'PycObject':
raise NotImplementedError
class PycSimpleSequence(PycSequence):
def __init__(self, type_id: int):
super().__init__(type_id)
self.m_values: List['PycObject'] = []
def load(self, stream: 'PycData', mod: 'PycModule'):
# [cite: 657-658]
self.m_size = stream.get32()
self.m_values = []
for _ in range(self.m_size):
obj = LoadObject(stream, mod)
if obj is not None:
self.m_values.append(obj)
def is_equal(self, obj: 'PycObject') -> bool:
# [cite: 659-661]
if self.type != obj.type:
return False
if not isinstance(obj, PycSimpleSequence):
return False
if self.m_size != obj.m_size:
return False
for v1, v2 in zip(self.m_values, obj.m_values):
if not v1.is_equal(v2):
return False
return True
@property
def values(self) -> List['PycObject']:
return self.m_values
def get(self, idx: int) -> 'PycObject':
if 0 <= idx < len(self.m_values):
return self.m_values[idx]
raise IndexError(f"Sequence index out of range: {idx}")
class PycTuple(PycSimpleSequence):
def __init__(self, type_id: int = PycObjectType.TYPE_TUPLE):
super().__init__(type_id)
def load(self, stream: 'PycData', mod: 'PycModule'):
# [cite: 662-664]
if self.type == PycObjectType.TYPE_SMALL_TUPLE:
self.m_size = stream.get_byte()
else:
self.m_size = stream.get32()
self.m_values = []
for _ in range(self.m_size):
obj = LoadObject(stream, mod)
if obj is not None:
self.m_values.append(obj)
class PycList(PycSimpleSequence):
def __init__(self, type_id: int = PycObjectType.TYPE_LIST):
super().__init__(type_id)
class PycSet(PycSimpleSequence):
def __init__(self, type_id: int = PycObjectType.TYPE_SET):
super().__init__(type_id)
class PycDict(PycObject):
def __init__(self, type_id: int = PycObjectType.TYPE_DICT):
super().__init__(type_id)
self.m_values: List[Tuple['PycObject', 'PycObject']] = []
def load(self, stream: 'PycData', mod: 'PycModule'):
# [cite: 665-667]
while True:
key = LoadObject(stream, mod)
if key is None or key.type == PycObjectType.TYPE_NULL: # NULL check depends on LoadObject impl
break
val = LoadObject(stream, mod)
self.m_values.append((key, val))
def is_equal(self, obj: 'PycObject') -> bool:
# [cite: 668-671]
if self.type != obj.type:
return False
if not isinstance(obj, PycDict):
return False
if len(self.m_values) != len(obj.m_values):
return False
# Order matters in marshalled dicts usually? The C++ impl compares sequentially.
for (k1, v1), (k2, v2) in zip(self.m_values, obj.m_values):
if not k1.is_equal(k2):
return False
if not v1.is_equal(v2):
return False
return True
@property
def values(self) -> List[Tuple['PycObject', 'PycObject']]:
return self.m_values