Skip to content

Commit 7e480af

Browse files
committed
gh-114685: PyBuffer_FillInfo now raises on PyBUF_{READ,WRITE}
1 parent 1c2ea8b commit 7e480af

File tree

4 files changed

+51
-5
lines changed

4 files changed

+51
-5
lines changed

Lib/test/test_buffer.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4591,6 +4591,23 @@ def test_c_buffer_invalid_flags(self):
45914591
self.assertRaises(SystemError, buf.__buffer__, PyBUF_READ)
45924592
self.assertRaises(SystemError, buf.__buffer__, PyBUF_WRITE)
45934593

4594+
@unittest.skipIf(_testcapi is None, "requires _testcapi")
4595+
def test_c_fill_buffer_invalid_flags(self):
4596+
# PyBuffer_FillInfo
4597+
self.assertRaises(SystemError, _testcapi.buffer_fill_info,
4598+
"abc", 0, PyBUF_READ)
4599+
self.assertRaises(SystemError, _testcapi.buffer_fill_info,
4600+
"abc", 0, PyBUF_WRITE)
4601+
4602+
@unittest.skipIf(_testcapi is None, "requires _testcapi")
4603+
def test_c_fill_buffer_readonly_and_writable(self):
4604+
self.assertEqual(
4605+
str(_testcapi.buffer_fill_info("abc", 1, PyBUF_SIMPLE), "utf8"),
4606+
"abc",
4607+
)
4608+
self.assertRaises(BufferError, _testcapi.buffer_fill_info,
4609+
"abc", 1, PyBUF_WRITABLE)
4610+
45944611
def test_inheritance(self):
45954612
class A(bytearray):
45964613
def __buffer__(self, flags):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:c:func:`PyBuffer_FillInfo` now raises a :exc:`SystemError` if called with
2+
:c:macro:`PyBUF_READ` or :c:macro:`PyBUF_WRITE` as flags. These flags should
3+
only be used with the ``PyMemoryView_*`` C API.

Modules/_testcapimodule.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,6 +1261,25 @@ make_memoryview_from_NULL_pointer(PyObject *self, PyObject *Py_UNUSED(ignored))
12611261
return PyMemoryView_FromBuffer(&info);
12621262
}
12631263

1264+
static PyObject *
1265+
buffer_fill_info(PyObject *self, PyObject *args)
1266+
{
1267+
Py_buffer info;
1268+
const char *data;
1269+
Py_ssize_t size;
1270+
int readonly;
1271+
int flags;
1272+
1273+
if (!PyArg_ParseTuple(args, "s#ii:buffer_fill_info",
1274+
&data, &size, &readonly, &flags)) {
1275+
return NULL;
1276+
}
1277+
1278+
if (PyBuffer_FillInfo(&info, NULL, (void *)data, size, readonly, flags) < 0)
1279+
return NULL;
1280+
return PyMemoryView_FromBuffer(&info);
1281+
}
1282+
12641283
static PyObject *
12651284
test_from_contiguous(PyObject* self, PyObject *Py_UNUSED(ignored))
12661285
{
@@ -3314,6 +3333,7 @@ static PyMethodDef TestMethods[] = {
33143333
{"eval_code_ex", eval_eval_code_ex, METH_VARARGS},
33153334
{"make_memoryview_from_NULL_pointer", make_memoryview_from_NULL_pointer,
33163335
METH_NOARGS},
3336+
{"buffer_fill_info", buffer_fill_info, METH_VARARGS},
33173337
{"crash_no_current_thread", crash_no_current_thread, METH_NOARGS},
33183338
{"test_current_tstate_matches", test_current_tstate_matches, METH_NOARGS},
33193339
{"run_in_subinterp", run_in_subinterp, METH_VARARGS},

Objects/abstract.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -767,11 +767,17 @@ PyBuffer_FillInfo(Py_buffer *view, PyObject *obj, void *buf, Py_ssize_t len,
767767
return -1;
768768
}
769769

770-
if (((flags & PyBUF_WRITABLE) == PyBUF_WRITABLE) &&
771-
(readonly == 1)) {
772-
PyErr_SetString(PyExc_BufferError,
773-
"Object is not writable.");
774-
return -1;
770+
if (flags != PyBUF_SIMPLE) { /* fast path */
771+
if (flags == PyBUF_READ || flags == PyBUF_WRITE) {
772+
PyErr_BadInternalCall();
773+
return -1;
774+
}
775+
if (((flags & PyBUF_WRITABLE) == PyBUF_WRITABLE) &&
776+
(readonly == 1)) {
777+
PyErr_SetString(PyExc_BufferError,
778+
"Object is not writable.");
779+
return -1;
780+
}
775781
}
776782

777783
view->obj = Py_XNewRef(obj);

0 commit comments

Comments
 (0)