-
Notifications
You must be signed in to change notification settings - Fork 4
/
pointless_ext.c
167 lines (138 loc) · 5.43 KB
/
pointless_ext.c
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
#include "pointless_ext.h"
uint32_t PyPointlessBitvector_is_set(PyPointlessBitvector* self, uint32_t i);
uint32_t PyPointlessBitvector_n_items(PyPointlessBitvector* self);
struct pointless_module_state {
// PyObject *error;
};
#define GETSTATE(m) ((struct pointless_module_state*)PyModule_GetState(m))
static struct PyPointless_CAPI CAPI = {
POINTLESS_API_MAGIC,
sizeof(struct PyPointless_CAPI),
pointless_dynarray_init,
pointless_dynarray_n_items,
pointless_dynarray_pop,
pointless_dynarray_push,
pointless_dynarray_push_bulk,
pointless_dynarray_clear,
pointless_dynarray_destroy,
pointless_dynarray_item_at,
pointless_calloc,
pointless_malloc,
pointless_free,
pointless_realloc,
pointless_strdup,
PyPointlessPrimVector_from_T_vector,
PyPointlessPrimVector_from_buffer,
bentley_sort_,
&PyPointlessType,
&PyPointlessVectorType,
&PyPointlessBitvectorType,
&PyPointlessSetType,
&PyPointlessMapType,
&PyPointlessPrimVectorType,
PyPointlessBitvector_is_set,
PyPointlessBitvector_n_items,
pypointless_value
};
PyObject* pointless_write_object(PyObject* self, PyObject* args, PyObject* kwds);
extern const char pointless_write_object_doc[];
PyObject* pointless_write_object_to_primvector(PyObject* self, PyObject* args, PyObject* kwds);
PyObject* pointless_write_object_to_bytearray(PyObject* self, PyObject* args, PyObject* kwds);
extern const char pointless_write_object_to_buffer_doc[];
PyObject* pointless_pyobject_hash_32(PyObject* self, PyObject* args);
extern const char pointless_pyobject_hash_32_doc[];
PyObject* pointless_cmp(PyObject* self, PyObject* args);
extern const char pointless_cmp_doc[];
PyObject* pointless_is_eq(PyObject* self, PyObject* args);
extern const char pointless_is_eq_doc[];
static PyMethodDef pointless_module_methods[] =
{
{"serialize", (PyCFunction)pointless_write_object, METH_VARARGS | METH_KEYWORDS, pointless_write_object_doc },
{"serialize_to_buffer", (PyCFunction)pointless_write_object_to_primvector, METH_VARARGS | METH_KEYWORDS, pointless_write_object_to_buffer_doc },
{"serialize_to_bytearray", (PyCFunction)pointless_write_object_to_bytearray, METH_VARARGS | METH_KEYWORDS, pointless_write_object_to_buffer_doc },
{"pyobject_hash", (PyCFunction)pointless_pyobject_hash_32, METH_VARARGS, pointless_pyobject_hash_32_doc },
{"pyobject_hash_32", (PyCFunction)pointless_pyobject_hash_32, METH_VARARGS, pointless_pyobject_hash_32_doc },
{"pointless_cmp", (PyCFunction)pointless_cmp, METH_VARARGS, pointless_cmp_doc },
{"pointless_is_eq", (PyCFunction)pointless_is_eq, METH_VARARGS, pointless_is_eq_doc },
{NULL, NULL},
};
static int pointless_module_traverse(PyObject* m, visitproc visit, void* arg) {
// Py_VISIT(GETSTATE(m)->error);
return 0;
}
static int pointless_module_clear(PyObject* m) {
// Py_CLEAR(GETSTATE(m)->error);
return 0;
}
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"pointless",
NULL,
sizeof(struct pointless_module_state),
pointless_module_methods,
NULL,
pointless_module_traverse,
pointless_module_clear,
NULL
};
#define MODULEINITERROR return NULL
PyMODINIT_FUNC
PyInit_pointless(void)
{
if (sizeof(Word_t) != sizeof(void*)) {
PyErr_SetString(PyExc_ValueError, "word size mismatch");
MODULEINITERROR;
}
PyObject* module_pointless = 0;
module_pointless = PyModule_Create(&moduledef);
if (module_pointless == 0) {
MODULEINITERROR;
}
struct {
PyTypeObject* type;
const char* name;
} types[15] = {
{&PyPointlessType, "Pointless" },
{&PyPointlessVectorType, "PointlessVector" },
{&PyPointlessVectorIterType, "PointlessVectorIter" },
{&PyPointlessVectorRevIterType, "PointlessVectorRevIter" },
{&PyPointlessBitvectorType, "PointlessBitvector" },
{&PyPointlessBitvectorIterType, "PointlessBitvectorIter" },
{&PyPointlessSetType, "PointlessSet" },
{&PyPointlessSetIterType, "PointlessSetIter" },
{&PyPointlessMapType, "PointlessMap" },
{&PyPointlessMapKeyIterType, "PointlessMapKeyIter" },
{&PyPointlessMapValueIterType, "PointlessMapValueIter" },
{&PyPointlessMapItemIterType, "PointlessMapItemIter" },
{&PyPointlessPrimVectorType, "PointlessPrimVector" },
{&PyPointlessPrimVectorIterType, "PointlessPrimVectorIter" },
{&PyPointlessPrimVectorRevIterType, "PointlessPrimVectorRevIter" },
};
int i;
for (i = 0; i < 15; i++) {
if (PyType_Ready(types[i].type) < 0) {
Py_DECREF(module_pointless);
MODULEINITERROR;
}
Py_INCREF((PyObject*)types[i].type);
if (PyModule_AddObject(module_pointless, types[i].name, (PyObject*)types[i].type) != 0) {
Py_DECREF(module_pointless);
MODULEINITERROR;
}
}
PyObject* c_api = PyCapsule_New(&CAPI, "pointless_CAPI", 0);
if (c_api == 0) {
Py_DECREF(module_pointless);
MODULEINITERROR;
}
if (PyCapsule_SetContext(c_api, (void*)POINTLESS_MAGIC_CONTEXT) != 0) {
Py_DECREF(module_pointless);
MODULEINITERROR;
}
if (PyModule_AddObject(module_pointless, "pointless_CAPI", c_api) != 0) {
Py_DECREF(module_pointless);
MODULEINITERROR;
}
return module_pointless;
}
#undef MODULEINITERROR