gh-114685: `PyBuffer_FillInfo` now raises on `PyBUF_{READ,WRITE}` by sobolevn · Pull Request #114802 · python/cpython · GitHub
Skip to content
Merged
Show file tree
Hide file tree
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
21 changes: 21 additions & 0 deletions Lib/test/test_buffer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:c:func:`PyBuffer_FillInfo` now raises a :exc:`SystemError` if called with
:c:macro:`PyBUF_READ` or :c:macro:`PyBUF_WRITE` as flags. These flags should
only be used with the ``PyMemoryView_*`` C API.
21 changes: 21 additions & 0 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1261,6 +1261,26 @@ make_memoryview_from_NULL_pointer(PyObject *self, PyObject *Py_UNUSED(ignored))
return PyMemoryView_FromBuffer(&info);
}

static PyObject *
buffer_fill_info(PyObject *self, PyObject *args)
{
Py_buffer info;
const char *data;
Py_ssize_t size;
int readonly;
int flags;

if (!PyArg_ParseTuple(args, "s#ii:buffer_fill_info",
&data, &size, &readonly, &flags)) {
return NULL;
}

if (PyBuffer_FillInfo(&info, NULL, (void *)data, size, readonly, flags) < 0) {
return NULL;
}
return PyMemoryView_FromBuffer(&info);
}

static PyObject *
test_from_contiguous(PyObject* self, PyObject *Py_UNUSED(ignored))
{
Expand Down Expand Up @@ -3314,6 +3334,7 @@ static PyMethodDef TestMethods[] = {
{"eval_code_ex", eval_eval_code_ex, METH_VARARGS},
{"make_memoryview_from_NULL_pointer", make_memoryview_from_NULL_pointer,
METH_NOARGS},
{"buffer_fill_info", buffer_fill_info, METH_VARARGS},
{"crash_no_current_thread", crash_no_current_thread, METH_NOARGS},
{"test_current_tstate_matches", test_current_tstate_matches, METH_NOARGS},
{"run_in_subinterp", run_in_subinterp, METH_VARARGS},
Expand Down
16 changes: 11 additions & 5 deletions Objects/abstract.c