forked from APrioriInvestments/typed_python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPyAlternativeInstance.cpp
More file actions
320 lines (251 loc) · 10.8 KB
/
Copy pathPyAlternativeInstance.cpp
File metadata and controls
320 lines (251 loc) · 10.8 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include "PyAlternativeInstance.hpp"
#include "PyFunctionInstance.hpp"
Alternative* PyAlternativeInstance::type() {
return (Alternative*)extractTypeFrom(((PyObject*)this)->ob_type);
}
ConcreteAlternative* PyConcreteAlternativeInstance::type() {
return (ConcreteAlternative*)extractTypeFrom(((PyObject*)this)->ob_type);
}
PyObject* PyAlternativeInstance::pyTernaryOperatorConcrete(PyObject* rhs, PyObject* thirdArg, const char* op, const char* opErr) {
auto it = type()->getMethods().find(op);
if (it != type()->getMethods().end()) {
Function* f = it->second;
PyObjectStealer argTuple(
PyTuple_Pack(3, (PyObject*)this, (PyObject*)rhs, (PyObject*)thirdArg)
);
for (const auto& overload: f->getOverloads()) {
std::pair<bool, PyObject*> res = PyFunctionInstance::tryToCallOverload(overload, nullptr, argTuple, nullptr);
if (res.first) {
return res.second;
}
}
}
return ((PyInstance*)this)->pyTernaryOperatorConcrete(rhs, thirdArg, op, opErr);
}
PyObject* PyConcreteAlternativeInstance::pyTernaryOperatorConcrete(PyObject* rhs, PyObject* thirdArg, const char* op, const char* opErr) {
auto it = type()->getAlternative()->getMethods().find(op);
if (it != type()->getAlternative()->getMethods().end()) {
Function* f = it->second;
PyObjectStealer argTuple(
PyTuple_Pack(3, (PyObject*)this, (PyObject*)rhs, (PyObject*)thirdArg)
);
for (const auto& overload: f->getOverloads()) {
std::pair<bool, PyObject*> res = PyFunctionInstance::tryToCallOverload(overload, nullptr, argTuple, nullptr);
if (res.first) {
return res.second;
}
}
}
return ((PyInstance*)this)->pyTernaryOperatorConcrete(rhs, thirdArg, op, opErr);
}
PyObject* PyAlternativeInstance::pyOperatorConcrete(PyObject* rhs, const char* op, const char* opErr) {
auto it = type()->getMethods().find(op);
if (it != type()->getMethods().end()) {
Function* f = it->second;
PyObjectStealer argTuple(
PyTuple_Pack(2, (PyObject*)this, (PyObject*)rhs)
);
for (const auto& overload: f->getOverloads()) {
std::pair<bool, PyObject*> res = PyFunctionInstance::tryToCallOverload(overload, nullptr, argTuple, nullptr);
if (res.first) {
return res.second;
}
}
}
return ((PyInstance*)this)->pyOperatorConcrete(rhs, op, opErr);
}
PyObject* PyConcreteAlternativeInstance::pyOperatorConcrete(PyObject* rhs, const char* op, const char* opErr) {
auto it = type()->getAlternative()->getMethods().find(op);
if (it != type()->getAlternative()->getMethods().end()) {
Function* f = it->second;
PyObjectStealer argTuple(
PyTuple_Pack(2, (PyObject*)this, (PyObject*)rhs)
);
for (const auto& overload: f->getOverloads()) {
std::pair<bool, PyObject*> res = PyFunctionInstance::tryToCallOverload(overload, nullptr, argTuple, nullptr);
if (res.first) {
return res.second;
}
}
}
return ((PyInstance*)this)->pyOperatorConcrete(rhs, op, opErr);
}
PyObject* PyAlternativeInstance::pyUnaryOperatorConcrete(const char* op, const char* opErr) {
auto it = type()->getMethods().find(op);
if (it != type()->getMethods().end()) {
Function* f = it->second;
PyObjectStealer argTuple(
PyTuple_Pack(2, (PyObject*)this)
);
for (const auto& overload: f->getOverloads()) {
std::pair<bool, PyObject*> res = PyFunctionInstance::tryToCallOverload(overload, nullptr, argTuple, nullptr);
if (res.first) {
return res.second;
}
}
}
return ((PyInstance*)this)->pyUnaryOperatorConcrete(op, opErr);
}
PyObject* PyConcreteAlternativeInstance::pyUnaryOperatorConcrete(const char* op, const char* opErr) {
auto it = type()->getAlternative()->getMethods().find(op);
if (it != type()->getAlternative()->getMethods().end()) {
Function* f = it->second;
PyObjectStealer argTuple(
PyTuple_Pack(2, (PyObject*)this)
);
for (const auto& overload: f->getOverloads()) {
std::pair<bool, PyObject*> res = PyFunctionInstance::tryToCallOverload(overload, nullptr, argTuple, nullptr);
if (res.first) {
return res.second;
}
}
}
return ((PyInstance*)this)->pyUnaryOperatorConcrete(op, opErr);
}
void PyConcreteAlternativeInstance::constructFromPythonArgumentsConcrete(ConcreteAlternative* alt, uint8_t* data, PyObject* args, PyObject* kwargs) {
alt->constructor(data, [&](instance_ptr p) {
if ((kwargs == nullptr || PyDict_Size(kwargs) == 0) && PyTuple_Size(args) == 1) {
//construct an alternative from a single argument.
//if it's a binary compatible subtype of the alternative we're constructing, then
//invoke the copy constructor.
PyObject* arg = PyTuple_GetItem(args, 0);
Type* argType = extractTypeFrom(arg->ob_type);
if (argType && argType->isBinaryCompatibleWith(alt)) {
//it's already the right kind of instance, so we can copy-through the underlying element
alt->elementType()->copy_constructor(p, alt->eltPtr(((PyInstance*)arg)->dataPtr()));
return;
}
//otherwise, if we have exactly one subelement, attempt to construct from that
if (alt->elementType()->getTypeCategory() != Type::TypeCategory::catNamedTuple) {
throw std::runtime_error("ConcreteAlternatives are supposed to only contain NamedTuples");
}
NamedTuple* alternativeEltType = (NamedTuple*)alt->elementType();
if (alternativeEltType->getTypes().size() != 1) {
throw std::logic_error("Can't initialize " + alt->name() + " with positional arguments because it doesn't have only one field.");
}
PyInstance::copyConstructFromPythonInstance(alternativeEltType->getTypes()[0], p, arg);
} else if (PyTuple_Size(args) == 0) {
//construct an alternative from Kwargs
constructFromPythonArguments(p, alt->elementType(), args, kwargs);
} else {
throw std::logic_error("Can only initialize " + alt->name() + " from python with kwargs or a single in-place argument");
}
});
}
PyObject* PyAlternativeInstance::tp_getattr_concrete(PyObject* pyAttrName, const char* attrName) {
if (mIsMatcher) {
if (type()->subtypes()[type()->which(dataPtr())].first == attrName) {
return incref(Py_True);
}
return incref(Py_False);
}
if (strcmp(attrName,"matches") == 0) {
PyInstance* self = duplicate();
self->mIteratorOffset = -1;
self->mIsMatcher = true;
return (PyObject*)self;
}
//see if its a method
Alternative* toCheck = type();
auto it = toCheck->getMethods().find(attrName);
if (it != toCheck->getMethods().end()) {
return PyMethod_New((PyObject*)it->second->getOverloads()[0].getFunctionObj(), (PyObject*)this);
}
//see if its a member of our held type
NamedTuple* heldT = (NamedTuple*)type()->subtypes()[type()->which(dataPtr())].second;
instance_ptr heldData = type()->eltPtr(dataPtr());
int ix = heldT->indexOfName(attrName);
if (ix >= 0) {
return extractPythonObject(
heldT->eltPtr(heldData, ix),
heldT->getTypes()[ix]
);
}
return PyInstance::tp_getattr_concrete(pyAttrName, attrName);
}
PyObject* PyConcreteAlternativeInstance::tp_getattr_concrete(PyObject* pyAttrName, const char* attrName) {
if (mIsMatcher) {
if (type()->getAlternative()->subtypes()[type()->getAlternative()->which(dataPtr())].first == attrName) {
return incref(Py_True);
}
return incref(Py_False);
}
if (strcmp(attrName,"matches") == 0) {
PyInstance* self = duplicate();
self->mIteratorOffset = -1;
self->mIsMatcher = true;
return (PyObject*)self;
}
//see if its a method
Alternative* toCheck = type()->getAlternative();
auto it = toCheck->getMethods().find(attrName);
if (it != toCheck->getMethods().end()) {
return PyMethod_New((PyObject*)it->second->getOverloads()[0].getFunctionObj(), (PyObject*)this);
}
//see if its a member of our held type
NamedTuple* heldT = (NamedTuple*)type()->getAlternative()->subtypes()[type()->which()].second;
instance_ptr heldData = type()->getAlternative()->eltPtr(dataPtr());
int ix = heldT->indexOfName(attrName);
if (ix >= 0) {
return extractPythonObject(
heldT->eltPtr(heldData, ix),
heldT->getTypes()[ix]
);
}
return PyInstance::tp_getattr_concrete(pyAttrName, attrName);
}
void PyAlternativeInstance::mirrorTypeInformationIntoPyTypeConcrete(Alternative* alt, PyTypeObject* pyType) {
PyObjectStealer alternatives(PyTuple_New(alt->subtypes().size()));
for (long k = 0; k < alt->subtypes().size(); k++) {
ConcreteAlternative* concrete = ConcreteAlternative::Make(alt, k);
PyDict_SetItemString(
pyType->tp_dict,
alt->subtypes()[k].first.c_str(),
(PyObject*)typeObjInternal(concrete)
);
PyTuple_SetItem(alternatives, k, incref((PyObject*)typeObjInternal(concrete)));
}
PyDict_SetItemString(
pyType->tp_dict,
"__typed_python_alternatives__",
alternatives
);
}
void PyConcreteAlternativeInstance::mirrorTypeInformationIntoPyTypeConcrete(ConcreteAlternative* alt, PyTypeObject* pyType) {
PyDict_SetItemString(
pyType->tp_dict,
"Alternative",
(PyObject*)typeObjInternal(alt->getAlternative())
);
PyDict_SetItemString(
pyType->tp_dict,
"Index",
PyLong_FromLong(alt->which())
);
PyDict_SetItemString(
pyType->tp_dict,
"Name",
PyUnicode_FromString(alt->getAlternative()->subtypes()[alt->which()].first.c_str())
);
PyDict_SetItemString(
pyType->tp_dict,
"ElementType",
(PyObject*)typeObjInternal(alt->elementType())
);
}
int PyAlternativeInstance::tp_setattr_concrete(PyObject* attrName, PyObject* attrVal) {
PyErr_Format(
PyExc_AttributeError,
"Cannot set attributes on instance of type '%s' because it is immutable",
type()->name().c_str()
);
return -1;
}
int PyConcreteAlternativeInstance::tp_setattr_concrete(PyObject* attrName, PyObject* attrVal) {
PyErr_Format(
PyExc_AttributeError,
"Cannot set attributes on instance of type '%s' because it is immutable",
type()->name().c_str()
);
return -1;
}