Commit 1275882b authored by Stefan Behnel's avatar Stefan Behnel

reduce code duplication in inlined utility function

parent 93ab8309
...@@ -483,17 +483,17 @@ static CYTHON_INLINE PyObject* {{TO_PY_FUNCTION}}({{TYPE}} value) { ...@@ -483,17 +483,17 @@ static CYTHON_INLINE PyObject* {{TO_PY_FUNCTION}}({{TYPE}} value) {
/////////////// CIntFromPyVerify /////////////// /////////////// CIntFromPyVerify ///////////////
// see CIntFromPy
#define __PYX_VERIFY_RETURN_INT(target_type, func_type, func_value) \ #define __PYX_VERIFY_RETURN_INT(target_type, func_type, func_value) \
{ \ { \
func_type value = func_value; \ func_type value = func_value; \
if (sizeof(target_type) < sizeof(func_type)) { \ if (sizeof(target_type) < sizeof(func_type)) { \
if (unlikely(value != (func_type) (target_type) value)) { \ if (unlikely(value != (func_type) (target_type) value)) { \
func_type zero = 0; \ func_type zero = 0; \
PyErr_SetString(PyExc_OverflowError, \ if (is_unsigned && unlikely(value < zero)) \
(is_unsigned && unlikely(value < zero)) ? \ goto raise_neg_overflow; \
"can't convert negative value to " #target_type : \ else \
"value too large to convert to " #target_type); \ goto raise_overflow; \
return (target_type) -1; \
} \ } \
} \ } \
return (target_type) value; \ return (target_type) value; \
...@@ -527,9 +527,7 @@ static CYTHON_INLINE {{TYPE}} {{FROM_PY_FUNCTION}}(PyObject *x) { ...@@ -527,9 +527,7 @@ static CYTHON_INLINE {{TYPE}} {{FROM_PY_FUNCTION}}(PyObject *x) {
} else { } else {
long val = PyInt_AS_LONG(x); long val = PyInt_AS_LONG(x);
if (is_unsigned && unlikely(val < 0)) { if (is_unsigned && unlikely(val < 0)) {
PyErr_SetString(PyExc_OverflowError, goto raise_neg_overflow;
"can't convert negative value to {{TYPE}}");
return ({{TYPE}}) -1;
} }
return ({{TYPE}}) val; return ({{TYPE}}) val;
} }
...@@ -546,9 +544,7 @@ static CYTHON_INLINE {{TYPE}} {{FROM_PY_FUNCTION}}(PyObject *x) { ...@@ -546,9 +544,7 @@ static CYTHON_INLINE {{TYPE}} {{FROM_PY_FUNCTION}}(PyObject *x) {
#endif #endif
#endif #endif
if (unlikely(Py_SIZE(x) < 0)) { if (unlikely(Py_SIZE(x) < 0)) {
PyErr_SetString(PyExc_OverflowError, goto raise_neg_overflow;
"can't convert negative value to {{TYPE}}");
return ({{TYPE}}) -1;
} }
if (sizeof({{TYPE}}) <= sizeof(unsigned long)) { if (sizeof({{TYPE}}) <= sizeof(unsigned long)) {
__PYX_VERIFY_RETURN_INT({{TYPE}}, unsigned long, PyLong_AsUnsignedLong(x)) __PYX_VERIFY_RETURN_INT({{TYPE}}, unsigned long, PyLong_AsUnsignedLong(x))
...@@ -606,5 +602,14 @@ static CYTHON_INLINE {{TYPE}} {{FROM_PY_FUNCTION}}(PyObject *x) { ...@@ -606,5 +602,14 @@ static CYTHON_INLINE {{TYPE}} {{FROM_PY_FUNCTION}}(PyObject *x) {
Py_DECREF(tmp); Py_DECREF(tmp);
return val; return val;
} }
}
raise_overflow:
PyErr_SetString(PyExc_OverflowError,
"value too large to convert to {{TYPE}}");
return ({{TYPE}}) -1;
raise_neg_overflow:
PyErr_SetString(PyExc_OverflowError,
"can't convert negative value to {{TYPE}}");
return ({{TYPE}}) -1;
}
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