In my experience writing ndarray-like objects, you likely want to implement __array__ instead of __array_interface__. The former gives you full control to create the ndarray yourself.

On Fri, Mar 13, 2015 at 7:22 AM, Daniel Smith <dgasmith@icloud.com> wrote:
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
_______________________________________________
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion