Ticket #405 (closed defect: fixed)

Opened 5 years ago

Last modified 5 years ago

[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

Changed 5 years ago by scoder

  • owner changed from somebody to lodatom
  • milestone changed from wishlist to 0.12

Changed 5 years ago by scoder

  • status changed from new to closed
  • resolution set to fixed
Note: See TracTickets for help on using tickets.