Commits

Takafumi Arakaki  committed 885fdee

test_owndata.py gose ok ('CArrayWrap_dealloc' called)

  • Participants
  • Parent commits 78341db

Comments (0)

Files changed (2)

File carraywrap/carraywrapmodule.c

 #include "structmember.h"
 #include <numpy/arrayobject.h>
 
+#define CARRAYWRAP_VERBOSE
 #define CARRAYWRAP_MAXDIM 3
 
 typedef struct __CArrayWrap__{
 static void       CArrayWrap_dealloc(CArrayWrap* self);
 static PyObject * CArrayWrap_new(PyTypeObject *type,
 				 PyObject *args, PyObject *kwds);
-static int        CArrayWrap_init(CArrayWrap *self,
-				  PyObject *args, PyObject *kwds);
+static int CArrayWrap_init(CArrayWrap *self, PyObject *args, PyObject *kwds);
+static int CArrayWrap_traverse(CArrayWrap *self, visitproc visit, void *arg);
+static int CArrayWrap_clear(CArrayWrap *self);
 
 static void print_indent(int inum)
 {
     0,				/*tp_getattro*/
     0,				/*tp_setattro*/
     0,				/*tp_as_buffer*/
-    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,	/*tp_flags*/
+    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
+    /* Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,	/\*tp_flags*\/ */
     "CArrayWrap objects",		/* tp_doc */
-    0,				/* tp_traverse */
-    0,				/* tp_clear */
+    (traverseproc)CArrayWrap_traverse,   /* tp_traverse */
+    (inquiry)CArrayWrap_clear,           /* tp_clear */
     0,				/* tp_richcompare */
     0,				/* tp_weaklistoffset */
     0,				/* tp_iter */
   import_array();   /* required NumPy initialization */
 }
 
+static int
+CArrayWrap_traverse(CArrayWrap *self, visitproc visit, void *arg)
+{
+  Py_VISIT(self->nparray);
+  return 0;
+}
+
+static int
+CArrayWrap_clear(CArrayWrap *self)
+{
+  Py_CLEAR(self->nparray);
+  return 0;
+}
 
 static void
 CArrayWrap_dealloc(CArrayWrap* self)
 {
-  Py_XDECREF(self->nparray);
+#ifdef CARRAYWRAP_VERBOSE
+  printf("'%s' called\n", __func__);
+#endif
+  CArrayWrap_clear(self);
   free(self->data);
   switch(self->ndim){
   case 2:
 CArrayWrap_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
 {
   CArrayWrap *self;
+#ifdef CARRAYWRAP_VERBOSE
+  printf("'%s' called\n", __func__);
+#endif
   self = (CArrayWrap *)type->tp_alloc(type, 0);
   self->ndim = 0;
   self->nparray = NULL;
 {
   PyObject *obj;
   int i, elemnum;
+#ifdef CARRAYWRAP_VERBOSE
+  printf("'%s' called\n", __func__);
+#endif
 
   if (!PyTuple_Check(args)){
     PyErr_SetString( PyExc_TypeError, "bad arguments");
     PyErr_SetString( PyExc_RuntimeError, "PyArray_SimpleNewFromData fails");
     goto fail;
   }
+  /* Py_INCREF(self->nparray); */
+
+/* #define CARRAYWRAP_TEST_OWNDATA_FAIL */
+#ifndef CARRAYWRAP_TEST_OWNDATA_FAIL
+  Py_INCREF(self);
+  PyArray_BASE(self->nparray) = (PyObject *)self;
+#endif
 
   return 0;
  fail:

File carraywrap/test_owndata.py

 a = caw.nparray
 a.fill(0)
 
+print "print a.flag"
 print a.flags
 print "print a"
 print a
 print "del caw"
+del caw.nparray
 del caw
 print "print a"
 print a
+## print "del a"
+## del a