summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastian Berg <sebastian@sipsolutions.net>2013-06-07 16:19:03 +0200
committerSebastian Berg <sebastian@sipsolutions.net>2013-06-09 17:00:47 +0200
commite67269a5c14954e03d8afaa30a5fe4299f2fd062 (patch)
tree5b197a5836b652eb683469a0967349ac129fbcd1
parenta6a2c601c28687e8ff8b44f5319280b20a9c3f71 (diff)
downloadnumpy-e67269a5c14954e03d8afaa30a5fe4299f2fd062.tar.gz
MAINT: Minor style fix and integer conversion optimizations
-rw-r--r--numpy/core/src/multiarray/conversion_utils.c24
1 files changed, 12 insertions, 12 deletions
diff --git a/numpy/core/src/multiarray/conversion_utils.c b/numpy/core/src/multiarray/conversion_utils.c
index 267b502d9..a177e49dd 100644
--- a/numpy/core/src/multiarray/conversion_utils.c
+++ b/numpy/core/src/multiarray/conversion_utils.c
@@ -712,12 +712,13 @@ PyArray_PyIntAsIntp(PyObject *o)
*/
#if !defined(NPY_PY3K)
if PyInt_CheckExact(o) {
- #if (NPY_SIZEOF_LONG < NPY_SIZEOF_INTP)
- long_value = (npy_longlong) PyInt_AsLong(o);
+ #if (NPY_SIZEOF_LONG <= NPY_SIZEOF_INTP)
+ /* No overflow is possible, so we can just return */
+ return PyInt_AS_LONG(o);
#else
- long_value = PyInt_AsLong(o);
+ long_value = PyInt_AS_LONG(o);
+ goto overflow_check;
#endif
- goto finish;
}
else
#endif
@@ -727,7 +728,7 @@ PyArray_PyIntAsIntp(PyObject *o)
#else
long_value = PyLong_AsLong(o);
#endif
- goto finish;
+ return (npy_intp)long_value;
}
/*
* The most general case. PyNumber_Index(o) covers everything
@@ -797,17 +798,16 @@ PyArray_PyIntAsIntp(PyObject *o)
}
finish:
- if (long_value == -1) {
+ if (error_converting(long_value)) {
err = PyErr_Occurred();
/* Only replace TypeError's here, which are the normal errors. */
- if (err) {
- if (PyErr_GivenExceptionMatches(err, PyExc_TypeError)) {
- PyErr_SetString(PyExc_TypeError, msg);
- }
- return -1;
+ if (PyErr_GivenExceptionMatches(err, PyExc_TypeError)) {
+ PyErr_SetString(PyExc_TypeError, msg);
}
+ return -1;
}
+ overflow_check:
#if (NPY_SIZEOF_LONG < NPY_SIZEOF_INTP)
#if (NPY_SIZEOF_LONGLONG > NPY_SIZEOF_INTP)
if ((long_value < NPY_MIN_INTP) || (long_value > NPY_MAX_INTP)) {
@@ -825,7 +825,7 @@ PyArray_PyIntAsIntp(PyObject *o)
}
#endif
#endif
- return (npy_intp) long_value;
+ return long_value;
}