[Numpy-discussion] Py_NotImplementedType leak

Charles R Harris charlesr.harris at gmail.com
Mon Jun 23 13:23:51 EDT 2008


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?

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20080623/5bb35edd/attachment.html>


More information about the NumPy-Discussion mailing list