File tree 1 file changed +30
-0
lines changed 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change @@ -4604,6 +4604,36 @@ def __release_buffer__(self, view):
4604
4604
self .assertEqual (rb_call_count , 0 )
4605
4605
self .assertEqual (rb_call_count , 1 )
4606
4606
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
+
4607
4637
4608
4638
if __name__ == "__main__" :
4609
4639
unittest .main ()
You can’t perform that action at this time.
0 commit comments