[Numpy-discussion] segfaults when passing ndarray subclass to ufunc with out=None

Darren Dale dsdale24@gmail....
Sun Aug 30 19:28:08 CDT 2009


Hi Stefan,

I think Chuck applied the patch after I filed a ticket at the trac
website. http://projects.scipy.org/numpy/ticket/1022 . I just tried
running the script I posted with the most recent checkout and numpy
raised an error instead of segfaulting, so I think this issue is
clear. Thank you for following up.

Darren

2009/8/30 Stéfan van der Walt <stefan@sun.ac.za>:
> Hi, Darren
>
> Is this problem still present?  If so, we should fix it before 1.4 is released.
>
> Regards
> Stéfan
>
>
> ---------- Forwarded message ----------
> From: Darren Dale <dsdale24@gmail.com>
> Date: 2009/3/8
> Subject: Re: [Numpy-discussion] segfaults when passing ndarray
> subclass to ufunc with out=None
> To: Discussion of Numerical Python <numpy-discussion@scipy.org>
>
>
>
>
> On Sun, Feb 8, 2009 at 12:49 PM, Darren Dale <dsdale24@gmail.com> wrote:
>>
>> I am seeing some really strange behavior when I try to pass an ndarray subclass and out=None to numpy's ufuncs. This example will reproduce the problem with svn numpy, the first print statement yields 1 as expected, the second yields  "<type 'builtin_function_or_method'>" and the third yields a segmentation fault:
>>
>> import numpy as np
>>
>> class MyArray(np.ndarray):
>>
>>     __array_priority__ = 20
>>
>>     def __new__(cls):
>>         return np.asarray(1).view(cls).copy()
>>
>>     def __repr__(self):
>>         return 'my_array'
>>
>>     __str__ = __repr__
>>
>>     def __mul__(self, other):
>>         return super(MyArray, self).__mul__(other)
>>
>>     def __rmul__(self, other):
>>         return super(MyArray, self).__rmul__(other)
>>
>> mine = MyArray()
>> print np.multiply(1, 1, None)
>> x = np.multiply(mine, mine, None)
>> print type(x)
>> print x
>
>
> I think I might have found a fix for this. The following patch allows
> my script to run without a segfault:
>
> $ svn diff
> Index: umath_ufunc_object.inc
> ===================================================================
> --- umath_ufunc_object.inc      (revision 6566)
> +++ umath_ufunc_object.inc      (working copy)
> @@ -3212,13 +3212,10 @@
>          output_wrap[i] = wrap;
>          if (j < nargs) {
>              obj = PyTuple_GET_ITEM(args, j);
> -            if (obj == Py_None) {
> -                continue;
> -            }
>              if (PyArray_CheckExact(obj)) {
>                  output_wrap[i] = Py_None;
>              }
> -            else {
> +            else if (obj != Py_None) {
>                  PyObject *owrap = PyObject_GetAttrString(obj,"__array_wrap__");
>                  incref = 0;
>                  if (!(owrap) || !(PyCallable_Check(owrap))) {
>
>
> That call to continue skipped this bit of code in the loop, which is
> apparently important:
>
>         if (incref) {
>             Py_XINCREF(output_wrap[i]);
>         }
>
>
> I've tested the trunk on 64 bit linux, with and without this patch
> applied, and I get the same result in both cases: 1 known failure, 11
> skips. Is there any chance someone could consider applying this patch
> before 1.3 ships?
>
> Darren
>
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>



-- 
"In our description of nature, the purpose is not to disclose the real
essence of the phenomena but only to track down, so far as it is
possible, relations between the manifold aspects of our experience" -
Niels Bohr

"It is a bad habit of physicists to take their most successful
abstractions to be real properties of our world." - N. David Mermin

"Once we have granted that any physical theory is essentially only a
model for the world of experience, we must renounce all hope of
finding anything like the correct theory ... simply because the
totality of experience is never accessible to us." - Hugh Everett III


More information about the NumPy-Discussion mailing list