-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
gh-104223: Fix issues with inheriting from buffer classes #104227
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 8 commits
b7fb7a4
c717784
9fcb214
88ce24d
80a4b0b
6e31664
b73e29c
15ad02d
9ded873
3b50138
056efee
9ee8eb5
5536853
02d7b9c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4579,6 +4579,117 @@ def test_c_buffer(self): | |
buf.__release_buffer__(mv) | ||
self.assertEqual(buf.references, 0) | ||
|
||
def test_inheritance(self): | ||
class A(bytearray): | ||
def __buffer__(self, flags): | ||
return super().__buffer__(flags) | ||
|
||
a = A(b"hello") | ||
mv = memoryview(a) | ||
self.assertEqual(mv.tobytes(), b"hello") | ||
|
||
def test_inheritance_releasebuffer(self): | ||
rb_call_count = 0 | ||
class B(bytearray): | ||
def __buffer__(self, flags): | ||
return super().__buffer__(flags) | ||
def __release_buffer__(self, view): | ||
nonlocal rb_call_count | ||
rb_call_count += 1 | ||
super().__release_buffer__(view) | ||
|
||
b = B(b"hello") | ||
with memoryview(b) as mv: | ||
self.assertEqual(mv.tobytes(), b"hello") | ||
self.assertEqual(rb_call_count, 0) | ||
self.assertEqual(rb_call_count, 1) | ||
|
||
def test_inherit_but_return_something_else(self): | ||
class A(bytearray): | ||
def __buffer__(self, flags): | ||
return memoryview(b"hello") | ||
|
||
a = A(b"hello") | ||
with memoryview(a) as mv: | ||
self.assertEqual(mv.tobytes(), b"hello") | ||
|
||
rb_call_count = 0 | ||
rb_raised = False | ||
class B(bytearray): | ||
def __buffer__(self, flags): | ||
return memoryview(b"hello") | ||
def __release_buffer__(self, view): | ||
nonlocal rb_call_count | ||
rb_call_count += 1 | ||
try: | ||
super().__release_buffer__(view) | ||
except ValueError: | ||
nonlocal rb_raised | ||
rb_raised = True | ||
|
||
b = B(b"hello") | ||
with memoryview(b) as mv: | ||
self.assertEqual(mv.tobytes(), b"hello") | ||
self.assertEqual(rb_call_count, 0) | ||
self.assertEqual(rb_call_count, 1) | ||
self.assertIs(rb_raised, True) | ||
|
||
def test_override_only_release(self): | ||
class C(bytearray): | ||
def __release_buffer__(self, buffer): | ||
super().__release_buffer__(buffer) | ||
|
||
c = C(b"hello") | ||
with memoryview(c) as mv: | ||
self.assertEqual(mv.tobytes(), b"hello") | ||
|
||
def test_release_saves_reference(self): | ||
smuggled_buffer = None | ||
|
||
class C(bytearray): | ||
def __release_buffer__(s, buffer: memoryview): | ||
with self.assertRaises(ValueError): | ||
memoryview(buffer) | ||
with self.assertRaises(ValueError): | ||
buffer.cast("b") | ||
with self.assertRaises(ValueError): | ||
buffer.toreadonly() | ||
with self.assertRaises(ValueError): | ||
buffer[:1] | ||
with self.assertRaises(ValueError): | ||
buffer.__buffer__(0) | ||
nonlocal smuggled_buffer | ||
smuggled_buffer = buffer | ||
self.assertEqual(buffer.tobytes(), b"hello") | ||
super().__release_buffer__(buffer) | ||
|
||
c = C(b"hello") | ||
with memoryview(c) as mv: | ||
self.assertEqual(mv.tobytes(), b"hello") | ||
c.clear() | ||
with self.assertRaises(ValueError): | ||
smuggled_buffer.tobytes() | ||
|
||
def test_release_saves_reference_no_subclassing(self): | ||
ba = bytearray(b"hello") | ||
|
||
class C: | ||
def __buffer__(self, flags): | ||
return memoryview(ba) | ||
|
||
def __release_buffer__(self, buffer): | ||
self.buffer = buffer | ||
|
||
c = C() | ||
with memoryview(c) as mv: | ||
self.assertEqual(mv.tobytes(), b"hello") | ||
self.assertEqual(c.buffer.tobytes(), b"hello") | ||
|
||
with self.assertRaises(BufferError): | ||
ba.clear() | ||
c.buffer.release() | ||
ba.clear() | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a test in which the class has multiple inheritance? Also tests for when there are two or classes in mro which support buffer protocol? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding some tests. It's hard to come up with a case where multiple bases support the C buffer protocol, because that will almost inevitably lead to:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, maybe try one var length and one pure python type which implements buffer protocol. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, just pushed. Thanks for the review! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I see, you would have to write a custom type in C whose layout doesn't conflicts for that. Probably an object which has just object header and supports buffer protocol. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TBH, I agree this is an edge case but I try to be extra careful when touching There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better to catch this now then right before 3.12 final! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Confirmed the bug I outlined above, I'll have to step out for a few hours and fix it after that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thinking about it more, that crash isn't actually due to PEP 688: it reproduces without any Python There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Filed #104297 for that case. |
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
Uh oh!
There was an error while loading. Please reload this page.