summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAllan Haldane <allan.haldane@gmail.com>2016-02-17 11:44:39 -0500
committerAllan Haldane <allan.haldane@gmail.com>2016-02-18 23:26:29 -0500
commit3fb847a93a6d16a8dd466f2545bc8b7d6ea1eb58 (patch)
tree3471a3d9a1fe93782920799a6d8a8f801abffa34
parent47b6c2b8bacb510cac62d490c159ec116080d1d0 (diff)
downloadnumpy-3fb847a93a6d16a8dd466f2545bc8b7d6ea1eb58.tar.gz
MAINT: Don't use PyInstance_Check to detect object type
That function does not exist in python3, causing differing behavior in python2 vs python3. This will slightly affect dtype detection when creating arrays from user-supplied object, in cases where an object array might be produced.
-rw-r--r--numpy/core/src/multiarray/common.c42
-rw-r--r--numpy/core/src/multiarray/ctors.c5
-rw-r--r--numpy/core/tests/test_dtype.py4
3 files changed, 7 insertions, 44 deletions
diff --git a/numpy/core/src/multiarray/common.c b/numpy/core/src/multiarray/common.c
index 1948b8b61..5db6b85bd 100644
--- a/numpy/core/src/multiarray/common.c
+++ b/numpy/core/src/multiarray/common.c
@@ -128,30 +128,6 @@ _array_find_python_scalar_type(PyObject *op)
return NULL;
}
-#if !defined(NPY_PY3K)
-static PyArray_Descr *
-_use_default_type(PyObject *op)
-{
- int typenum, l;
- PyObject *type;
-
- typenum = -1;
- l = 0;
- type = (PyObject *)Py_TYPE(op);
- while (l < NPY_NUMUSERTYPES) {
- if (type == (PyObject *)(userdescrs[l]->typeobj)) {
- typenum = l + NPY_USERDEF;
- break;
- }
- l++;
- }
- if (typenum == -1) {
- typenum = NPY_OBJECT;
- }
- return PyArray_DescrFromType(typenum);
-}
-#endif
-
/*
* These constants are used to signal that the recursive dtype determination in
* PyArray_DTypeFromObject encountered a string type, and that the recursive
@@ -490,19 +466,6 @@ PyArray_DTypeFromObjectHelper(PyObject *obj, int maxdims,
}
}
- /* Not exactly sure what this is about... */
-#if !defined(NPY_PY3K)
- if (PyInstance_Check(obj)) {
- dtype = _use_default_type(obj);
- if (dtype == NULL) {
- goto fail;
- }
- else {
- goto promote_types;
- }
- }
-#endif
-
/*
* If we reached the maximum recursion depth without hitting one
* of the above cases, the output dtype should be OBJECT
@@ -519,8 +482,9 @@ PyArray_DTypeFromObjectHelper(PyObject *obj, int maxdims,
}
/*
- * fails if convertable to list but no len is defined which some libraries
- * require to get object arrays
+ * If we get here, it may be a sequence. However some libraries define
+ * sequence-like classes but want them to be treated as objects, and they
+ * expect numpy to treat it as an object if __len__ is not defined.
*/
size = PySequence_Size(obj);
if (size < 0) {
diff --git a/numpy/core/src/multiarray/ctors.c b/numpy/core/src/multiarray/ctors.c
index 785b3073a..79c2b16b1 100644
--- a/numpy/core/src/multiarray/ctors.c
+++ b/numpy/core/src/multiarray/ctors.c
@@ -695,11 +695,6 @@ discover_dimensions(PyObject *obj, int *maxndim, npy_intp *d, int check_it,
/* obj is not a Sequence */
if (!PySequence_Check(obj) ||
-#if defined(NPY_PY3K)
- /* FIXME: XXX -- what is the correct thing to do here? */
-#else
- PyInstance_Check(obj) ||
-#endif
PySequence_Length(obj) < 0) {
*maxndim = 0;
PyErr_Clear();
diff --git a/numpy/core/tests/test_dtype.py b/numpy/core/tests/test_dtype.py
index 6d898eaa1..a6cb66b7d 100644
--- a/numpy/core/tests/test_dtype.py
+++ b/numpy/core/tests/test_dtype.py
@@ -590,6 +590,10 @@ def test_rational_dtype():
a = np.array([1111], dtype=rational).astype
assert_raises(OverflowError, a, 'int8')
+ # test that dtype detection finds user-defined types
+ x = rational(1)
+ assert_equal(np.array([x,x]).dtype, np.dtype(rational))
+
if __name__ == "__main__":
run_module_suite()