-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
BUG: Fix order='K' behavior for tobytes #16328
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
Conversation
2756323
to
9fbc4ba
Compare
numpy/core/src/multiarray/convert.c
Outdated
Py_DECREF(ravel_out); | ||
|
||
ret = PyBytes_FromStringAndSize(NULL, (Py_ssize_t) numbytes); | ||
if (ret == NULL) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure if we need this here just as a micro opt or if there is a deeper reason ?
numpy/core/src/multiarray/convert.c
Outdated
|| (PyArray_IS_F_CONTIGUOUS(self) && (order == NPY_FORTRANORDER))) { | ||
ret = PyBytes_FromStringAndSize(PyArray_DATA(self), (Py_ssize_t) numbytes); | ||
|
||
ravel_out = PyArray_Ravel(self, order); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would appreciate input on a better name here.
Sorry, I see I misjudged this a bit. The current algorithm uses an iterator while
Avoiding the second copy, can be done by either using the new iterator:
although that is a bit annoying, but if you would like to get to know that iterator API maybe not a bad exercise. |
Or maybe we just put the axis-reorganizing ravel does into an internal helper, so that we do not have to duplicate it. |
717c597
to
f2b6d3f
Compare
return NULL; | ||
} | ||
NPY_BEGIN_THREADS_DEF; | ||
needs_api = NpyIter_IterationNeedsAPI(iter); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i was a bit confused about the needs api stuff and the macros for NPY_BEGIN_THREADS_DEF , NPY_BEGIN_THREADS_NDITER etc. My understanding is that we just want to hold the GIL and I wasn't sure if we need any of this stuff.
f2b6d3f
to
6cdfcc9
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will give a wrong result as LHS = 2 whereas the RHS = 3.
When the order of 'x' will be in K, then the tobytes function should hold.
@@ -4714,6 +4714,10 @@ def test_empty_files_text(self): | |||
y = np.fromfile(self.filename, sep=" ") | |||
assert_(y.size == 0, "Array not empty") | |||
|
|||
def test_tobytes_order(self): | |||
x = np.array([[1, 2, 3], [4, 5, 6]], order='F') | |||
assert_(x.tobytes('F') == x.tobytes('K')) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will give a wrong result as LHS = 2 whereas the RHS = 3.
When the order of 'x' will be in K, then the tobytes function should hold.
The test looks correct to me. If it fails, the code needs fixing, not the test. |
@anirudh2290 Needs a rebase. Do you want to pursue this? |
Hi @charris, I won't be able to pursue this in the near future, feel free to close/reassign. |
Simplify PyArray_ToString to call PyArray_Ravel internally.
This addresses #16569 (see also: #16291 (comment))