Ticket #405 (closed defect: fixed)
[with patch] PyInt functions generate warnings under -Wextra
| Reported by: | lodatom | Owned by: | lodatom |
|---|---|---|---|
| Priority: | minor | Milestone: | 0.12 |
| Component: | Code Generation | Keywords: | warnings |
| Cc: |
Description
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.
Attachments
Change History
Note: See
TracTickets for help on using
tickets.

