forked from APrioriInvestments/typed_python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPyCompositeTypeInstance.cpp
More file actions
146 lines (116 loc) · 5.38 KB
/
Copy pathPyCompositeTypeInstance.cpp
File metadata and controls
146 lines (116 loc) · 5.38 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
#include "PyCompositeTypeInstance.hpp"
CompositeType* PyCompositeTypeInstance::type() {
return (CompositeType*)extractTypeFrom(((PyObject*)this)->ob_type);
}
Tuple* PyTupleInstance::type() {
return (Tuple*)extractTypeFrom(((PyObject*)this)->ob_type);
}
NamedTuple* PyNamedTupleInstance::type() {
return (NamedTuple*)extractTypeFrom(((PyObject*)this)->ob_type);
}
PyObject* PyCompositeTypeInstance::sq_item_concrete(Py_ssize_t ix) {
if (ix < 0 || ix >= (int64_t)type()->getTypes().size()) {
PyErr_SetString(PyExc_IndexError, "index out of range");
return NULL;
}
Type* eltType = type()->getTypes()[ix];
return extractPythonObject(type()->eltPtr(dataPtr(), ix), eltType);
}
Py_ssize_t PyCompositeTypeInstance::mp_and_sq_length_concrete() {
return type()->getTypes().size();
}
void PyNamedTupleInstance::copyConstructFromPythonInstanceConcrete(NamedTuple* namedTupleT, instance_ptr tgt, PyObject* pyRepresentation, bool isExplicit) {
if (PyDict_Check(pyRepresentation)) {
if (namedTupleT->getTypes().size() < PyDict_Size(pyRepresentation)) {
throw std::runtime_error("Couldn't initialize type of " + namedTupleT->name() + " because supplied dictionary had too many items");
}
long actuallyUsed = 0;
namedTupleT->constructor(tgt,
[&](uint8_t* eltPtr, int64_t k) {
const std::string& name = namedTupleT->getNames()[k];
Type* t = namedTupleT->getTypes()[k];
PyObject* o = PyDict_GetItemString(pyRepresentation, name.c_str());
if (o) {
copyConstructFromPythonInstance(t, eltPtr, o);
actuallyUsed++;
}
else if (namedTupleT->is_default_constructible()) {
t->constructor(eltPtr);
} else {
throw std::logic_error("Can't default initialize argument " + name);
}
});
if (actuallyUsed != PyDict_Size(pyRepresentation)) {
throw std::runtime_error("Couldn't initialize type of " + namedTupleT->name() + " because supplied dictionary had unused arguments");
}
return;
}
PyInstance::copyConstructFromPythonInstanceConcrete(namedTupleT, tgt, pyRepresentation, isExplicit);
}
void PyNamedTupleInstance::constructFromPythonArgumentsConcrete(NamedTuple* namedTupleT, uint8_t* data, PyObject* args, PyObject* kwargs) {
long actuallyUsed = 0;
if (kwargs) {
namedTupleT->constructor(
data,
[&](uint8_t* eltPtr, int64_t k) {
Type* eltType = namedTupleT->getTypes()[k];
PyObject* o = PyDict_GetItemString(kwargs, namedTupleT->getNames()[k].c_str());
if (o) {
copyConstructFromPythonInstance(eltType, eltPtr, o);
actuallyUsed++;
}
else if (eltType->is_default_constructible()) {
eltType->constructor(eltPtr);
} else {
throw std::logic_error("Can't default initialize argument " + namedTupleT->getNames()[k]);
}
});
if (actuallyUsed != PyDict_Size(kwargs)) {
namedTupleT->destroy(data);
throw std::runtime_error("Couldn't initialize type of " + namedTupleT->name() + " because supplied dictionary had unused arguments");
}
return;
}
return PyInstance::constructFromPythonArgumentsConcrete(namedTupleT, data, args, kwargs);
}
PyObject* PyNamedTupleInstance::tp_getattr_concrete(PyObject* pyAttrName, const char* attrName) {
//see if its a member of our held type
int ix = type()->indexOfName(attrName);
if (ix >= 0) {
return extractPythonObject(
type()->eltPtr(dataPtr(), ix),
type()->getTypes()[ix]
);
}
return PyInstance::tp_getattr_concrete(pyAttrName, attrName);
}
void PyTupleInstance::mirrorTypeInformationIntoPyTypeConcrete(Tuple* tupleT, PyTypeObject* pyType) {
PyObject* res = PyTuple_New(tupleT->getTypes().size());
for (long k = 0; k < tupleT->getTypes().size(); k++) {
PyTuple_SetItem(res, k, incref(typePtrToPyTypeRepresentation(tupleT->getTypes()[k])));
}
//expose 'ElementType' as a member of the type object
PyDict_SetItemString(pyType->tp_dict, "ElementTypes", res);
}
void PyNamedTupleInstance::mirrorTypeInformationIntoPyTypeConcrete(NamedTuple* tupleT, PyTypeObject* pyType) {
PyObjectStealer types(PyTuple_New(tupleT->getTypes().size()));
for (long k = 0; k < tupleT->getTypes().size(); k++) {
PyTuple_SetItem(types, k, incref(typePtrToPyTypeRepresentation(tupleT->getTypes()[k])));
}
PyObjectStealer names(PyTuple_New(tupleT->getNames().size()));
for (long k = 0; k < tupleT->getNames().size(); k++) {
PyObject* namePtr = PyUnicode_FromString(tupleT->getNames()[k].c_str());
PyTuple_SetItem(names, k, namePtr);
}
//expose 'ElementType' as a member of the type object
PyDict_SetItemString(pyType->tp_dict, "ElementTypes", types);
PyDict_SetItemString(pyType->tp_dict, "ElementNames", names);
}
int PyNamedTupleInstance::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;
}