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

Code before:
            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 {
        Py_ssize_t shape_tuple_len = PyTuple_GET_SIZE(attr);
        if (shape_tuple_len > NPY_MAXDIMS) {
            PyErr_Format(PyExc_ValueError,
                         "number of dimensions must be within [0, %d], got %d",
                         NPY_MAXDIMS, shape_tuple_len);
            goto fail;
        }
        for (i = 0; i < shape_tuple_len; i++) {
            PyObject *tmp = PyTuple_GET_ITEM(attr, i);
            dims[i] = PyArray_PyIntAsIntp(tmp);
            if (error_converting(dims[i])) {
                goto fail;
            }
        }
        n = (int)shape_tuple_len;
    }
    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)");
            goto fail;
        }
        dataptr = PyTuple_GET_ITEM(attr, 0);
        if (PyLong_Check(dataptr)) {
            data = PyLong_AsVoidPtr(dataptr);
            if (data == NULL && PyErr_Occurred()) {
                goto fail;
            }
        }

Review comment (suggestion):
Maybe just move it up?  Or even just make `n` and `i` `Py_ssize_t` to begin with.

Code after:
            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)");
            goto fail;
        }
        dataptr = PyTuple_GET_ITEM(attr, 0);
        if (PyLong_Check(dataptr)) {
            data = PyLong_AsVoidPtr(dataptr);
            if (data == NULL && PyErr_Occurred()) {
                goto fail;
            }
        }
        else {