[MOAB-dev] r3145 - MOAB/branches/python/tools/iMesh/python

jvporter at wisc.edu jvporter at wisc.edu
Thu Sep 17 14:08:57 CDT 2009


Author: jvporter
Date: 2009-09-17 14:08:57 -0500 (Thu, 17 Sep 2009)
New Revision: 3145

Added:
   MOAB/branches/python/tools/iMesh/python/helpers.h
Removed:
   MOAB/branches/python/tools/iMesh/python/numpy_extensions.inl
Modified:
   MOAB/branches/python/tools/iMesh/python/errors.h
   MOAB/branches/python/tools/iMesh/python/iMesh.c
   MOAB/branches/python/tools/iMesh/python/numpy_extensions.h
   MOAB/branches/python/tools/iMesh/python/setup.py
Log:
* More reorganization courtesy of iGeom
* Fix minor memory leaks
* Better checks for array sizes


Modified: MOAB/branches/python/tools/iMesh/python/errors.h
===================================================================
--- MOAB/branches/python/tools/iMesh/python/errors.h	2009-09-16 19:38:55 UTC (rev 3144)
+++ MOAB/branches/python/tools/iMesh/python/errors.h	2009-09-17 19:08:57 UTC (rev 3145)
@@ -9,5 +9,6 @@
 #define ERR_ARR_DIMS      "1- or 2-dimensional array expected"
 #define ERR_TYPE_CODE     "invalid type code"
 #define ERR_ADJ_LIST      "unable to create adjacency list"
+#define ERR_INVALID_BASIS "invalid basis"
 
 #endif

Added: MOAB/branches/python/tools/iMesh/python/helpers.h
===================================================================
--- MOAB/branches/python/tools/iMesh/python/helpers.h	                        (rev 0)
+++ MOAB/branches/python/tools/iMesh/python/helpers.h	2009-09-17 19:08:57 UTC (rev 3145)
@@ -0,0 +1,62 @@
+#ifndef PYTAPS_HELPERS_H
+#define PYTAPS_HELPERS_H
+
+/* TODO: these are never freed! */
+static PyObject *g_helper_module;
+static PyObject *g_adj_list;
+static PyObject *g_ind_adj_list;
+
+/* NOTE: steals references to adj and offsets */
+static PyObject *
+AdjacencyList_New(PyObject *adj,PyObject *offsets)
+{
+    PyObject *res;
+
+    if( (res = PyObject_CallFunction(g_adj_list,"OO",adj,offsets)) == NULL)
+        PyErr_SetString(PyExc_RuntimeError,ERR_ADJ_LIST);
+
+    Py_DECREF(adj);
+    Py_DECREF(offsets);
+
+    return res;
+}
+
+/* NOTE: steals references to ents, adj, indices, and offsets */
+static PyObject *
+IndexedAdjacencyList_New(PyObject *ents, PyObject *adj,PyObject *indices,
+                         PyObject *offsets)
+{
+    PyObject *res;
+
+    if( (res = PyObject_CallFunction(g_ind_adj_list,"OOOO",ents,adj,indices,
+                                     offsets)) == NULL)
+        PyErr_SetString(PyExc_RuntimeError,ERR_ADJ_LIST);
+
+    Py_DECREF(ents);
+    Py_DECREF(adj);
+    Py_DECREF(indices);
+    Py_DECREF(offsets);
+
+    return res;
+}
+
+static int import_helpers(void)
+{
+    if( (g_helper_module = PyImport_ImportModule("itaps.helpers")) == NULL)
+        return -1;
+    if( (g_adj_list = PyObject_GetAttrString(g_helper_module,"AdjacencyList") )
+        == NULL)
+        return -1;
+    if( (g_ind_adj_list = PyObject_GetAttrString(g_helper_module,
+        "IndexedAdjacencyList")) == NULL)
+        return -1;
+
+    return 0;
+
+    /* Suppress warnings if above functions aren't used */
+    (void)AdjacencyList_New;
+    (void)IndexedAdjacencyList_New;
+
+}
+
+#endif

Modified: MOAB/branches/python/tools/iMesh/python/iMesh.c
===================================================================
--- MOAB/branches/python/tools/iMesh/python/iMesh.c	2009-09-16 19:38:55 UTC (rev 3144)
+++ MOAB/branches/python/tools/iMesh/python/iMesh.c	2009-09-17 19:08:57 UTC (rev 3145)
@@ -1,7 +1,8 @@
 #define _IMESH_MODULE
+#include "iMesh_Python.h"
 #include "errors.h"
 #include "common.h"
-#include "iMesh_Python.h"
+#include "helpers.h"
 #include "numpy_extensions.h"
 
 static enum iBase_TagValueType char_to_type(char c);
@@ -14,12 +15,6 @@
 static PyTypeObject iMeshTag_Type;
 static int NPY_IMESHTAG;
 
-
-/* TODO: these are never freed! */
-static PyObject *g_helper_module;
-static PyObject *g_adj_list;
-static PyObject *g_ind_adj_list;
-
 static int
 checkError(iMesh_Instance mesh,int err)
 {
@@ -35,40 +30,6 @@
         return 0;
 }
 
-/* NOTE: steals references to adj and offsets */
-static PyObject *
-AdjacencyList_New(PyObject *adj,PyObject *offsets)
-{
-    PyObject *res;
-
-    if( (res = PyObject_CallFunction(g_adj_list,"OO",adj,offsets)) == NULL)
-        PyErr_SetString(PyExc_RuntimeError,ERR_ADJ_LIST);
-
-    Py_DECREF(adj);
-    Py_DECREF(offsets);
-
-    return res;
-}
-
-/* NOTE: steals references to ents, adj, indices, and offsets */
-static PyObject *
-IndexedAdjacencyList_New(PyObject *ents, PyObject *adj,PyObject *indices,
-                         PyObject *offsets)
-{
-    PyObject *res;
-
-    if( (res = PyObject_CallFunction(g_ind_adj_list,"OOOO",ents,adj,indices,
-                                     offsets)) == NULL)
-        PyErr_SetString(PyExc_RuntimeError,ERR_ADJ_LIST);
-
-    Py_DECREF(ents);
-    Py_DECREF(adj);
-    Py_DECREF(indices);
-    Py_DECREF(offsets);
-
-    return res;
-}
-
 static iMeshEntitySet_Object *
 iMeshEntitySet_New(iMesh_Object *instance)
 {
@@ -139,7 +100,7 @@
     if(checkError(self->handle,err))
         return NULL;
 
-    return Py_BuildValue("i",dim);
+    return PyInt_FromLong(dim);
 }
 
 static int
@@ -216,6 +177,7 @@
     {
         if(storage_order == -1)
         {
+            Py_DECREF(ents);
             PyErr_SetString(PyExc_ValueError,ERR_STORAGE_ORDER);
             return NULL;
         }
@@ -234,11 +196,7 @@
         if(checkError(self->handle,err))
             return NULL;
 
-        npy_intp outer;
-        if(storage_order == iBase_BLOCKED)
-            outer = 3;
-        else
-            outer = size;
+        npy_intp outer = (storage_order == iBase_BLOCKED) ? 3:size;
         /* TODO: think about this */
         npy_intp dims[] = {outer, coords_size/outer};
         return PyArray_NewFromMalloc(2,dims,NPY_DOUBLE,coords);
@@ -249,7 +207,10 @@
         iMesh_getVtxCoord(self->handle,iBaseEntity_GetHandle(obj), v+0,v+1,v+2,
                           &err);
         if(checkError(self->handle,err))
+        {
+            free(v);
             return NULL;
+        }
 
         npy_intp dims[] = {3};
         return PyArray_NewFromMalloc(1,dims,NPY_DOUBLE,v);
@@ -281,8 +242,8 @@
         size = PyArray_SIZE(ents);
         data = PyArray_DATA(ents);
 
-        iMesh_getEntArrTopo(self->handle,data,size,&topos,&topo_alloc,&topo_size,
-                            &err);
+        iMesh_getEntArrTopo(self->handle,data,size,&topos,&topo_alloc,
+                            &topo_size,&err);
         Py_DECREF(ents);
         if(checkError(self->handle,err))
             return NULL;
@@ -328,8 +289,8 @@
         size = PyArray_SIZE(ents);
         data = PyArray_DATA(ents);
       
-        iMesh_getEntArrType(self->handle,data,size,&types,&type_alloc,&type_size,
-                            &err);
+        iMesh_getEntArrType(self->handle,data,size,&types,&type_alloc,
+                            &type_size,&err);
         Py_DECREF(ents);
         if(checkError(self->handle,err))
             return NULL;
@@ -390,19 +351,6 @@
         return AdjacencyList_New(
             PyArray_NewFromMalloc(1,adj_dims,NPY_IBASEENT,adj),
             PyArray_NewFromMalloc(1,off_dims,NPY_INT,offsets) );
-
-        PyObject *pair = PyTuple_New(2);
-        npy_intp dims[1];
- 
-        dims[0] = adj_size;
-        PyTuple_SET_ITEM(pair, 0,
-            PyArray_NewFromMalloc(1,dims,NPY_IBASEENT,adj));
-
-        dims[0] = offsets_size;
-        PyTuple_SET_ITEM(pair, 1,
-            PyArray_NewFromMalloc(1,dims,NPY_INT,offsets));
-
-        return pair;
     }
     else if(iBaseEntity_Check(obj))
     {
@@ -461,19 +409,6 @@
         return AdjacencyList_New(
             PyArray_NewFromMalloc(1,adj_dims,NPY_IBASEENT,adj),
             PyArray_NewFromMalloc(1,off_dims,NPY_INT,offsets) );
-
-        PyObject *pair = PyTuple_New(2);
-        npy_intp dims[1];
- 
-        dims[0] = adj_size;
-        PyTuple_SET_ITEM(pair, 0,
-            PyArray_NewFromMalloc(1,dims,NPY_IBASEENT,adj));
-
-        dims[0] = offsets_size;
-        PyTuple_SET_ITEM(pair, 1,
-            PyArray_NewFromMalloc(1,dims,NPY_INT,offsets));
-
-        return pair;
     }
     else if(iBaseEntity_Check(obj))
     {
@@ -554,11 +489,13 @@
     {
         if(storage_order == -1)
         {
+            Py_DECREF(ents);
             PyErr_SetString(PyExc_ValueError,ERR_STORAGE_ORDER);
             goto err;
         }
 
-        verts = PyArray_FROMANY(data,NPY_DOUBLE,2,2,NPY_C_CONTIGUOUS);
+        verts = PyArray_ToVectors(data,NPY_DOUBLE,2,3,
+                                  storage_order==iBase_INTERLEAVED);
         if(verts == NULL)
             goto err;
 
@@ -574,16 +511,10 @@
     }
     else if(iBaseEntity_Check(obj))
     {
-        verts = PyArray_FROMANY(data,NPY_DOUBLE,1,1,NPY_C_CONTIGUOUS);
+        verts = PyArray_ToVectors(data,NPY_DOUBLE,1,3,0);
         if(verts == NULL)
             goto err;
 
-        if(PyArray_SIZE(verts) != 3)
-        {
-            PyErr_SetString(PyExc_ValueError,ERR_ARR_SIZE);
-            goto err;
-        }
-
         double *v = PyArray_DATA(verts);
         iBase_EntityHandle entity = iBaseEntity_GetHandle(obj);
 
@@ -698,10 +629,7 @@
         return NULL;
     }
 
-    PyObject *pair = PyTuple_New(2);
-    PyTuple_SET_ITEM(pair,0,(PyObject*)entity);
-    PyTuple_SET_ITEM(pair,1,Py_BuildValue("i",status));
-    return pair;
+    return Py_BuildValue("(Oi)",entity,status);
 }
 
 static PyObject *
@@ -731,18 +659,12 @@
     if(checkError(self->handle,err))
         return NULL;
 
-    PyObject *pair = PyTuple_New(2);
-    npy_intp dims[1];
-
-    dims[0] = ent_size;
-    PyTuple_SET_ITEM(pair, 0,
-        PyArray_NewFromMalloc(1,dims,NPY_IBASEENT,entities));
-
-    dims[0] = stat_size;
-    PyTuple_SET_ITEM(pair, 1,
-        PyArray_NewFromMalloc(1,dims,NPY_INT,status));
-
-    return pair;
+    npy_intp ent_dims[] = {ent_size};
+    npy_intp stat_dims[] = {stat_size};
+    return Py_BuildValue("(OO)",
+        PyArray_NewFromMalloc(1,ent_dims,NPY_IBASEENT,entities),
+        PyArray_NewFromMalloc(1,stat_dims,NPY_INT,status)
+        );
 }
 
 
@@ -1077,6 +999,7 @@
     m = Py_InitModule("iMesh",module_methods);
     import_array();
     import_iBase();
+    import_helpers();
 
     /***** register C API *****/
     static void *IMesh_API[6];
@@ -1096,16 +1019,6 @@
     if(api_obj != NULL)
         PyModule_AddObject(m, "_C_API", api_obj);
 
-    /***** import helper module *****/
-    if( (g_helper_module = PyImport_ImportModule("itaps.helpers")) == NULL)
-        return;
-    if( (g_adj_list = PyObject_GetAttrString(g_helper_module,"AdjacencyList") )
-        == NULL)
-        return;
-    if( (g_ind_adj_list = PyObject_GetAttrString(g_helper_module,
-        "IndexedAdjacencyList")) == NULL)
-        return;
-
     REGISTER_SIMPLE(m,"Mesh",iMesh);
 
     /***** initialize topology enum *****/
@@ -1170,4 +1083,3 @@
 #include "iMesh_entSet.inl"
 #include "iMesh_iter.inl"
 #include "iMesh_tag.inl"
-#include "numpy_extensions.inl"

Modified: MOAB/branches/python/tools/iMesh/python/numpy_extensions.h
===================================================================
--- MOAB/branches/python/tools/iMesh/python/numpy_extensions.h	2009-09-16 19:38:55 UTC (rev 3144)
+++ MOAB/branches/python/tools/iMesh/python/numpy_extensions.h	2009-09-17 19:08:57 UTC (rev 3145)
@@ -10,8 +10,40 @@
     void *memory;
 } ArrDealloc_Object;
 
-static PyTypeObject ArrDealloc_Type;
+static void
+ArrDeallocObj_dealloc(ArrDealloc_Object *self)
+{
+    free(self->memory);
+    Py_XDECREF(self->base);
 
+    self->ob_type->tp_free((PyObject *)self);
+}
+
+static PyTypeObject ArrDealloc_Type = {
+    PyObject_HEAD_INIT(NULL)
+    0,                                          /* ob_size */
+    "arrdealloc",                               /* tp_name */
+    sizeof(ArrDealloc_Object),                  /* tp_basicsize */
+    0,                                          /* tp_itemsize */
+    (destructor)ArrDeallocObj_dealloc,          /* tp_dealloc */
+    0,                                          /* tp_print */
+    0,                                          /* tp_getattr */
+    0,                                          /* tp_setattr */
+    0,                                          /* tp_compare */
+    0,                                          /* tp_repr */
+    0,                                          /* tp_as_number */
+    0,                                          /* tp_as_sequence */
+    0,                                          /* tp_as_mapping */
+    0,                                          /* tp_hash */
+    0,                                          /* tp_call */
+    0,                                          /* tp_str */
+    0,                                          /* tp_getattro */
+    0,                                          /* tp_setattro */
+    0,                                          /* tp_as_buffer */
+    Py_TPFLAGS_DEFAULT,                         /* tp_flags */
+    "Internal deallocator object",              /* tp_doc */
+};
+
 #define PyArray_NewFromMalloc(nd,dims,typenum,data) \
     PyArray_NewFromMallocBaseStrided(nd,dims,NULL,typenum,data,0)
 
@@ -23,9 +55,45 @@
 
 static PyObject *
 PyArray_NewFromMallocBaseStrided(int nd,npy_intp *dims,npy_intp *strides,
-                                 int typenum,void *data,PyObject *base);
+                                 int typenum,void *data,PyObject *base)
+{
+    ArrDealloc_Object *newobj;
+    PyObject *arr = PyArray_New(&PyArray_Type,nd,dims,typenum,strides,data,0,
+                                NPY_CARRAY,NULL);
 
+    newobj = PyObject_New(ArrDealloc_Object,&ArrDealloc_Type);
+    newobj->memory = data;
+    Py_XINCREF(base);
+    newobj->base = base;
+
+    PyArray_BASE(arr) = (PyObject*)newobj;
+    return arr;
+}
+
 static PyObject *
-PyArray_TryFromObject(PyObject *obj,int typenum,int min_depth,int max_depth);
+PyArray_TryFromObject(PyObject *obj,int typenum,int min_depth,int max_depth)
+{
+    PyObject *ret = PyArray_FromAny(obj,PyArray_DescrFromType(typenum),
+                                    min_depth,max_depth,NPY_C_CONTIGUOUS,NULL);
+    PyErr_Clear();
+    return ret;
+}
 
+static PyObject *
+PyArray_ToVectors(PyObject *obj,int typenum,int nd,npy_intp vec_dim,int index)
+{
+    assert(index < nd);
+    PyObject *ret = PyArray_FromAny(obj,PyArray_DescrFromType(typenum),nd,nd,
+                                    NPY_C_CONTIGUOUS,NULL);
+    if(ret == NULL)
+        return NULL;
+    if(PyArray_DIM(ret,index) != vec_dim)
+    {
+        Py_DECREF(ret);
+        PyErr_SetString(PyExc_ValueError,"Expected n-d vector"); /* TODO */
+        return NULL;
+    }
+    return ret;
+}
+
 #endif

Deleted: MOAB/branches/python/tools/iMesh/python/numpy_extensions.inl
===================================================================
--- MOAB/branches/python/tools/iMesh/python/numpy_extensions.inl	2009-09-16 19:38:55 UTC (rev 3144)
+++ MOAB/branches/python/tools/iMesh/python/numpy_extensions.inl	2009-09-17 19:08:57 UTC (rev 3145)
@@ -1,59 +0,0 @@
-static PyObject *
-PyArray_TryFromObject(PyObject *obj,int typenum,int min_depth,int max_depth)
-{
-    PyObject *ret = PyArray_FromAny(obj,PyArray_DescrFromType(typenum),
-                                    min_depth,max_depth,NPY_C_CONTIGUOUS,NULL);
-    PyErr_Clear();
-    return ret;
-}
-
-static void
-ArrDeallocObj_dealloc(ArrDealloc_Object *self)
-{
-    free(self->memory);
-    Py_XDECREF(self->base);
-
-    self->ob_type->tp_free((PyObject *)self);
-}
-
-static PyTypeObject ArrDealloc_Type = {
-    PyObject_HEAD_INIT(NULL)
-    0,                                          /* ob_size */
-    "arrdealloc",                               /* tp_name */
-    sizeof(ArrDealloc_Object),                  /* tp_basicsize */
-    0,                                          /* tp_itemsize */
-    (destructor)ArrDeallocObj_dealloc,          /* tp_dealloc */
-    0,                                          /* tp_print */
-    0,                                          /* tp_getattr */
-    0,                                          /* tp_setattr */
-    0,                                          /* tp_compare */
-    0,                                          /* tp_repr */
-    0,                                          /* tp_as_number */
-    0,                                          /* tp_as_sequence */
-    0,                                          /* tp_as_mapping */
-    0,                                          /* tp_hash */
-    0,                                          /* tp_call */
-    0,                                          /* tp_str */
-    0,                                          /* tp_getattro */
-    0,                                          /* tp_setattro */
-    0,                                          /* tp_as_buffer */
-    Py_TPFLAGS_DEFAULT,                         /* tp_flags */
-    "Internal deallocator object",              /* tp_doc */
-};
-
-static PyObject *
-PyArray_NewFromMallocBaseStrided(int nd,npy_intp *dims,npy_intp *strides,
-                                 int typenum,void *data,PyObject *base)
-{
-    ArrDealloc_Object *newobj;
-    PyObject *arr = PyArray_New(&PyArray_Type,nd,dims,typenum,strides,data,0,
-                                NPY_CARRAY,NULL);
-
-    newobj = PyObject_New(ArrDealloc_Object,&ArrDealloc_Type);
-    newobj->memory = data;
-    Py_XINCREF(base);
-    newobj->base = base;
-
-    PyArray_BASE(arr) = (PyObject*)newobj;
-    return arr;
-}

Modified: MOAB/branches/python/tools/iMesh/python/setup.py
===================================================================
--- MOAB/branches/python/tools/iMesh/python/setup.py	2009-09-16 19:38:55 UTC (rev 3144)
+++ MOAB/branches/python/tools/iMesh/python/setup.py	2009-09-17 19:08:57 UTC (rev 3145)
@@ -48,10 +48,10 @@
                   )
 
 iMesh = Extension('itaps.iMesh',
-                  depends = ['common.h', 'errors.h', 'iMesh_Python.h',
-                             'iBase_Python.h', 'iMesh_entSet.inl',
-                             'iMesh_iter.inl', 'iMesh_tag.inl',
-                             'numpy_extensions.h', 'numpy_extensions.inl'],
+                  depends = ['common.h', 'errors.h', 'helpers.h', 
+                             'iMesh_Python.h', 'iBase_Python.h',
+                             'iMesh_entSet.inl', 'iMesh_iter.inl',
+                             'iMesh_tag.inl', 'numpy_extensions.h'],
                   sources = ['iMesh.c']
                   )
 



More information about the moab-dev mailing list