gh-114685: PyBuffer_FillInfo() now raises on PyBUF_{READ,WRITE} (GH-114802)

This commit is contained in:
Nikita Sobolev 2024-02-04 22:16:43 +03:00 committed by GitHub
parent da8f9fb2ea
commit 929d44e15a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 56 additions and 5 deletions

View file

@ -4591,6 +4591,27 @@ class TestPythonBufferProtocol(unittest.TestCase):
self.assertRaises(SystemError, buf.__buffer__, PyBUF_READ)
self.assertRaises(SystemError, buf.__buffer__, PyBUF_WRITE)
@unittest.skipIf(_testcapi is None, "requires _testcapi")
def test_c_fill_buffer_invalid_flags(self):
# PyBuffer_FillInfo
source = b"abc"
self.assertRaises(SystemError, _testcapi.buffer_fill_info,
source, 0, PyBUF_READ)
self.assertRaises(SystemError, _testcapi.buffer_fill_info,
source, 0, PyBUF_WRITE)
@unittest.skipIf(_testcapi is None, "requires _testcapi")
def test_c_fill_buffer_readonly_and_writable(self):
source = b"abc"
with _testcapi.buffer_fill_info(source, 1, PyBUF_SIMPLE) as m:
self.assertEqual(bytes(m), b"abc")
self.assertTrue(m.readonly)
with _testcapi.buffer_fill_info(source, 0, PyBUF_WRITABLE) as m:
self.assertEqual(bytes(m), b"abc")
self.assertFalse(m.readonly)
self.assertRaises(BufferError, _testcapi.buffer_fill_info,
source, 1, PyBUF_WRITABLE)
def test_inheritance(self):
class A(bytearray):
def __buffer__(self, flags):