[Numpy-discussion] ANN: numpy.i - added managed deallocation to ARGOUTVIEW_ARRAY1 (ARGOUTVIEWM_ARRAY1)
Egor Zindy
ezindy at gmail.com
Mon Dec 8 07:55:02 EST 2008
Hello list,
just a quick follow-up on the managed deallocation. This is what I've
done this week-end:
In numpy.i, I have redefined the import_array() function to also take
care of the managed memory initialisation (the _MyDeallocType.tp_new =
PyType_GenericNew; statement). This means that in %init(), the only call
is to import_array(). Basically, the same as with the "normal" numpy.i.
Only difference in a swig file (.i) between "unmanaged" and "managed"
memory allocation is the use of either the ARGOUTVIEW_ARRAY or
ARGOUTVIEWM_ARRAY fragments. Everything else is hidden.
In numpy.i, this is what's now happening (my previous attempts were a
bit clumsy):
%#undef import_array
%#define import_array() {if (_import_array() < 0) {PyErr_Print();
PyErr_SetString(PyExc_ImportError, "numpy.core.multiarray failed to
import"); return; }; _MyDeallocType.tp_new = PyType_GenericNew; if
(PyType_Ready(&_MyDeallocType) < 0) {PyErr_Print();
PyErr_SetString(PyExc_ImportError, "Custom memory management failed to
initialize (numpy.i)"); return; } }
%#undef import_array1
%#define import_array1(ret) {if (_import_array() < 0) {PyErr_Print();
PyErr_SetString(PyExc_ImportError, "numpy.core.multiarray failed to
import"); return ret; }; _MyDeallocType.tp_new = PyType_GenericNew; if
(PyType_Ready(&_MyDeallocType) < 0) {PyErr_Print();
PyErr_SetString(PyExc_ImportError, "Custom memory management failed to
initialize (numpy.i)"); return ret; } }
%#undef import_array2
%#define import_array2(msg, ret) {if (_import_array() < 0)
{PyErr_Print(); PyErr_SetString(PyExc_ImportError, msg); return ret; };
_MyDeallocType.tp_new = PyType_GenericNew; if
(PyType_Ready(&_MyDeallocType) < 0) {PyErr_Print();
PyErr_SetString(PyExc_ImportError, msg); return ret; } }
My wiki (sorry, haven't moved it to the scipy cookbook yet) has all the
details (the modified numpy.i, explanations, and some test code):
http://code.google.com/p/ezwidgets/wiki/NumpyManagedMemory
Regards,
Egor
More information about the NumPy-Discussion
mailing list