Repository: numpy/numpy
File: numpy/_core/src/multiarray/ctors.c (C)

Code before:
        else if (result == 1) {
            Py_DECREF(iface);
            Py_DECREF(attr);
            PyErr_SetString(PyExc_ValueError,
                    "Missing __array_interface__ shape");
            return NULL;
        }
        /* Assume shape as scalar otherwise */
        else {
            /* NOTE: pointers to data and base should be NULL */
            n = dims[0] = 0;
        }
    }
    /* Make sure 'shape' is a tuple */
    else if (!PyTuple_Check(attr)) {
        PyErr_SetString(PyExc_TypeError,
                "shape must be a tuple");
        goto fail;
    }
    /* Get dimensions from shape tuple */
    else {
        n = PyTuple_GET_SIZE(attr);
        if (n > NPY_MAXDIMS) {
            PyErr_Format( PyExc_ValueError,
                          "number of dimensions must be within [0, %d], got %d",
                          NPY_MAXDIMS, n );
            goto fail;
        }
        for (i = 0; i < n; i++) {
            PyObject *tmp = PyTuple_GET_ITEM(attr, i);
            dims[i] = PyArray_PyIntAsIntp(tmp);
            if (error_converting(dims[i])) {
                goto fail;
            }
        }
    }
    Py_CLEAR(attr);

    /* Get data buffer from interface specification */
    result = PyDict_GetItemStringRef(iface, "data", &attr);
    if (result == -1){
        return NULL;
    }

    /* Case for data access through pointer */
    if (attr && PyTuple_Check(attr)) {
        PyObject *dataptr;
        if (PyTuple_GET_SIZE(attr) != 2) {
            PyErr_SetString(PyExc_TypeError,
                    "__array_interface__ data must be a 2-tuple with "
                    "(data pointer integer, read-only flag)");

Review comment (bug):
```suggestion
            PyErr_Format(PyExc_ValueError,
                         "number of dimensions must be within [0, %d], got %d",
                         NPY_MAXDIMS, n);
```
Just a nitpick but we never put spaces there.

There is actually another small bug (hard to test here).  `n` should be a `Py_ssize_t`.  I am honestly always surprised that we don't have compiler warnings for an implicit unsafe cast here...

Code after:
        else if (result == 1) {
            Py_DECREF(iface);
            Py_DECREF(attr);
            PyErr_SetString(PyExc_ValueError,
                    "Missing __array_interface__ shape");
            return NULL;
        }
        /* Assume shape as scalar otherwise */
        else {
            /* NOTE: pointers to data and base should be NULL */
            n = dims[0] = 0;
        }
    }
    /* Make sure 'shape' is a tuple */
    else if (!PyTuple_Check(attr)) {
        PyErr_SetString(PyExc_TypeError,
                "shape must be a tuple");
        goto fail;
    }
    /* Get dimensions from shape tuple */
    else {
        n = PyTuple_GET_SIZE(attr);
        if (n > NPY_MAXDIMS) {
            PyErr_Format(PyExc_ValueError,
                         "number of dimensions must be within [0, %d], got %d",
                         NPY_MAXDIMS, n);
            goto fail;
        }
        for (i = 0; i < n; i++) {
            PyObject *tmp = PyTuple_GET_ITEM(attr, i);
            dims[i] = PyArray_PyIntAsIntp(tmp);
            if (error_converting(dims[i])) {
                goto fail;
            }
        }
    }
    Py_CLEAR(attr);

    /* Get data buffer from interface specification */
    result = PyDict_GetItemStringRef(iface, "data", &attr);
    if (result == -1){
        return NULL;
    }

    /* Case for data access through pointer */
    if (attr && PyTuple_Check(attr)) {
        PyObject *dataptr;
        if (PyTuple_GET_SIZE(attr) != 2) {
            PyErr_SetString(PyExc_TypeError,
                    "__array_interface__ data must be a 2-tuple with "
                    "(data pointer integer, read-only flag)");