File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed
Expand file tree Collapse file tree 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):
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
46084638if __name__ == "__main__" :
46094639 unittest .main ()
You can’t perform that action at this time.
0 commit comments