Exception handling 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 return PyList_SetItem(list->constraints, list->cursor++, value);
23 }
24
25 static PyObject * _isl_isl_basic_set_constraints(PyObject *self,
26 PyObject* args) {
27 long ptr;
28 isl_basic_set *bset;
29 int n;
30 PyObject *constraints;
31 _isl_constraint_list *list;
32
33 if (!PyArg_ParseTuple(args, "l", &ptr))
34 return NULL;
35 bset = (isl_basic_set*) ptr;
36 n = isl_basic_set_n_constraint(bset);
37 if (n == -1) {
38 PyErr_SetString(PyExc_RuntimeError,
39 "an error occurred in isl_basic_set_n_constraint");
40 return NULL;
41 }
42 constraints = PyList_New(n);
43 if (constraints == NULL) {
44 return NULL;
45 }
46 list = malloc(sizeof(_isl_constraint_list));
47 if (list == NULL) {
48 Py_DECREF(constraints);
49 return PyErr_NoMemory();
50 }
51 list->cursor = 0;
52 list->constraints = constraints;
53 n = isl_basic_set_foreach_constraint(bset,
54 _isl_isl_basic_set_add_constraint_list, list);
55 free(list);
56 if (n == -1) {
57 PyErr_SetString(PyExc_RuntimeError,
58 "an error occurred in isl_basic_set_foreach_constraint");
59 Py_DECREF(constraints);
60 return NULL;
61 }
62 return constraints;
63 }
64
65 static PyMethodDef _isl_methods[] = {
66 {"isl_basic_set_constraints", _isl_isl_basic_set_constraints, METH_VARARGS, NULL},
67 {NULL, NULL, 0, NULL}
68 };
69
70 static struct PyModuleDef _islmodule = {
71 PyModuleDef_HEAD_INIT,
72 "_isl",
73 NULL,
74 0,
75 _isl_methods
76 };
77
78 PyMODINIT_FUNC PyInit__isl(void) {
79 PyObject *m;
80 m = PyModule_Create(&_islmodule);
81 if (m == NULL) {
82 return NULL;
83 }
84
85 if (PyModule_AddObject(m, "isl_dim_set", PyLong_FromLong(isl_dim_set)) == -1) {
86 return NULL;
87 }
88
89 return m;
90 }