bug w/macros

24 views
Skip to first unread message

Andreas van Cranenburgh

unread,
Apr 20, 2013, 10:32:30 AM4/20/13
to cython...@googlegroups.com
I seem to have discovered a bug where the declared type of a macro is not honored.

I get the following output with this code (Cython 0.18):

74976710656
bit 48 is set
281474976710656
bit 48 is really set
bit 48 is set
281474976710656
bit 48 is really set
bit 48 is NOT set?

[A separate mystery is why the first line doesn't start with "2814" like the others.]
Looking at the generated C code, in the last case the result of the TESTBIT macro is assigned to an int, when it is declared to return an unsigned long.
In the other cases it is assigned to an unsigned long variable, and it works; an explicit '!=0' also avoids the problem.

Stefan Behnel

unread,
Apr 20, 2013, 3:51:01 PM4/20/13
to cython...@googlegroups.com
Andreas van Cranenburgh, 20.04.2013 16:32:
Thanks for the report and the analysis.

I'm sure it's a problem with boolean coercion. Coercion of integer types to
bint (which Cython does in this case) should coerce the value to 0/1,
instead of just silently downcasting and truncating the value.

While it might be possible to do it only for large integer types,
consistency concerns would suggest to always do it.

Stefan

Bradley Froehle

unread,
Apr 20, 2013, 4:15:05 PM4/20/13
to cython...@googlegroups.com, stef...@behnel.de
On Saturday, April 20, 2013 12:51:01 PM UTC-7, Stefan Behnel wrote:
Andreas van Cranenburgh, 20.04.2013 16:32:
> I seem to have discovered a bug where the declared type of a macro is not
> honored.
> Code to reproduce: https://gist.github.com/andreasvc/5426158
Thanks for the report and the analysis.

I'm sure it's a problem with boolean coercion. Coercion of integer types to
bint (which Cython does in this case) should coerce the value to 0/1,
instead of just silently downcasting and truncating the value.

While it might be possible to do it only for large integer types,
consistency concerns would suggest to always do it.

Simple code to reproduce:

# bug2.pyx
def test(unsigned long foo):
    if True and foo:
        print "yes"

$ python -m "import bug2; bug2.test(1 << 48)"

As you guessed the issue is the generated code silently truncates the unsigned long into an integer.


 

Stefan Behnel

unread,
Apr 20, 2013, 4:29:40 PM4/20/13
to cython...@googlegroups.com
Bradley Froehle, 20.04.2013 22:15:
Here's a fix:

https://github.com/cython/cython/commit/beb42251d5c5ace129f75b36af949cb6cf0d38c0

Stefan

Reply all
Reply to author
Forward
0 new messages