Skip to content

Commit b7520f1

Browse files
committed
pythongh-126980: Fix bytearray.__buffer__ crash on PyBUF_{READ,WRITE}
1 parent b0fcc2c commit b7520f1

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

Lib/test/test_buffer.py

+7
Original file line numberDiff line numberDiff line change
@@ -4439,6 +4439,13 @@ def test_issue_7385(self):
44394439
x = ndarray([1,2,3], shape=[3], flags=ND_GETBUF_FAIL)
44404440
self.assertRaises(BufferError, memoryview, x)
44414441

4442+
def test_bytearray_release_buffer_read_flag(self):
4443+
# See https://github.com/python/cpython/issues/126980
4444+
with self.assertRaises(SystemError):
4445+
bytearray().__buffer__(inspect.BufferFlags.READ)
4446+
with self.assertRaises(SystemError):
4447+
bytearray().__buffer__(inspect.BufferFlags.WRITE)
4448+
44424449
@support.cpython_only
44434450
def test_pybuffer_size_from_format(self):
44444451
# basic tests
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix :meth:`~object.__buffer__` of :class:`bytearray` crashing when
2+
:attr:`~inspect.BufferFlags.READ` or :attr:`~inspect.BufferFlags.WRITE` are
3+
passed as flags.

Objects/bytearrayobject.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ bytearray_getbuffer(PyObject *self, Py_buffer *view, int flags)
5252
}
5353

5454
void *ptr = (void *) PyByteArray_AS_STRING(obj);
55-
/* cannot fail if view != NULL and readonly == 0 */
56-
(void)PyBuffer_FillInfo(view, (PyObject*)obj, ptr, Py_SIZE(obj), 0, flags);
55+
if (PyBuffer_FillInfo(view, (PyObject*)obj, ptr, Py_SIZE(obj), 0, flags) < 0) {
56+
return -1;
57+
}
5758
obj->ob_exports++;
5859
return 0;
5960
}

0 commit comments

Comments
 (0)