Defining custom types

Jonathan Wang jontwang at
Fri Oct 27 14:37:01 CDT 2006

On 10/27/06, Travis Oliphant <oliphant.travis at> wrote:
> > If I redefine the string function, I encounter another, perhaps more
> > serious problem leading to a segfault. I've defined my string function
> > to be extremely simple:
> > >>> def printer(arr):
> > ...   return str(arr[0])
> >
> > Now, if I try to print an element of the array:
> > >>> mxArr[0]
> >
> > I get to this stack trace:
> > #0  scalar_value (scalar=0x814be10, descr=0x5079e0) at
> >
> > #1  0x0079936a in PyArray_Scalar (data=0x814cf98, descr=0x5079e0,
> > base=0x814e7a8) at arrayobject.c:1419
> > #2  0x007d259f in array_subscript_nice (self=0x814e7a8, op=0x804eb8c)
> > at arrayobject.c:1985
> > #3  0x00d17dde in PyObject_GetItem (o=0x814e7a8, key=0x804eb8c) at
> > Objects/abstract.c:94
> >
> > (Note: for some reason gdb claims that arrayobject.c:1985 is
> > array_subscript_nice, but looking at my source this line is actually
> > in array_item_nice. *boggle*)
> >
> > But scalar_value returns NULL for all non-native types. So, destptr in
> > PyArray_Scalar is set to NULL, and the call the copyswap segfaults.
> >
> > Perhaps scalar_value should be checking the scalarkind field of
> > PyArray_Descr, or using the elsize and alignment fields to figure out
> > the pointer to return if scalarkind isn't set?
> Hmmm... It looks like the modifications to scalar_value did not take
> into account user-defined types.  I've added a correction so that
> user-defined types will use setitem to set the scalar value into the
> array.  Presumably your setitem function can handle setting the array
> with scalars of your new type?
> I've checked the changes into SVN.

Do there also need to be changes in to use getitem if a
user-defined type does not inherit from a Numpy scalar? i.e. at we should return some pointer calculated from the
PyArray_Descr's elsize and alignment field to get the destination for the
"custom scalar" type to be copied.

As it stands, if the user-defined type does not inherit from a Numpy scalar,
lots of things continue to break. Furthermore it seems like the scalar
conversions prefer the builtin types, but it seems to me that the
user-defined type should be preferred.

i.e. if I try to get an element from my mxDateTime array, I get a float
>>> mxArr[0] =
>>> mxArr[0][0]

But what I really want is the mxDateTime, which, oddly enough, is what
happens if I use tolist():
>>> mxArr.tolist()[0]
[<DateTime object for '2006-10-27 14:33:57.25' at a73c60>]
-------------- next part --------------
An HTML attachment was scrubbed...
-------------- next part --------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
-------------- next part --------------
Numpy-discussion mailing list
Numpy-discussion at

More information about the Numpy-discussion mailing list