summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastian Berg <sebastian@sipsolutions.net>2013-06-17 15:08:00 +0200
committerSebastian Berg <sebastian@sipsolutions.net>2013-06-17 18:02:31 +0200
commitea0da13308da78928fb99eaa0876626da1e8e9ac (patch)
treedc1cb507ee3d4011ed00544a243e1e4398aff098
parent8f547d3a42d12e8d7ac993eef37bc3088de60688 (diff)
downloadnumpy-ea0da13308da78928fb99eaa0876626da1e8e9ac.tar.gz
BUG: Fix failing python long behaviour and possible heisen bug
The priority error fixes np.float_(64) + python_long not working anymore. The second change reformats the PyArray_GetAttrString_SuppressException to avoid a possible heisenbug decrefing an object before use.
-rw-r--r--numpy/core/src/multiarray/common.c46
-rw-r--r--numpy/core/src/multiarray/multiarraymodule.c12
-rw-r--r--numpy/core/tests/test_regression.py5
3 files changed, 35 insertions, 28 deletions
diff --git a/numpy/core/src/multiarray/common.c b/numpy/core/src/multiarray/common.c
index 4787c6482..c1b6ef5b3 100644
--- a/numpy/core/src/multiarray/common.c
+++ b/numpy/core/src/multiarray/common.c
@@ -49,31 +49,35 @@ PyArray_GetAttrString_SuppressException(PyObject *obj, char *name)
{
PyTypeObject *tp = Py_TYPE(obj);
PyObject *res = (PyObject *)NULL;
- if (/* Is not trivial type */
- obj != Py_None &&
- !PyList_CheckExact(obj) &&
- !PyTuple_CheckExact(obj)) {
- /* Attribute referenced by (char *)name */
- if (tp->tp_getattr != NULL) {
- res = (*tp->tp_getattr)(obj, name);
- if (res == NULL) {
- PyErr_Clear();
- }
+
+ /* We do not need to check for special attributes on trivial types */
+ if (obj == Py_None ||
+ PyList_CheckExact(obj) ||
+ PyTuple_CheckExact(obj)) {
+ return NULL;
+ }
+
+ /* Attribute referenced by (char *)name */
+ if (tp->tp_getattr != NULL) {
+ res = (*tp->tp_getattr)(obj, name);
+ if (res == NULL) {
+ PyErr_Clear();
}
- /* Attribute referenced by (PyObject *)name */
- else if (tp->tp_getattro != NULL) {
+ }
+ /* Attribute referenced by (PyObject *)name */
+ else if (tp->tp_getattro != NULL) {
#if defined(NPY_PY3K)
- PyObject *w = PyUnicode_InternFromString(name);
+ PyObject *w = PyUnicode_InternFromString(name);
#else
- PyObject *w = PyString_InternFromString(name);
+ PyObject *w = PyString_InternFromString(name);
#endif
- if (w == NULL)
- return (PyObject *)NULL;
- Py_DECREF(w);
- res = (*tp->tp_getattro)(obj, w);
- if (res == NULL) {
- PyErr_Clear();
- }
+ if (w == NULL) {
+ return (PyObject *)NULL;
+ }
+ res = (*tp->tp_getattro)(obj, w);
+ Py_DECREF(w);
+ if (res == NULL) {
+ PyErr_Clear();
}
}
return res;
diff --git a/numpy/core/src/multiarray/multiarraymodule.c b/numpy/core/src/multiarray/multiarraymodule.c
index 426ab695c..92bf93933 100644
--- a/numpy/core/src/multiarray/multiarraymodule.c
+++ b/numpy/core/src/multiarray/multiarraymodule.c
@@ -70,14 +70,12 @@ PyArray_GetPriority(PyObject *obj, double default_)
return priority;
ret = PyArray_GetAttrString_SuppressException(obj, "__array_priority__");
- if (ret != NULL) {
- priority = PyFloat_AsDouble(ret);
- }
- if (PyErr_Occurred()) {
- PyErr_Clear();
- priority = default_;
+ if (ret == NULL) {
+ return default_;
}
- Py_XDECREF(ret);
+
+ priority = PyFloat_AsDouble(ret);
+ Py_DECREF(ret);
return priority;
}
diff --git a/numpy/core/tests/test_regression.py b/numpy/core/tests/test_regression.py
index fcbb75ba2..6dfee51d8 100644
--- a/numpy/core/tests/test_regression.py
+++ b/numpy/core/tests/test_regression.py
@@ -263,6 +263,11 @@ class TestRegression(TestCase):
"""Ticket #143"""
assert_equal(0,np.add.identity)
+ def test_numpy_float_python_long_addition(self):
+ # Check that numpy float and python longs can be added correctly.
+ a = np.float_(23.) + 2**135
+ assert_equal(a, 23. + 2**135)
+
def test_binary_repr_0(self,level=rlevel):
"""Ticket #151"""
assert_equal('0',np.binary_repr(0))