[Cython] overflow bug?

Stefan Behnel stefan_ml at behnel.de
Sat Sep 5 11:22:47 CEST 2015


Hi Mark!

Mark Florisson schrieb am 14.07.2015 um 20:34:
> I think this might be a bug (python 3.3 + cython 0.22.1):
> 
>     def f(term=b"12345"):
> 
>     val = int('987278186585')
>     # The below line does not work, because it treats 1 as a constant integer
>     # in the C code (32 bit on my machine). Using 1L does work however.
>     val -= 1 << (len(term) * 8)
>     return val
> 
> print(f())
> 
> This works in pure-python, but Cython generates '1 <<
> __pyx_t_somevar', which I think treats the '1' as an integer (causing
> it to overflow). Using '1L' works in the Cython code however (but that
> may be just my platform).

Thanks for the report. Yes, the generated C code evaluates the shift
operation in C space as

     (1 << (__pyx_t_2 * 8))

And yes, I consider this a bug. It's not entirely easy to find the right
fix, though. I mean, the obvious thing to do would be to evaluate all
left-shift operations in Python space, but that wouldn't be fast for the
"normal" cases. Not sure if there's a point where to draw a line, though...

Stefan



More information about the cython-devel mailing list