Skip to content

Commit 88ce24d

Browse files
committed
Add a test
1 parent 9fcb214 commit 88ce24d

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

Lib/test/test_buffer.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4604,6 +4604,36 @@ def __release_buffer__(self, view):
46044604
self.assertEqual(rb_call_count, 0)
46054605
self.assertEqual(rb_call_count, 1)
46064606

4607+
def test_inherit_but_return_something_else(self):
4608+
class A(bytearray):
4609+
def __buffer__(self, flags):
4610+
return memoryview(b"hello")
4611+
4612+
a = A(b"hello")
4613+
with memoryview(a) as mv:
4614+
self.assertEqual(mv.tobytes(), b"hello")
4615+
4616+
rb_call_count = 0
4617+
rb_raised = False
4618+
class B(bytearray):
4619+
def __buffer__(self, flags):
4620+
return memoryview(b"hello")
4621+
def __release_buffer__(self, view):
4622+
nonlocal rb_call_count
4623+
rb_call_count += 1
4624+
try:
4625+
super().__release_buffer__(view)
4626+
except ValueError:
4627+
nonlocal rb_raised
4628+
rb_raised = True
4629+
4630+
b = B(b"hello")
4631+
with memoryview(b) as mv:
4632+
self.assertEqual(mv.tobytes(), b"hello")
4633+
self.assertEqual(rb_call_count, 0)
4634+
self.assertEqual(rb_call_count, 1)
4635+
self.assertIs(rb_raised, True)
4636+
46074637

46084638
if __name__ == "__main__":
46094639
unittest.main()

0 commit comments

Comments
 (0)