Methods Polyhedron.fromsympy(), Polyhedron.tosympy()
[linpy.git] / pypol / _isl.c
index 7441d46..1b086dc 100644 (file)
@@ -1,48 +1,68 @@
-#include <stdlib.h>
-
 #include <Python.h>
+
 #include <isl/constraint.h>
 #include <isl/set.h>
 
-struct _isl_constraint_list {
+struct pointer_list {
     int cursor;
-    PyObject *constraints;
+    PyObject *pointers;
 };
-typedef struct _isl_constraint_list _isl_constraint_list;
+typedef struct pointer_list pointer_list;
 
-int _isl_isl_basic_set_add_constraint_list(__isl_take isl_constraint *c,
-        void *user) {
-    _isl_constraint_list *list;
+static int pointer_list_append_constraint(isl_constraint *c, void *user) {
+    pointer_list *list;
+    PyObject *value;
 
-    list = (_isl_constraint_list *) user;
-    return PyList_SetItem(list->constraints, list->cursor++,
-        PyLong_FromVoidPtr(c));
+    list = (pointer_list *) user;
+    value = PyLong_FromVoidPtr(c);
+    if (value == NULL) {
+        return -1;
+    }
+    PyList_SET_ITEM(list->pointers, list->cursor++, value);
+    return 0;
 }
 
-static PyObject * _isl_isl_basic_set_constraints(PyObject *self,
-        PyObject* args) {
+static PyObject * basic_set_constraints(PyObject *self, PyObject* args) {
     long ptr;
     isl_basic_set *bset;
     int n;
-    PyObject *constraints;
-    _isl_constraint_list *list;
+    PyObject *pointers;
+    pointer_list *list;
 
     if (!PyArg_ParseTuple(args, "l", &ptr))
         return NULL;
     bset = (isl_basic_set*) ptr;
     n = isl_basic_set_n_constraint(bset);
-    constraints = PyList_New(n);
-    list = malloc(sizeof(_isl_constraint_list));
+    if (n == -1) {
+        PyErr_SetString(PyExc_RuntimeError,
+            "an error occurred in isl_basic_set_n_constraint");
+        return NULL;
+    }
+    pointers = PyList_New(n);
+    if (pointers == NULL) {
+        return NULL;
+    }
+    list = malloc(sizeof(pointer_list));
+    if (list == NULL) {
+        Py_DECREF(pointers);
+        return PyErr_NoMemory();
+    }
     list->cursor = 0;
-    list->constraints = constraints;
-    isl_basic_set_foreach_constraint(bset,
-        _isl_isl_basic_set_add_constraint_list, list);
+    list->pointers = pointers;
+    n = isl_basic_set_foreach_constraint(bset, pointer_list_append_constraint,
+        list);
     free(list);
-    return constraints;
+    if (n == -1) {
+        PyErr_SetString(PyExc_RuntimeError,
+            "an error occurred in isl_basic_set_foreach_constraint");
+        Py_DECREF(pointers);
+        return NULL;
+    }
+    return pointers;
 }
 
 static PyMethodDef _isl_methods[] = {
-    {"isl_basic_set_constraints", _isl_isl_basic_set_constraints, METH_VARARGS, NULL},
+    {"basic_set_constraints", basic_set_constraints, METH_VARARGS, NULL},
     {NULL, NULL, 0, NULL}
 };
 
@@ -50,17 +70,20 @@ static struct PyModuleDef _islmodule = {
     PyModuleDef_HEAD_INIT,
     "_isl",
     NULL,
-    -1,
+    0,
     _isl_methods
 };
 
 PyMODINIT_FUNC PyInit__isl(void) {
     PyObject *m;
     m = PyModule_Create(&_islmodule);
-    if (m == NULL)
+    if (m == NULL) {
         return NULL;
+    }
 
-    PyModule_AddObject(m, "isl_dim_set", PyLong_FromLong(isl_dim_set));
+    if (PyModule_AddObject(m, "dim_set", PyLong_FromLong(isl_dim_set)) == -1) {
+        return NULL;
+    }
 
     return m;
 }