def test_memoryview(): # create a base memoryview data = bytes(range(8)) V = memoryview(data) assert list(V) == [0,1,2,3,4,5,6,7] # create a cast of it. B = V.cast('H') # B.format = 'H' # B.view.format = points to B.format # slice the cast C = B[0:2] # C.format = # C.view.format = points to B.format # clone the cast D = memoryview(B) # D.format = # D.view.format = # free()s B including B.format del B # try to use C / D # boom! C/D.view.format = assert C[0] == 256 assert D[0] == 256 assert C.format == 'H' assert D.format == 'H' # (to show this bug in non-debug python, where the freed string # is still sitting there, this will allocate a new memoryview # likely in the same place, corrupting the 'H' with an 'I') T = V.cast('I') # C/D.view.format = pointer to T.format = 'I' assert C[0] == 256 assert D[0] == 256 assert C.format == 'H' assert D.format == 'H' if __name__ == '__main__': test_memoryview()

AltStyle によって変換されたページ (->オリジナル) /