-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
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
BUG: Buffer protocol doesn't support read-write access for numpy records #23973
Comments
We can probably change this, although I don't like that our scalars are mutable this way; it also doesn't really make much sense to act differently in different places. (I suspect there may be ways to create structured scalars which do not allow to write, but I would have to search a bit.) So, I am a bit curious about you needing to mutate the scalars (although, NumPy turning 0-D arrays to scalars might be enough of a reason). I.e. the fact that we return a read-only version for the buffer protocol was maybe even intentional to some degree. |
If I recall correctly there were issues around correctly handling the buffer protocol and padding (if needed). |
My use case is that I am creating a numpy record that looks like a C structure, and then passing that numpy record to C code. I need to find out the actual address of the numpy record in my SWIG wrapper to give to C. Slightly longer history. All my numpy records are currently created as the sole element of a numpy array:
and in this case, I can get from a numpy record to its actual address by means of base:
However I will soon be needing to handle arrays of records, and the above trick will only work with the 0-th element. I can correct this by using the C side of the array_interface protocol:
This works fine, and it correctly reports that the data is read-write. But the documentation says this protocol is "legacy", and that new code should use the buffer protocol. Hence my surprising discovery that:
fails. If I lie to it and use Until this is fixed (or declined), can I ask for advice? Am I better off using the legacy array_interface or lying to the buffer protocol? Both feel dangerous in different ways. |
Does using this instead work? record = np.zeros((), dtype="i,i,i,i,d,d,d"). # my actual descriptor is more complicated This gives you an array rather than a scalar, which should be read-write. |
The numpy record is used a lot on both the C side and on the Python side. The bigger project is a Python wrapper around a NASA toolkit. It would be tricky explaining to users (most of them are astronomers, not programmers) that they have to type But in general, there ought to be a natural way of modifying a record in C, shouldn't there? [* Yes, I'm actually using rec.array. But that detail was irrelevant to the original bug report.] |
Just double checking. Until this bug is resolved one way or the other, is there a recommended way to get the address of a record? Is there any intent to deprecate array_interface? |
The fact that accessing a single element Also, the documentation for
but actually it gives a memoryview. |
Describe the issue:
Numpy does not allow you to use the buffer-protocol to get a read-write buffer for numpy record. It requires you to ask for the record read-only. This is in spite of the fact that the pointer returned using the buffer protocol does, in fact, point to a read-write piece of memory and one ought to be able to modify it.
Using the buffer protocol is the recommended way to read and write "array-like objects" in C. To use the buffer protocol for a numpy record, you have to lie and request a read-only buffer, and then write to it anyway. The older array_interface works just fine.
Reproduce the code example:
Error message:
No response
Runtime information:
numpy.version
'1.25.0'
Context for the issue:
I should be able to use the buffer protocol to read and write a numpy record without lying about my intentions to write to it.
The text was updated successfully, but these errors were encountered: