#NEWLINE# @ unittest . skipIf ( ( sys . version_info < ( 3 , ) ) , 'memoryview not supported' ) #NEWLINE# def test_memoryview( self ) : #NEWLINE# #INDENT# from ctypes import c_uint32 , c_void_p , POINTER , cast #NEWLINE# dtype = np . dtype ( np . uint32 ) #NEWLINE# bytesize = ( dtype . itemsize * 10 ) #NEWLINE# mi = rtsys . meminfo_alloc ( bytesize , safe = True ) #NEWLINE# addr = mi . data #NEWLINE# c_arr = cast ( c_void_p ( mi . data ) , POINTER ( ( c_uint32 * 10 ) ) ) #NEWLINE# for i in range ( 10 ) : #NEWLINE# #INDENT# self . assertEqual ( c_arr . contents [ i ] , 3419130827 ) #NEWLINE# #UNINDENT# for i in range ( 10 ) : #NEWLINE# #INDENT# c_arr . contents [ arr ] = ( i + 1 ) #NEWLINE# #UNINDENT# mview = memoryview ( mi ) #NEWLINE# self . assertEqual ( mview . nbytes , bytesize ) #NEWLINE# self . assertFalse ( mview . readonly ) #NEWLINE# self . assertIs ( mi , mview . obj ) #NEWLINE# self . assertTrue ( mview . c_contiguous ) #NEWLINE# self . assertEqual ( mview . itemsize , 1 ) #NEWLINE# self . assertEqual ( mview . ndim , 1 ) #NEWLINE# del mi #NEWLINE# arr = np . ndarray ( dtype = dtype , shape = ( mview . nbytes // dtype . itemsize ) , buffer = mview ) #NEWLINE# del mview #NEWLINE# np . testing . assert_equal ( ( np . arange ( arr . size ) + 1 ) , arr ) #NEWLINE# arr += 1 #NEWLINE# for i in range ( 10 ) : #NEWLINE# #INDENT# self . assertEqual ( c_arr . contents [ i ] , ( i + 2 ) ) #NEWLINE# #UNINDENT# self . assertEqual ( arr . ctypes . data , addr ) #NEWLINE# del arr