[Cython] Use of long type for intermediate integral variables

Robert McGibbon rmcgibbo at gmail.com
Wed Jul 1 11:12:34 CEST 2015


Hi,

(First time poster. Apologies in advance if I'm not following the
appropriate protocol, or if this has been posted already)

I noticed an issue on Windows when debugging an issue in scipy
<https://github.com/scipy/scipy/issues/4907>, but I think it might be a
little more general.  In some places in the generated code, it looks like
intermediate integral variables are declared with type long, even when long
is too small to hold necessary value. For example, with the code pasted
below, the value n+1 is stored in a variable of type long (using Cython
0.22.1) before being supplied to F.__getitem__.

This is especially pertinent on Windows (32 bit and 64 bit) and 32-bit
linux, where longs are 32-bits, so you get an overflow for a program like
the example below. The result is that it prints 1 instead of the expected
value, 2**53+1 = 9007199254740993. But this same issue comes up basically
whenever you do arithmetic on an array index in 64-bit Windows, for indices
larger than 2**31-1, since sizeof(long) << sizeof(void*).

```
from libc.stdint cimport int64_t

class F(object):
    def __getitem__(self, i):
        print(i)

cdef int64_t n = 2**53
f = F()
f[n+1]
```
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cython-devel/attachments/20150701/533acaa4/attachment.html>


More information about the cython-devel mailing list