[Numpy-discussion] Help with user-defined data type
Neal Becker
ndbecker2 at gmail.com
Sun Feb 17 09:41:57 EST 2008
I'm trying out user-defined data type for numpy, but I'm a bit stuck. Maybe
someone can spot the problem?
I'm creating a descr like this:
PyArray_Descr * d = (PyArray_Descr*)(PyObject_New (PyArray_Descr,
&PyArrayDescr_Type));
d->typeobj = typeptr;
d->kind = 'O'; // ???
d->type = 'z';
d->byteorder = '=';
d->hasobject = 0;
d->elsize = sizeof (cmplx_int_t);
d->alignment = __alignof__ (cmplx_int_t);
d->subarray = 0;
d->fields = 0;
d->names = 0;
PyArray_ArrFuncs * f = new PyArray_ArrFuncs;
PyArray_InitArrFuncs (f);
f->copyswapn = ©swapn<cmplx_int_t>;
f->copyswap = ©swap<cmplx_int_t>;
f->getitem = &getitem<cmplx_int_t>;
f->setitem = &setitem<cmplx_int_t>;
d->f = f;
incref (typeptr);
Then register:
PyArray_RegisterDataType (d1);
This seems OK, I get a typenum 256.
But if I try to use it, bad things happen.
This tests creating an array, both from typenum and from descr:
PyArray_Descr* descr = PyArray_DescrFromType (typenum);
npy_intp dims[] = {10};
PyObject * o1 = PyArray_NewFromDescr (&PyArray_Type, descr, 1, dims, 0, 0,
0, 0);
PyObject * o2 = PyArray_New (&PyArray_Type, 1, dims, descr->type_num, 0,
0, 0, 0, 0);
If I try this for typenum=1, it's fine. But if I use my typenum=256 I get
>>> a,b = testit (256)
>>> a
python: Objects/typeobject.c:1418: extra_ivars: Assertion `t_size >= b_size'
failed.
Program received signal SIGABRT, Aborted.
0x0000003834c30ec5 in raise () from /lib64/libc.so.6
More information about the NumPy-Discussion
mailing list