Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[with patch] PyInt functions generate warnings under -Wextra #968

Closed
robertwb opened this issue Oct 12, 2009 · 2 comments
Closed

[with patch] PyInt functions generate warnings under -Wextra #968

robertwb opened this issue Oct 12, 2009 · 2 comments

Comments

@robertwb
Copy link
Contributor

When compiling Cython modules, gcc -Wall -Wextra emits many warnings for the PyInt type conversions.

$ cat warningtest.pyx     
cdef extern from "stdint.h":
    ctypedef long int64_t
    ctypedef unsigned long uint64_t
cdef int64_t _f(int64_t x):
    return x >> 1
cdef uint64_t _fu(uint64_t x):
    return x >> 1
def f(x):
    return _f(x)
def fu(x):
    return _fu(x)
$ cython warningtest.pyx
$ gcc -fPIC -fno-strict-aliasing -g -O2 -Wall -Wextra -I/usr/include/python2.6 -shared -o warningtest.so warningtest.c
warningtest.c: In function ‘__Pyx_PyInt_from_py_uint64_t’:
warningtest.c:674: warning: comparison of unsigned expression < 0 is always false
warningtest.c:678: warning: comparison of unsigned expression < 0 is always false
warningtest.c:682: warning: comparison of unsigned expression < 0 is always false
warningtest.c:686: warning: comparison of unsigned expression < 0 is always false
warningtest.c:690: warning: comparison of unsigned expression < 0 is always false
warningtest.c: In function ‘__Pyx_PyInt_to_py_uint64_t’:
warningtest.c:708: warning: comparison of unsigned expression < 0 is always false
warningtest.c:712: warning: comparison of unsigned expression < 0 is always false
warningtest.c: In function ‘__Pyx_PyInt_AsUnsignedLong’:
warningtest.c:888: warning: comparison of unsigned expression < 0 is always false
warningtest.c:890: warning: signed and unsigned type in conditional expression
warningtest.c: In function ‘__Pyx_PyInt_AsUnsignedLongLong’:
warningtest.c:919: warning: comparison of unsigned expression < 0 is always false
warningtest.c:921: warning: signed and unsigned type in conditional expression
warningtest.c: In function ‘__Pyx_PyInt_AsLong’:
warningtest.c:952: warning: signed and unsigned type in conditional expression
warningtest.c: In function ‘__Pyx_PyInt_AsLongLong’:
warningtest.c:983: warning: signed and unsigned type in conditional expression
warningtest.c: In function ‘__Pyx_PyInt_AsSignedLong’:
warningtest.c:1014: warning: signed and unsigned type in conditional expression
warningtest.c: In function ‘__Pyx_PyInt_AsSignedLongLong’:
warningtest.c:1045: warning: signed and unsigned type in conditional expression

This is because Cython outputs code that looks like the following:

[warningtest.c:888]
        return (((unsigned long)-1) < ((unsigned long)0)) ?
               PyLong_AsLong(x) :
               PyLong_AsUnsignedLong(x);

I've attached a patch that stops these warnings by creating a new temporary variable is_unsigned, and by converting the ternary operator to a normal if/else.

In my testing, it seems to work, but it would be good to get another pairs of eyes to look at this to make sure what I did was correct.

At 2009-10-12T16:41:28Z @MarkLodato added attachment 0001-prevent-warnings-when-compiling-with-gcc-Wextra.patch

Migrated from http://trac.cython.org/ticket/405

@robertwb
Copy link
Contributor Author

scoder changed milestone from wishlist to 0.12
owner from somebody to lodatom
commented

Applied in

http://hg.cython.org/cython-devel/rev/c798ca30c65f

@robertwb
Copy link
Contributor Author

scoder changed resolution to fixed
status from new to closed
commented

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant