-
-
Notifications
You must be signed in to change notification settings - Fork 30k
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
PyObject_GetBuffer(PyBUF_FORMAT) breaks on a memoryview #123609
Comments
The docs state that However, it looks like we do support standalone import _testbuffer
_testbuffer.ndarray(b"no good!", getbuf=_testbuffer.PyBUF_FORMAT) Maybe it's worth trying to explicitly disallow those? (@encukou, this needs |
Why would I know the format ahead of time? What I am trying to do is to introspect the detail of the buffer protocol implemented by an object (I've already called |
If the requested buffer was
|
Two points...
|
You do that with
Oh, I think you misinterpreted what I meant by that. I meant, by requesting I'm guessing you want to do something like the following: static PyObject *
print_buffer_format(PyObject *Py_UNUSED(self), PyObject *obj)
{
PyObject *obj;
// technically, you should be using y* to get the buffer, but I'm just making an example
if (!PyArg_ParseTuple("O", &obj))
return NULL;
Py_buffer buf;
if (PyObject_GetBuffer(obj, &buf, PyBUF_FORMAT) < 0)
return NULL;
printf("Format string: %s\n", buf.format);
PyBuffer_Release(&buf);
Py_RETURN_NONE;
} Again, using if (PyObject_GetBuffer(obj, &buf, PyBUF_SIMPLE) < 0)
return NULL;
puts("Format string: B");
PyBuffer_Release(&buf); |
I've now read the PEP and have a much better understanding of what It seems that there is no mechanism for making general queries about the buffer capabilities of an object. The nearest you can get is to make repeated calls to I guess my problem was that I misunderstood the docs. This statement from the PEP really helped : |
It does, just not directly. They say not to use I think, for consistency, we should also disallow this in |
@encukou, I think this should get closed. Disallowing |
I think a little note like this could make things clearer:
|
Sure, I'll submit a PR. Would it be worth mentioning that |
I've created #123778 to fix the docs. @philthompson10, does this look clearer to you? |
No. CPython doesn't check that you're not playing by the rules, but that doesn't mean you should do it. (Even though we don't want to add the check and break people who didn't study the docs carefully.) Please keep the message short, that way more people will read it :) |
Yeah, I mentioned this in the PR by using |
User-facing docs shouldn't describe the implementation (especially if it's slightly buggy), but how users should use it. |
@ZeroIntensity sorry, no, the docs change doesn't really make things clearer and (strictly speaking) isn't true. As the value of |
|
Bug report
Bug description:
Calling
PyObject_GetBuffer()
with thePyBUF_FORMAT
flag on a memoryview object that wraps a bytes object fails with the exception:BufferError: memoryview: cannot cast to unsigned bytes if the format flag is present
Calling it directly on the same bytes object does not fail.
The test is in
memory_getbuf()
in memoryobject.c. The test is failing becauseformat
is notNULL
.format
is actually"B"
which is the equivalent ofNULL
. I think the test should be changed to allow aformat
value of"B"
.An alternative might be to change
PyBuffer_FillInfo()
to always setformat
toNULL
.CPython versions tested on:
3.12, 3.13
Operating systems tested on:
Linux, macOS
Linked PRs
PyBUF_FORMAT
#123778PyBUF_FORMAT
(GH-123778) #123903PyBUF_FORMAT
(GH-123778) #123904The text was updated successfully, but these errors were encountered: