[Cython] bug in doc Tutorials/Working with NumPy

Robert Bradshaw robertwb at gmail.com
Tue Nov 10 00:44:15 EST 2015


On Fri, Nov 6, 2015 at 7:18 AM, Stepan Kasal <kasal at ucw.cz> wrote:
> Hello,
>
> I was learning Cython from this tutorial when I noticed a glitch:
>
> Near the end, after boundscheck False, some variables are changed to
> unsigned int.  But this has unexpected consequences: variables t_from and such
> are not determined correctly, as one of the arguments is negative, but gets
> cast to unsigned for comaprison.  (Yes, this is a weird feature of C.)
>
> Actually, the use of unsigned does not seem to have any impact on performance,
> so I suggest to scratch that part of tutorial.  Just explain that with boundscheck
> off, negaitive indices no longer work.
>
> (But there might perhaps be one small improvent in cython itself: with boundscheck
> off, the index might be cast to unsigned before it is used:  this way we probably
> crash on negative values, instead of silently accessing the negative offset of
> the array data.)

There's two directives: wraparound and boundscheck. The wraparound
directive can be used to disable the check for negative values. I
heartily agree that using unsigned types is the source of much
surprising pain, we shouldn't be recommending it:
https://github.com/cython/cython/commit/06f75b803d91805f3068bbad2b9241efbd5cdc44


More information about the cython-devel mailing list