[Numpy-discussion] Custom __array_interface__ error

Daniel Smith dgasmith at icloud.com
Fri Mar 13 10:22:14 EDT 2015


Greetings everyone,
I have a new project that deals with core and disk tensors wrapped into a single object so that the expressions are transparent to the user after the tensor is formed. I would like to add __array_interface__ to the core tensor and provide a reasonable error message if someone tries to call the __array_interface__ for a disk tensor. I may be missing something, but I do not see an obvious way to do this in the python layer.

Currently I do something like:

            if ttype == “Core":
                self.__array_interface__ = self.tensor.ndarray_interface()
            else:
                self.__array_interface__ = {'typestr’: 'Only Core tensor types are supported.’}

Which provides at least a readable error message if it is not a core tensor:
TypeError: data type "Only Core tensor types are supported." not understood

A easy solution I see is to change numpy C side __array_interface__ error message to throw custom strings.

In numpy/core/src/multiarray/ctors.c:2100 we have the __array_interface__ conversion:

    if (!PyDict_Check(iface)) {
        Py_DECREF(iface);
        PyErr_SetString(PyExc_ValueError,
                "Invalid __array_interface__ value, must be a dict");
        return NULL;
    }

It could simply be changed to:

    if (!PyDict_Check(iface)) {
	if (PyString_Check(iface)){
            PyErr_SetString(PyExc_ValueError, iface);
        }
        else{
            PyErr_SetString(PyExc_ValueError,
                    "Invalid __array_interface__ value, must be a dict”);
        }
        Py_DECREF(iface);
        return NULL;
    }

Thoughts?

Cheers,
-Daniel Smith


More information about the NumPy-Discussion mailing list