[Numpy-discussion] Py_NotImplementedType leak

Charles R Harris charlesr.harris@gmail....
Mon Jun 23 14:28:51 CDT 2008

On Mon, Jun 23, 2008 at 11:23 AM, Charles R Harris <
charlesr.harris@gmail.com> wrote:

> Leaks of Py_NotImplementedType to the user are regarded as errors by the
> Python developers and google turns up several patches fixing such issues. In
> numpy we have this problem because we issue the same call for, say,
> right_shift as we do for >>. This leads to things like:
> In [1]: int64(1) >> float32(1)
> ---------------------------------------------------------------------------
> TypeError                                 Traceback (most recent call last)
> /home/charris/<ipython console> in <module>()
> TypeError: unsupported operand type(s) for >>: 'int' and 'numpy.float32'
> In [2]: right_shift(int64(1),float32(1))
> Out[2]: NotImplemented
> There are several things going on here. First, all the numbers are promoted
> to 0-D object arrays, so numpy is using the logic for numpy.object scalars.
> Second, the Python interpreter knows how to deal with the >> operator for
> Python ints, which has the left and right slots, but can't deal with the
> explicit call to right_shift because it knows nothing about it. Since proper
> behavior in this case depends on knowledge of how right_shift is called I
> think the thing to do is raise a TypeError{NotImplemented} exception in the
> explicit right_shift call and move the NotImplementedType logic up to the
> __rrshift__ slot for the numpy array types. This also applies to the other
> standard numeric methods.
> Thoughts?

In particular:

Extension types can now set the type flag Py_TPFLAGS_CHECKTYPES in their
> PyTypeObject structure to indicate that they support the new coercion
> model. In such extension types, the numeric slot functions can no longer
> assume that they'll be passed two arguments of the same type; instead they
> may be passed two arguments of differing types, and can then perform their
> own internal coercion.

So the NotImplemented return is restricted to numeric slot functions. Given
this, and the fact that leaking NotImplemented to the user is a bug, I'm
going to make the fix unless someone objects.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20080623/a60845fc/attachment-0001.html 

More information about the Numpy-discussion mailing list