def test_from_ctype_ptr_ptr_is_alive(self): data=(ctypes.POINTER(ctypes.c_int)*3)((ctypes.c_int*1)(42), (ctypes.c_int*1)(1), (ctypes.c_int*1)(1)) ref_cnt = sys.getrefcount(data) mem = IndirectMemory2D.from_ctype_ptr(data, 2,1) self.assertEqual(ref_cnt+1, sys.getrefcount(data)) del mem self.assertEqual(ref_cnt, sys.getrefcount(data))
def test_from_ctype_ptr_less_rows_ok(self): data=(ctypes.POINTER(ctypes.c_int)*3)((ctypes.c_int*1)(42), (ctypes.c_int*1)(1), (ctypes.c_int*1)(1)) mem = IndirectMemory2D.from_ctype_ptr(data, 2,1) mem.reinterpret_data('i') view=memoryview(mem) self.assertEqual(view[0, 0], 42) view[0, 0] = 21 self.assertEqual(data[0] [0], 21)
def test_tfrom_ctype_ptr_3d(self): data=(ctypes.POINTER(ctypes.POINTER(ctypes.c_int))*3)() with self.assertRaises(BufferError) as context: mem = IndirectMemory2D.from_ctype_ptr(data, 3,0) self.assertTrue("wrong format: b'" in context.exception.args[0])
def test_from_ctype_ptr_wrong_row_number(self): data=(ctypes.POINTER(ctypes.c_int)*2)((ctypes.c_int*1)(1), (ctypes.c_int*1)(1)) with self.assertRaises(BufferError) as context: mem = IndirectMemory2D.from_ctype_ptr(data, 3,1) self.assertEqual("less rows than expected: 2 vs. 3", context.exception.args[0])
def test_from_ctype_ptr_1d(self): data=(ctypes.c_double*2)(1.0,2.0) with self.assertRaises(BufferError) as context: mem = IndirectMemory2D.from_ctype_ptr(data, 2,0) self.assertTrue("wrong format: b'" in context.exception.args[0])