Hi, 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). Cheers, Mark
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
On 2015-09-05 11:22, Stefan Behnel wrote:
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...
To me, it's not different than overflow when multiplying C integers. So I don't really consider it a bug. It is something which could be dealt with by the overflowcheck directive.
participants (3)
-
Jeroen Demeyer -
Mark Florisson -
Stefan Behnel