[Numpy-discussion] Code generator bug and fix?

Charles R Harris charlesr.harris@gmail....
Mon Apr 21 23:01:38 CDT 2008


On Mon, Apr 21, 2008 at 9:09 PM, Travis E. Oliphant <oliphant@enthought.com>
wrote:

> Charles R Harris wrote:
> >
> >
> > On Mon, Apr 21, 2008 at 8:53 PM, Charles R Harris
> > <charlesr.harris@gmail.com <mailto:charlesr.harris@gmail.com>> wrote:
> >
> >
> >
> >     On Mon, Apr 21, 2008 at 8:28 PM, Travis E. Oliphant
> >     <oliphant@enthought.com <mailto:oliphant@enthought.com>> wrote:
> >
> >         Charles R Harris wrote:
> >         > I've gotten my own python class with a logical_not method to
> >         work
> >         > correctly if I goto
> >         numpy/core/code_generators/generate_umath.py and
> >         > change
> >         I need more context for this.   Why does the umath generator
> >         matter for
> >         your python class?
> >         >
> >         > 'logical_not' :
> >         >     Ufunc(1, 1, None,
> >         >           'returns not x elementwise.',
> >         >           TD(noobj, out='?'),
> >         >           TD(M, f='logical_not', out='?'),
> >         >           ),
> >         >
> >         > to
> >         >
> >         > 'logical_not' :
> >         >     Ufunc(1, 1, None,
> >         >           'returns not x elementwise.',
> >         >           TD(noobj, out='?'),
> >         >           TD(M, f='logical_not'),
> >         >           ),
> >         >
> >         Why is this an error?   Is the difference only removing the out
> >         variable?  It's been a while since I reviewed this code, so
> >         what does
> >         removing the out variable do functionally (What is the
> >         difference in the
> >         ufunc that is generated)?
> >
> >
> >     The way it is, it passes a boolean array to the PyUFunc_O_O_method
> >     loop where the loop is expecting an object array. I checked the
> >     step size to see this, and that's also how the generated signature
> >     reads. Consequently, when the reference count is decremented bad
> >     things happen. I suspect this hasn't been seen before because it
> >     hadn't been tried. I wrote loop tests before cleaning up the loop
> >     code and the bug turned up then.
> >
> >     My guess is that here M means object called through non-Python
> >     method (logical_not), and omitting out means the output type is
> >     the same as the input. I suspect that '?' should do the same thing
> >     and that there might be a bug in the function dispatcher or the
> >     signature generator, but I'm not clear on that yet.
> >
> >
> > If out is omitted, the output types matches the input types. Here's
> > where the output types are generated:
> >
> >     def finish_signature(self, nin, nout):
> >         if self.in_ is None:
> >             self.in_ = self.type * nin
> >         assert len(self.in_) == nin
> >         if self.out is None:
> >             self.out = self.type * nout
> >         assert len(self.out) == nout
> >
> > So that seems like the right thing to do. I still don't know what  '?'
> > means, though.
>
> Thanks Chuck,
>
> I get it now (I should have spent a minute looking at the code).  The
> '?' is the character code for "boolean"
>
> So, I think you are right about the bug.
>

I note that other logical operators, <, ==, do in fact return booleans when
operating on objects. So another fix is to write special case loops for
logical_{not, or, and, xor} that do the same. Perhaps a ticket for an
enhancement should be opened.

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


More information about the Numpy-discussion mailing list