mirror of https://github.com/python/cpython
gh-126980: Fix `bytearray.__buffer__` crash on `PyBUF_{READ,WRITE}` (#126981)
Co-authored-by: Victor Stinner <vstinner@python.org>
This commit is contained in:
parent
4d771977b1
commit
3932e1db53
|
@ -4439,6 +4439,14 @@ class TestBufferProtocol(unittest.TestCase):
|
||||||
x = ndarray([1,2,3], shape=[3], flags=ND_GETBUF_FAIL)
|
x = ndarray([1,2,3], shape=[3], flags=ND_GETBUF_FAIL)
|
||||||
self.assertRaises(BufferError, memoryview, x)
|
self.assertRaises(BufferError, memoryview, x)
|
||||||
|
|
||||||
|
def test_bytearray_release_buffer_read_flag(self):
|
||||||
|
# See https://github.com/python/cpython/issues/126980
|
||||||
|
obj = bytearray(b'abc')
|
||||||
|
with self.assertRaises(SystemError):
|
||||||
|
obj.__buffer__(inspect.BufferFlags.READ)
|
||||||
|
with self.assertRaises(SystemError):
|
||||||
|
obj.__buffer__(inspect.BufferFlags.WRITE)
|
||||||
|
|
||||||
@support.cpython_only
|
@support.cpython_only
|
||||||
def test_pybuffer_size_from_format(self):
|
def test_pybuffer_size_from_format(self):
|
||||||
# basic tests
|
# basic tests
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
Fix :meth:`~object.__buffer__` of :class:`bytearray` crashing when
|
||||||
|
:attr:`~inspect.BufferFlags.READ` or :attr:`~inspect.BufferFlags.WRITE` are
|
||||||
|
passed as flags.
|
|
@ -52,8 +52,9 @@ bytearray_getbuffer(PyObject *self, Py_buffer *view, int flags)
|
||||||
}
|
}
|
||||||
|
|
||||||
void *ptr = (void *) PyByteArray_AS_STRING(obj);
|
void *ptr = (void *) PyByteArray_AS_STRING(obj);
|
||||||
/* cannot fail if view != NULL and readonly == 0 */
|
if (PyBuffer_FillInfo(view, (PyObject*)obj, ptr, Py_SIZE(obj), 0, flags) < 0) {
|
||||||
(void)PyBuffer_FillInfo(view, (PyObject*)obj, ptr, Py_SIZE(obj), 0, flags);
|
return -1;
|
||||||
|
}
|
||||||
obj->ob_exports++;
|
obj->ob_exports++;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue