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

Code before:
}

/*NUMPY_API*/
NPY_NO_EXPORT PyObject *
PyArray_ToString(PyArrayObject *self, NPY_ORDER order)
{
    npy_intp numbytes;
    PyObject *ret;

    if (order == NPY_ANYORDER)
        order = PyArray_ISFORTRAN(self) ? NPY_FORTRANORDER : NPY_CORDER;

    /*        if (PyArray_TYPE(self) == NPY_OBJECT) {
              PyErr_SetString(PyExc_ValueError, "a string for the data" \
              "in an object array is not appropriate");
              return NULL;
              }
    */

    numbytes = PyArray_NBYTES(self);
    if ((PyArray_IS_C_CONTIGUOUS(self) && (order == NPY_CORDER))
        || (PyArray_IS_F_CONTIGUOUS(self) && (order == NPY_FORTRANORDER))) {
        return PyBytes_FromStringAndSize(PyArray_DATA(self), (Py_ssize_t) numbytes);
    }
    
    /* Avoid Ravel where possible for lesser copies. */
    if (!PyDataType_REFCHK(PyArray_DESCR(self)) && 
        ((PyArray_DESCR(self)->flags & NPY_NEEDS_INIT) == 0)) {
        
        /* Allocate final Byte Object */
        ret = PyBytes_FromStringAndSize(NULL, (Py_ssize_t) numbytes);
        if (ret == NULL) {
            return NULL;
        }
        
        /* Writable Buffer */
        char* dest = PyBytes_AS_STRING(ret);

        int flags = NPY_ARRAY_WRITEABLE;
        if (order == NPY_FORTRANORDER) {
            flags |= NPY_ARRAY_F_CONTIGUOUS;
        }

        Py_INCREF(PyArray_DESCR(self));
        /* Array view */
        PyArrayObject *dest_array = (PyArrayObject *)PyArray_NewFromDescr(
            &PyArray_Type,
            PyArray_DESCR(self),
            PyArray_NDIM(self),
            PyArray_DIMS(self),
            NULL, // strides

Review comment (suggestion):
```suggestion
    /* Avoid Ravel where possible for fewer copies. */
```

Code after:
}

/*NUMPY_API*/
NPY_NO_EXPORT PyObject *
PyArray_ToString(PyArrayObject *self, NPY_ORDER order)
{
    npy_intp numbytes;
    PyObject *ret;

    if (order == NPY_ANYORDER)
        order = PyArray_ISFORTRAN(self) ? NPY_FORTRANORDER : NPY_CORDER;

    /*        if (PyArray_TYPE(self) == NPY_OBJECT) {
              PyErr_SetString(PyExc_ValueError, "a string for the data" \
              "in an object array is not appropriate");
              return NULL;
              }
    */

    numbytes = PyArray_NBYTES(self);
    if ((PyArray_IS_C_CONTIGUOUS(self) && (order == NPY_CORDER))
        || (PyArray_IS_F_CONTIGUOUS(self) && (order == NPY_FORTRANORDER))) {
        return PyBytes_FromStringAndSize(PyArray_DATA(self), (Py_ssize_t) numbytes);
    }
    
    /* Avoid Ravel where possible for fewer copies. */
    if (!PyDataType_REFCHK(PyArray_DESCR(self)) && 
        ((PyArray_DESCR(self)->flags & NPY_NEEDS_INIT) == 0)) {
        
        /* Allocate final Bytes Object */
        ret = PyBytes_FromStringAndSize(NULL, (Py_ssize_t) numbytes);
        if (ret == NULL) {
            return NULL;
        }
        
        /* Writable Buffer */
        char* dest = PyBytes_AS_STRING(ret);

        int flags = NPY_ARRAY_WRITEABLE;
        if (order == NPY_FORTRANORDER) {
            flags |= NPY_ARRAY_F_CONTIGUOUS;
        }

        Py_INCREF(PyArray_DESCR(self));
        /* Array view */
        PyArrayObject *dest_array = (PyArrayObject *)PyArray_NewFromDescr(
            &PyArray_Type,
            PyArray_DESCR(self),
            PyArray_NDIM(self),
            PyArray_DIMS(self),
            NULL, // strides