forked from APrioriInvestments/typed_python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClassType.cpp
More file actions
238 lines (187 loc) · 6.61 KB
/
Copy pathClassType.cpp
File metadata and controls
238 lines (187 loc) · 6.61 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include "AllTypes.hpp"
bool Class::isBinaryCompatibleWithConcrete(Type* other) {
if (other->getTypeCategory() != m_typeCategory) {
return false;
}
Class* otherO = (Class*)other;
return m_heldClass->isBinaryCompatibleWith(otherO->m_heldClass);
}
void Class::_forwardTypesMayHaveChanged() {
m_is_default_constructible = m_heldClass->is_default_constructible();
m_name = m_heldClass->name();
}
instance_ptr Class::eltPtr(instance_ptr self, int64_t ix) const {
layout& l = **(layout**)self;
return m_heldClass->eltPtr(l.data, ix);
}
void Class::setAttribute(instance_ptr self, int64_t ix, instance_ptr elt) const {
layout& l = **(layout**)self;
m_heldClass->setAttribute(l.data, ix, elt);
}
bool Class::checkInitializationFlag(instance_ptr self, int64_t ix) const {
layout& l = **(layout**)self;
return m_heldClass->checkInitializationFlag(l.data, ix);
}
bool Class::cmp(instance_ptr left, instance_ptr right, int pyComparisonOp) {
const char* method = nullptr;
switch (pyComparisonOp) {
case Py_EQ:
method = "__eq__";
break;
case Py_NE:
method = "__ne__";
break;
case Py_LT:
method = "__lt__";
break;
case Py_GT:
method = "__gt__";
break;
case Py_LE:
method = "__le__";
break;
case Py_GE:
method = "__ge__";
break;
}
auto it = m_heldClass->getMemberFunctions().find(method);
if (it != m_heldClass->getMemberFunctions().end()) {
//we found a user-defined method for this comparison function.
PyObjectStealer leftAsPyObj(PyInstance::extractPythonObject(left, this));
PyObjectStealer rightAsPyObj(PyInstance::extractPythonObject(right, this));
std::pair<bool, PyObject*> res = PyFunctionInstance::tryToCall(
it->second,
leftAsPyObj,
rightAsPyObj
);
if (res.first && !res.second) {
throw PythonExceptionSet();
}
bool result = res.second == Py_True;
decref(res.second);
return result;
}
if (pyComparisonOp == Py_NE) {
return !cmp(left, right, Py_EQ);
}
if (pyComparisonOp == Py_EQ) {
//if these operators are not implemented, we defer to the class pointer
uint64_t leftPtr = *(uint64_t*)left;
uint64_t rightPtr = *(uint64_t*)right;
return leftPtr == rightPtr;
}
PyErr_Format(
PyExc_TypeError,
"'%s' not defined between instances of '%s' and '%s'",
pyComparisonOp == Py_EQ ? "==" :
pyComparisonOp == Py_NE ? "!=" :
pyComparisonOp == Py_LT ? "<" :
pyComparisonOp == Py_LE ? "<=" :
pyComparisonOp == Py_GT ? ">" :
pyComparisonOp == Py_GE ? ">=" : "?",
name().c_str(),
name().c_str()
);
throw PythonExceptionSet();
}
void Class::repr(instance_ptr self, ReprAccumulator& stream) {
auto it = m_heldClass->getMemberFunctions().find(stream.isStrCall() ? "__str__" : "__repr__");
if (it != m_heldClass->getMemberFunctions().end()) {
PyEnsureGilAcquired acquireTheGil;
PyObjectStealer selfAsPyObj(PyInstance::extractPythonObject(self, this));
std::pair<bool, PyObject*> res = PyFunctionInstance::tryToCall(
it->second,
selfAsPyObj
);
if (res.first) {
if (!res.second) {
throw PythonExceptionSet();
}
if (!PyUnicode_Check(res.second)) {
decref(res.second);
throw std::runtime_error(
stream.isStrCall() ? "__str__ returned a non-string" : "__repr__ returned a non-string"
);
}
stream << PyUnicode_AsUTF8(res.second);
decref(res.second);
return;
}
throw std::runtime_error(
stream.isStrCall() ? "Found a __str__ method but failed to call it with 'self'"
: "Found a __repr__ method but failed to call it with 'self'"
);
}
layout& l = **(layout**)self;
m_heldClass->repr(l.data, stream);
}
int32_t Class::hash32(instance_ptr left) {
auto it = m_heldClass->getMemberFunctions().find("__hash__");
if (it != m_heldClass->getMemberFunctions().end()) {
PyEnsureGilAcquired acquireTheGil;
PyObjectStealer leftAsPyObj(PyInstance::extractPythonObject(left, this));
std::pair<bool, PyObject*> res = PyFunctionInstance::tryToCall(
it->second,
leftAsPyObj
);
if (res.first) {
if (!res.second) {
throw PythonExceptionSet();
}
if (!PyLong_Check(res.second)) {
decref(res.second);
throw std::runtime_error("__hash__ returned a non-int");
}
int32_t retval = PyLong_AsLong(res.second);
decref(res.second);
if (retval == -1) {
retval = -2;
}
return retval;
}
throw std::runtime_error("Found a __hash__ method but failed to call it with 'self'");
}
layout& l = **(layout**)left;
return m_heldClass->hash32(l.data);
}
void Class::emptyConstructor(instance_ptr self) {
if (!m_is_default_constructible) {
throw std::runtime_error(m_name + " is not default-constructible");
}
*(layout**)self = (layout*)malloc(sizeof(layout) + m_heldClass->bytecount());
layout& l = **(layout**)self;
l.refcount = 1;
m_heldClass->emptyConstructor(l.data);
}
void Class::constructor(instance_ptr self) {
if (!m_is_default_constructible) {
throw std::runtime_error(m_name + " is not default-constructible");
}
*(layout**)self = (layout*)malloc(sizeof(layout) + m_heldClass->bytecount());
layout& l = **(layout**)self;
l.refcount = 1;
m_heldClass->constructor(l.data);
}
int64_t Class::refcount(instance_ptr self) {
layout& l = **(layout**)self;
return l.refcount;
}
void Class::destroy(instance_ptr self) {
layout& l = **(layout**)self;
if (l.refcount.fetch_sub(1) == 1) {
m_heldClass->destroy(l.data);
free(*(layout**)self);
}
}
void Class::copy_constructor(instance_ptr self, instance_ptr other) {
(*(layout**)self) = (*(layout**)other);
(*(layout**)self)->refcount++;
}
void Class::assign(instance_ptr self, instance_ptr other) {
layout* old = (*(layout**)self);
(*(layout**)self) = (*(layout**)other);
if (*(layout**)self) {
(*(layout**)self)->refcount++;
}
destroy((instance_ptr)&old);
}