Commit 96fbd74f authored by Robert Bradshaw's avatar Robert Bradshaw

More int type conversion simplification.

parent d5370b4b
...@@ -453,11 +453,29 @@ static CYTHON_INLINE Py_UNICODE __Pyx_PyObject_AsPy_UNICODE(PyObject* x) { ...@@ -453,11 +453,29 @@ static CYTHON_INLINE Py_UNICODE __Pyx_PyObject_AsPy_UNICODE(PyObject* x) {
/////////////// CIntToPy /////////////// /////////////// CIntToPy ///////////////
/////////////// CIntFromPyVerify ///////////////
#define __PYX_VERIFY_RETURN_INT(type, value_type, func) \
{ \
value_type value = func(x); \
if (sizeof(type) < sizeof(value_type)) { \
if (unlikely(value != (type) value)) { \
PyErr_SetString(PyExc_OverflowError, \
(is_unsigned && unlikely(value < 0)) ? \
"can't convert negative value to " #type : \
"value too large to convert to " #type); \
return (type) -1; \
} \
} \
return (type) value; \
}
/////////////// CIntFromPy.proto /////////////// /////////////// CIntFromPy.proto ///////////////
static CYTHON_INLINE {{TYPE}} {{FROM_PY_FUNCTION}}(PyObject *); static CYTHON_INLINE {{TYPE}} {{FROM_PY_FUNCTION}}(PyObject *);
/////////////// CIntFromPy /////////////// /////////////// CIntFromPy ///////////////
//@requires: CIntFromPyVerify
#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3 #if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
#if CYTHON_USE_PYLONG_INTERNALS #if CYTHON_USE_PYLONG_INTERNALS
...@@ -469,25 +487,16 @@ static CYTHON_INLINE {{TYPE}} {{FROM_PY_FUNCTION}}(PyObject *x) { ...@@ -469,25 +487,16 @@ static CYTHON_INLINE {{TYPE}} {{FROM_PY_FUNCTION}}(PyObject *x) {
const int is_unsigned = neg_one > const_zero; const int is_unsigned = neg_one > const_zero;
#if PY_MAJOR_VERSION < 3 #if PY_MAJOR_VERSION < 3
if (likely(PyInt_Check(x))) { if (likely(PyInt_Check(x))) {
long val = PyInt_AS_LONG(x);
if (sizeof({{TYPE}}) < sizeof(long)) { if (sizeof({{TYPE}}) < sizeof(long)) {
if (unlikely(val != ({{TYPE}}) val)) { __PYX_VERIFY_RETURN_INT({{TYPE}}, long, PyInt_AS_LONG)
PyErr_SetString(PyExc_OverflowError,
(is_unsigned && unlikely(val < 0)) ?
"can't convert negative value to {{TYPE}}" :
"value too large to convert to {{TYPE}}");
return ({{TYPE}}) -1;
} else {
return ({{TYPE}}) val;
}
} else { } else {
long val = PyInt_AS_LONG(x);
if (is_unsigned && unlikely(val < 0)) { if (is_unsigned && unlikely(val < 0)) {
PyErr_SetString(PyExc_OverflowError, PyErr_SetString(PyExc_OverflowError,
"can't convert negative value to {{TYPE}}"); "can't convert negative value to {{TYPE}}");
return ({{TYPE}}) -1; return ({{TYPE}}) -1;
} else {
return ({{TYPE}}) val;
} }
return ({{TYPE}}) val;
} }
} else } else
#endif #endif
...@@ -498,7 +507,7 @@ static CYTHON_INLINE {{TYPE}} {{FROM_PY_FUNCTION}}(PyObject *x) { ...@@ -498,7 +507,7 @@ static CYTHON_INLINE {{TYPE}} {{FROM_PY_FUNCTION}}(PyObject *x) {
if (sizeof(digit) <= sizeof({{TYPE}})) { if (sizeof(digit) <= sizeof({{TYPE}})) {
switch (Py_SIZE(x)) { switch (Py_SIZE(x)) {
case 0: return 0; case 0: return 0;
case 1: return (%(type)s) ((PyLongObject*)x)->ob_digit[0]; case 1: return ({{TYPE}}) ((PyLongObject*)x)->ob_digit[0];
} }
} }
#endif #endif
...@@ -509,25 +518,11 @@ static CYTHON_INLINE {{TYPE}} {{FROM_PY_FUNCTION}}(PyObject *x) { ...@@ -509,25 +518,11 @@ static CYTHON_INLINE {{TYPE}} {{FROM_PY_FUNCTION}}(PyObject *x) {
return ({{TYPE}}) -1; return ({{TYPE}}) -1;
} }
if (sizeof({{TYPE}}) <= sizeof(unsigned long)) { if (sizeof({{TYPE}}) <= sizeof(unsigned long)) {
unsigned long val = PyLong_AsUnsignedLong(x); __PYX_VERIFY_RETURN_INT({{TYPE}}, unsigned long, PyLong_AsUnsignedLong)
if (sizeof({{TYPE}}) < sizeof(unsigned long)
&& unlikely(val != ({{TYPE}}) val)) {
PyErr_SetString(PyExc_OverflowError,
"value too large to convert to {{TYPE}}");
return ({{TYPE}}) -1;
}
return ({{TYPE}}) val;
} else if (sizeof({{TYPE}}) <= sizeof(unsigned long long)) { } else if (sizeof({{TYPE}}) <= sizeof(unsigned long long)) {
unsigned long long val = PyLong_AsUnsignedLongLong(x); __PYX_VERIFY_RETURN_INT({{TYPE}}, unsigned long long, PyLong_AsUnsignedLongLong)
if (sizeof({{TYPE}}) < sizeof(unsigned long long)
&& unlikely(val != ({{TYPE}}) val)) {
PyErr_SetString(PyExc_OverflowError,
"value too large to convert to {{TYPE}}");
return ({{TYPE}}) -1;
}
return ({{TYPE}}) val;
} else { } else {
// Huh? // That's a big type...
return ({{TYPE}}) PyLong_AsUnsignedLongLong(x); return ({{TYPE}}) PyLong_AsUnsignedLongLong(x);
} }
} else { } else {
...@@ -543,25 +538,11 @@ static CYTHON_INLINE {{TYPE}} {{FROM_PY_FUNCTION}}(PyObject *x) { ...@@ -543,25 +538,11 @@ static CYTHON_INLINE {{TYPE}} {{FROM_PY_FUNCTION}}(PyObject *x) {
#endif #endif
#endif #endif
if (sizeof({{TYPE}}) <= sizeof(long)) { if (sizeof({{TYPE}}) <= sizeof(long)) {
long val = PyLong_AsLong(x); __PYX_VERIFY_RETURN_INT({{TYPE}}, long, PyLong_AsLong)
if (sizeof({{TYPE}}) < sizeof(long)
&& unlikely(val != ({{TYPE}}) val)) {
PyErr_SetString(PyExc_OverflowError,
"value too large to convert to {{TYPE}}");
return ({{TYPE}}) -1;
}
return ({{TYPE}}) val;
} else if (sizeof({{TYPE}}) <= sizeof(long long)) { } else if (sizeof({{TYPE}}) <= sizeof(long long)) {
long long val = PyLong_AsLongLong(x); __PYX_VERIFY_RETURN_INT({{TYPE}}, long long, PyLong_AsLongLong)
if (sizeof({{TYPE}}) < sizeof(long long)
&& unlikely(val != ({{TYPE}}) val)) {
PyErr_SetString(PyExc_OverflowError,
"value too large to convert to {{TYPE}}");
return ({{TYPE}}) -1;
}
return ({{TYPE}}) val;
} else { } else {
// Huh? // That's a big type...
return ({{TYPE}}) PyLong_AsLongLong(x); return ({{TYPE}}) PyLong_AsLongLong(x);
} }
} }
......
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