Use PyList_SET_ITEM for list initialization in _isl.c
[linpy.git] / pypol / _isl.c
1 #include <Python.h>
2
3 #include <isl/constraint.h>
4 #include <isl/set.h>
5
6 struct _isl_constraint_list {
7 int cursor;
8 PyObject *constraints;
9 };
10 typedef struct _isl_constraint_list _isl_constraint_list;
11
12 int _isl_isl_basic_set_add_constraint_list(__isl_take isl_constraint *c,
13 void *user) {
14 _isl_constraint_list *list;
15 PyObject *value;
16
17 list = (_isl_constraint_list *) user;
18 value = PyLong_FromVoidPtr(c);
19 if (value == NULL) {
20 return -1;
21 }
22 PyList_SET_ITEM(list->constraints, list->cursor++, value);
23 return 0;
24 }
25
26 static PyObject * _isl_isl_basic_set_constraints(PyObject *self,
27 PyObject* args) {
28 long ptr;
29 isl_basic_set *bset;
30 int n;
31 PyObject *constraints;
32 _isl_constraint_list *list;
33
34 if (!PyArg_ParseTuple(args, "l", &ptr))
35 return NULL;
36 bset = (isl_basic_set*) ptr;
37 n = isl_basic_set_n_constraint(bset);
38 if (n == -1) {
39 PyErr_SetString(PyExc_RuntimeError,
40 "an error occurred in isl_basic_set_n_constraint");
41 return NULL;
42 }
43 constraints = PyList_New(n);
44 if (constraints == NULL) {
45 return NULL;
46 }
47 list = malloc(sizeof(_isl_constraint_list));
48 if (list == NULL) {
49 Py_DECREF(constraints);
50 return PyErr_NoMemory();
51 }
52 list->cursor = 0;
53 list->constraints = constraints;
54 n = isl_basic_set_foreach_constraint(bset,
55 _isl_isl_basic_set_add_constraint_list, list);
56 free(list);
57 if (n == -1) {
58 PyErr_SetString(PyExc_RuntimeError,
59 "an error occurred in isl_basic_set_foreach_constraint");
60 Py_DECREF(constraints);
61 return NULL;
62 }
63 return constraints;
64 }
65
66 static PyMethodDef _isl_methods[] = {
67 {"isl_basic_set_constraints", _isl_isl_basic_set_constraints, METH_VARARGS, NULL},
68 {NULL, NULL, 0, NULL}
69 };
70
71 static struct PyModuleDef _islmodule = {
72 PyModuleDef_HEAD_INIT,
73 "_isl",
74 NULL,
75 0,
76 _isl_methods
77 };
78
79 PyMODINIT_FUNC PyInit__isl(void) {
80 PyObject *m;
81 m = PyModule_Create(&_islmodule);
82 if (m == NULL) {
83 return NULL;
84 }
85
86 if (PyModule_AddObject(m, "isl_dim_set", PyLong_FromLong(isl_dim_set)) == -1) {
87 return NULL;
88 }
89
90 return m;
91 }