Skip to content

BUG: Fix dot and inner type/value exception failures #6988

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions numpy/core/src/multiarray/ctors.c
Original file line number Diff line number Diff line change
Expand Up @@ -1926,14 +1926,34 @@ PyArray_FromArray(PyArrayObject *arr, PyArray_Descr *newtype, int flags)
/* Raise an error if the casting rule isn't followed */
if (!PyArray_CanCastArrayTo(arr, newtype, casting)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After some more debugging with gdb apparently the segmentation fault is occurring at this line. The real line responsible is in PyArray_EquivTypes. Somehow PyArray_EquivTypes is getting NULL for its first type, which is the cause of the problem.

PyObject *errmsg;
PyArray_Descr *arr_descr = NULL;
PyObject *arr_descr_repr = NULL;
PyObject *newtype_repr = NULL;

PyErr_Clear();
errmsg = PyUString_FromString("Cannot cast array data from ");
PyUString_ConcatAndDel(&errmsg,
PyObject_Repr((PyObject *)PyArray_DESCR(arr)));
arr_descr = PyArray_DESCR(arr);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think calling Py_DECREF(arr_descr); may have been the cause of the segmentation faults. At the time, I didn't realize PyArray_DESCR returned a borrowed reference. So, I was effectively deleting part of the array, but leaving the rest of the array around. As a result, we saw some weird behavior due to this.

if (arr_descr == NULL) {
Py_DECREF(newtype);
Py_DECREF(errmsg);
return NULL;
}
arr_descr_repr = PyObject_Repr((PyObject *)arr_descr);
if (arr_descr_repr == NULL) {
Py_DECREF(newtype);
Py_DECREF(errmsg);
return NULL;
}
PyUString_ConcatAndDel(&errmsg, arr_descr_repr);
PyUString_ConcatAndDel(&errmsg,
PyUString_FromString(" to "));
PyUString_ConcatAndDel(&errmsg,
PyObject_Repr((PyObject *)newtype));
newtype_repr = PyObject_Repr((PyObject *)newtype);
if (newtype_repr == NULL) {
Py_DECREF(newtype);
Py_DECREF(errmsg);
return NULL;
}
PyUString_ConcatAndDel(&errmsg, newtype_repr);
PyUString_ConcatAndDel(&errmsg,
PyUString_FromFormat(" according to the rule %s",
npy_casting_to_string(casting)));
Expand Down
14 changes: 14 additions & 0 deletions numpy/core/tests/test_multiarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -2041,6 +2041,13 @@ def __numpy_ufunc__(self, ufunc, method, pos, inputs, **kwargs):
assert_raises(TypeError, np.dot, b, c)
assert_raises(TypeError, c.dot, b)

def test_dot_type_mismatch(self):
c = 1.
A = np.array((1,1), dtype='i,i')

assert_raises(ValueError, np.dot, c, A)
assert_raises(TypeError, np.dot, A, c)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, I managed to get this built so I can debug with python3-dbg interactively and discovered this line will cause the strange exception. However, the line before does not cause the exception.


def test_diagonal(self):
a = np.arange(12).reshape((3, 4))
assert_equal(a.diagonal(), [0, 5, 10])
Expand Down Expand Up @@ -4825,6 +4832,13 @@ def test_matmul_inplace():

class TestInner(TestCase):

def test_inner_type_mismatch(self):
c = 1.
A = np.array((1,1), dtype='i,i')

assert_raises(TypeError, np.inner, c, A)
assert_raises(TypeError, np.inner, A, c)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same story as above. This line will cause the exception, as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to add that this error occurs with python 3.5 debug also.


def test_inner_scalar_and_vector(self):
for dt in np.typecodes['AllInteger'] + np.typecodes['AllFloat'] + '?':
sca = np.array(3, dtype=dt)[()]
Expand Down