Commit dabef9db authored by Mark Lodato's avatar Mark Lodato

prevent warnings when compiling with `gcc -Wextra`

The PyInt conversion functions generate two warnings when compiled under
`gcc -Wall -Wextra`:

1. comparison of unsigned expression < 0 is always false

2. signed and unsigned type in conditional expression

This patch fixes these problems by creating a new temporary variable
`is_unsigned`, which fixes problem 1, and by converting the ternary
return expression into a normal if/else branch, which fixes problem 2.
parent af702c96
...@@ -621,12 +621,14 @@ static INLINE %(type)s __Pyx_PyInt_As%(SignWord)s%(TypeName)s(PyObject *); ...@@ -621,12 +621,14 @@ static INLINE %(type)s __Pyx_PyInt_As%(SignWord)s%(TypeName)s(PyObject *);
""", """,
impl=""" impl="""
static INLINE %(type)s __Pyx_PyInt_As%(SignWord)s%(TypeName)s(PyObject* x) { static INLINE %(type)s __Pyx_PyInt_As%(SignWord)s%(TypeName)s(PyObject* x) {
const %(type)s neg_one = (%(type)s)-1, zero = 0;
const int is_unsigned = neg_one > zero;
if (sizeof(%(type)s) < sizeof(long)) { if (sizeof(%(type)s) < sizeof(long)) {
long val = __Pyx_PyInt_AsLong(x); long val = __Pyx_PyInt_AsLong(x);
if (unlikely(val != (long)(%(type)s)val)) { if (unlikely(val != (long)(%(type)s)val)) {
if (!unlikely(val == -1 && PyErr_Occurred())) { if (!unlikely(val == -1 && PyErr_Occurred())) {
PyErr_SetString(PyExc_OverflowError, PyErr_SetString(PyExc_OverflowError,
(((%(type)s)-1) > ((%(type)s)0) && unlikely(val < 0)) ? (is_unsigned && unlikely(val < 0)) ?
"can't convert negative value to %(type)s" : "can't convert negative value to %(type)s" :
"value too large to convert to %(type)s"); "value too large to convert to %(type)s");
} }
...@@ -644,10 +646,12 @@ static INLINE %(type)s __Pyx_PyInt_As%(SignWord)s%(TypeName)s(PyObject *); ...@@ -644,10 +646,12 @@ static INLINE %(type)s __Pyx_PyInt_As%(SignWord)s%(TypeName)s(PyObject *);
""", """,
impl=""" impl="""
static INLINE %(type)s __Pyx_PyInt_As%(SignWord)s%(TypeName)s(PyObject* x) { static INLINE %(type)s __Pyx_PyInt_As%(SignWord)s%(TypeName)s(PyObject* x) {
const %(type)s neg_one = (%(type)s)-1, zero = 0;
const int is_unsigned = neg_one > zero;
#if PY_VERSION_HEX < 0x03000000 #if PY_VERSION_HEX < 0x03000000
if (likely(PyInt_Check(x))) { if (likely(PyInt_Check(x))) {
long val = PyInt_AS_LONG(x); long val = PyInt_AS_LONG(x);
if (((%(type)s)-1) > ((%(type)s)0) && unlikely(val < 0)) { if (is_unsigned && unlikely(val < 0)) {
PyErr_SetString(PyExc_OverflowError, PyErr_SetString(PyExc_OverflowError,
"can't convert negative value to %(type)s"); "can't convert negative value to %(type)s");
return (%(type)s)-1; return (%(type)s)-1;
...@@ -656,14 +660,16 @@ static INLINE %(type)s __Pyx_PyInt_As%(SignWord)s%(TypeName)s(PyObject* x) { ...@@ -656,14 +660,16 @@ static INLINE %(type)s __Pyx_PyInt_As%(SignWord)s%(TypeName)s(PyObject* x) {
} else } else
#endif #endif
if (likely(PyLong_Check(x))) { if (likely(PyLong_Check(x))) {
if (((%(type)s)-1) > ((%(type)s)0) && unlikely(Py_SIZE(x) < 0)) { if (is_unsigned) {
PyErr_SetString(PyExc_OverflowError, if (unlikely(Py_SIZE(x) < 0)) {
"can't convert negative value to %(type)s"); PyErr_SetString(PyExc_OverflowError,
return (%(type)s)-1; "can't convert negative value to %(type)s");
return (%(type)s)-1;
}
return PyLong_AsUnsigned%(TypeName)s(x);
} else {
return PyLong_As%(TypeName)s(x);
} }
return (((%(type)s)-1) < ((%(type)s)0)) ?
PyLong_As%(TypeName)s(x) :
PyLong_AsUnsigned%(TypeName)s(x);
} else { } else {
%(type)s val; %(type)s val;
PyObject *tmp = __Pyx_PyNumber_Int(x); PyObject *tmp = __Pyx_PyNumber_Int(x);
...@@ -681,35 +687,44 @@ static INLINE %(type)s __Pyx_PyInt_from_py_%(TypeName)s(PyObject *); ...@@ -681,35 +687,44 @@ static INLINE %(type)s __Pyx_PyInt_from_py_%(TypeName)s(PyObject *);
""", """,
impl=""" impl="""
static INLINE %(type)s __Pyx_PyInt_from_py_%(TypeName)s(PyObject* x) { static INLINE %(type)s __Pyx_PyInt_from_py_%(TypeName)s(PyObject* x) {
/**/ if (sizeof(%(type)s) == sizeof(char)) const %(type)s neg_one = (%(type)s)-1, zero = 0;
return (((%(type)s)-1) < ((%(type)s)0)) ? const int is_unsigned = neg_one > zero;
(%(type)s)__Pyx_PyInt_AsSignedChar(x) : if (sizeof(%(type)s) == sizeof(char)) {
(%(type)s)__Pyx_PyInt_AsUnsignedChar(x); if (is_unsigned)
else if (sizeof(%(type)s) == sizeof(short)) return (%(type)s)__Pyx_PyInt_AsUnsignedChar(x);
return (((%(type)s)-1) < ((%(type)s)0)) ? else
(%(type)s)__Pyx_PyInt_AsSignedShort(x) : return (%(type)s)__Pyx_PyInt_AsSignedChar(x);
(%(type)s)__Pyx_PyInt_AsUnsignedShort(x); } else if (sizeof(%(type)s) == sizeof(short)) {
else if (sizeof(%(type)s) == sizeof(int)) if (is_unsigned)
return (((%(type)s)-1) < ((%(type)s)0)) ? return (%(type)s)__Pyx_PyInt_AsUnsignedShort(x);
(%(type)s)__Pyx_PyInt_AsSignedInt(x) : else
(%(type)s)__Pyx_PyInt_AsUnsignedInt(x); return (%(type)s)__Pyx_PyInt_AsSignedShort(x);
else if (sizeof(%(type)s) == sizeof(long)) } else if (sizeof(%(type)s) == sizeof(int)) {
return (((%(type)s)-1) < ((%(type)s)0)) ? if (is_unsigned)
(%(type)s)__Pyx_PyInt_AsSignedLong(x) : return (%(type)s)__Pyx_PyInt_AsUnsignedInt(x);
(%(type)s)__Pyx_PyInt_AsUnsignedLong(x); else
else if (sizeof(%(type)s) == sizeof(PY_LONG_LONG)) return (%(type)s)__Pyx_PyInt_AsSignedInt(x);
return (((%(type)s)-1) < ((%(type)s)0)) ? } else if (sizeof(%(type)s) == sizeof(long)) {
(%(type)s)__Pyx_PyInt_AsSignedLongLong(x) : if (is_unsigned)
(%(type)s)__Pyx_PyInt_AsUnsignedLongLong(x); return (%(type)s)__Pyx_PyInt_AsUnsignedLong(x);
else
return (%(type)s)__Pyx_PyInt_AsSignedLong(x);
} else if (sizeof(%(type)s) == sizeof(PY_LONG_LONG)) {
if (is_unsigned)
return (%(type)s)__Pyx_PyInt_AsUnsignedLongLong(x);
else
return (%(type)s)__Pyx_PyInt_AsSignedLongLong(x);
#if 0 #if 0
else if (sizeof(%(type)s) > sizeof(short) && } else if (sizeof(%(type)s) > sizeof(short) &&
sizeof(%(type)s) < sizeof(int)) /* __int32 ILP64 ? */ sizeof(%(type)s) < sizeof(int)) { /* __int32 ILP64 ? */
return (((%(type)s)-1) < ((%(type)s)0)) ? if (is_unsigned)
(%(type)s)__Pyx_PyInt_AsSignedInt(x) : return (%(type)s)__Pyx_PyInt_AsUnsignedInt(x);
(%(type)s)__Pyx_PyInt_AsUnsignedInt(x); else
return (%(type)s)__Pyx_PyInt_AsSignedInt(x);
#endif #endif
PyErr_SetString(PyExc_TypeError, "%(TypeName)s"); }
return (%(type)s)-1; PyErr_SetString(PyExc_TypeError, "%(TypeName)s");
return (%(type)s)-1;
} }
""") """)
...@@ -719,16 +734,21 @@ static INLINE PyObject *__Pyx_PyInt_to_py_%(TypeName)s(%(type)s); ...@@ -719,16 +734,21 @@ static INLINE PyObject *__Pyx_PyInt_to_py_%(TypeName)s(%(type)s);
""", """,
impl=""" impl="""
static INLINE PyObject *__Pyx_PyInt_to_py_%(TypeName)s(%(type)s val) { static INLINE PyObject *__Pyx_PyInt_to_py_%(TypeName)s(%(type)s val) {
/**/ if (sizeof(%(type)s) < sizeof(long)) const %(type)s neg_one = (%(type)s)-1, zero = 0;
return PyInt_FromLong((long)val); const int is_unsigned = neg_one > zero;
else if (sizeof(%(type)s) == sizeof(long)) if (sizeof(%(type)s) < sizeof(long)) {
return (((%(type)s)-1) < ((%(type)s)0)) ? return PyInt_FromLong((long)val);
PyInt_FromLong((long)val) : } else if (sizeof(%(type)s) == sizeof(long)) {
PyLong_FromUnsignedLong((unsigned long)val); if (is_unsigned)
else /* (sizeof(%(type)s) > sizeof(long)) */ return PyLong_FromUnsignedLong((unsigned long)val);
return (((%(type)s)-1) < ((%(type)s)0)) ? else
PyLong_FromLongLong((PY_LONG_LONG)val) : return PyInt_FromLong((long)val);
PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG)val); } else { /* (sizeof(%(type)s) > sizeof(long)) */
if (is_unsigned)
return PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG)val);
else
return PyLong_FromLongLong((PY_LONG_LONG)val);
}
} }
""") """)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment