8000 BUG: Fix order='K' behavior for tobytes by shubham11941140 · Pull Request #19718 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: Fix order='K' behavior for tobytes #19718

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 47 additions & 25 deletions numpy/core/src/multiarray/convert.c
Original file line number Diff line number Diff line change
Expand Up @@ -296,11 +296,17 @@ NPY_NO_EXPORT PyObject *
PyArray_ToString(PyArrayObject *self, NPY_ORDER order)
{
npy_intp numbytes;
npy_intp i;
char *dptr;
int elsize;
PyObject *ret;
PyArrayIterObject *it;

int needs_api;
NpyIter *iter;
NpyIter_IterNextFunc *iternext;
char **dataptr;
npy_intp *strideptr, *innersizeptr;
char *data;
npy_intp stride, count;

if (order == NPY_ANYORDER)
order = PyArray_ISFORTRAN(self);
Expand All @@ -318,37 +324,53 @@ PyArray_ToString(PyArrayObject *self, NPY_ORDER order)
ret = PyBytes_FromStringAndSize(PyArray_DATA(self), (Py_ssize_t) numbytes);
}
else {
PyObject *new;
if (order == NPY_FORTRANORDER) {
/* iterators are always in C-order */
new = PyArray_Transpose(self, NULL);
if (new == NULL) {
return NULL;
}
}
else {
Py_INCREF(self);
new = (PyObject *)self;
}
it = (PyArrayIterObject *)PyArray_IterNew(new);
Py_DECREF(new);
if (it == NULL) {
iter = NpyIter_New(self, NPY_ITER_READONLY |
NPY_ITER_GROWINNER |
NPY_ITER_EXTERNAL_LOOP |
NPY_ITER_DONT_NEGATE_STRIDES,
order, NPY_NO_CASTING, NULL);
if (iter == NULL) {
return NULL;
}
NPY_BEGIN_THREADS_DEF;
needs_api = NpyIter_IterationNeedsAPI(iter);

ret = PyBytes_FromStringAndSize(NULL, (Py_ssize_t) numbytes);
if (ret == NULL) {
Py_DECREF(it);
NpyIter_Deallocate(iter);
return NULL;
}


iternext = NpyIter_GetIterNext(iter, NULL);
if (iternext == NULL) {
NpyIter_Deallocate(iter);
return NULL;
}

NPY_BEGIN_THREADS_NDITER(iter);

dataptr = NpyIter_GetDataPtrArray(iter);
strideptr = NpyIter_GetInnerStrideArray(iter);
innersizeptr = NpyIter_GetInnerLoopSizePtr(iter);
dptr = PyBytes_AS_STRING(ret);
i = it->size;
elsize = PyArray_DESCR(self)->elsize;
while (i--) {
memcpy(dptr, it->dataptr, elsize);
dptr += elsize;
PyArray_ITER_NEXT(it);
}
Py_DECREF(it);
do {
data = *dataptr;
stride = *strideptr;
count = *innersizeptr;
while (count--) {
memcpy(dptr, data, elsize);
if (needs_api && PyErr_Occurred()) {
goto finish;
}
data += stride;
dptr += elsize;
}
} while(iternext(iter));
finish:
NPY_END_THREADS;
NpyIter_Deallocate(iter);
}
return ret;
}
Expand Down
0