def test_byref_pointer(self): from ctypes import c_short, c_uint, c_int, c_long, pointer, POINTER, byref LPINT = POINTER(c_int) LPINT.from_param(byref(c_int(42))) self.assertRaises(TypeError, LPINT.from_param, byref(c_short(22))) if c_int != c_long: self.assertRaises(TypeError, LPINT.from_param, byref(c_long(22))) self.assertRaises(TypeError, LPINT.from_param, byref(c_uint(22)))
def test_byref_pointer(self): from ctypes import c_short, c_uint, c_int, c_long, POINTER, byref LPINT = POINTER(c_int) LPINT.from_param(byref(c_int(42))) self.assertRaises(TypeError, LPINT.from_param, byref(c_short(22))) if c_int != c_long: self.assertRaises(TypeError, LPINT.from_param, byref(c_long(22))) self.assertRaises(TypeError, LPINT.from_param, byref(c_uint(22)))
def test_byref_pointerpointer(self): # See above from ctypes import c_short, c_uint, c_int, c_long, pointer, POINTER, byref LPLPINT = POINTER(POINTER(c_int)) LPLPINT.from_param(byref(pointer(c_int(42)))) raises(TypeError, LPLPINT.from_param, byref(pointer(c_short(22)))) if c_int != c_long: raises(TypeError, LPLPINT.from_param, byref(pointer(c_long(22)))) raises(TypeError, LPLPINT.from_param, byref(pointer(c_uint(22))))
def test_array_pointers(self): from ctypes import c_short, c_uint, c_int, c_long, POINTER INTARRAY = c_int * 3 ia = INTARRAY() self.assertEqual(len(ia), 3) self.assertEqual([ia[i] for i in range(3)], [0, 0, 0]) LPINT = POINTER(c_int) LPINT.from_param((c_int * 3)()) self.assertRaises(TypeError, LPINT.from_param, c_short * 3) self.assertRaises(TypeError, LPINT.from_param, c_long * 3) self.assertRaises(TypeError, LPINT.from_param, c_uint * 3)
def test_int_pointers(self): from ctypes import c_short, c_uint, c_int, c_long, POINTER, pointer LPINT = POINTER(c_int) x = LPINT.from_param(pointer(c_int(42))) self.assertEqual(x.contents.value, 42) self.assertEqual(LPINT(c_int(42)).contents.value, 42) self.assertEqual(LPINT.from_param(None), None) if c_int != c_long: self.assertRaises(TypeError, LPINT.from_param, pointer(c_long(42))) self.assertRaises(TypeError, LPINT.from_param, pointer(c_uint(42))) self.assertRaises(TypeError, LPINT.from_param, pointer(c_short(42)))
def test_array_pointers(self): from ctypes import c_short, c_uint, c_int, c_long, POINTER INTARRAY = c_int * 3 ia = INTARRAY() self.assertEqual(len(ia), 3) self.assertEqual([ ia[i] for i in range(3) ], [0, 0, 0]) LPINT = POINTER(c_int) LPINT.from_param((c_int * 3)()) self.assertRaises(TypeError, LPINT.from_param, c_short * 3) self.assertRaises(TypeError, LPINT.from_param, c_long * 3) self.assertRaises(TypeError, LPINT.from_param, c_uint * 3)
def test_byref_pointer(self): # The from_param class method of POINTER(typ) classes accepts what is # returned by byref(obj), it type(obj) == typ from ctypes import c_short, c_uint, c_int, c_long, pointer, POINTER, byref LPINT = POINTER(c_int) LPINT.from_param(byref(c_int(42))) self.assertRaises(TypeError, LPINT.from_param, byref(c_short(22))) if c_int != c_long: self.assertRaises(TypeError, LPINT.from_param, byref(c_long(22))) self.assertRaises(TypeError, LPINT.from_param, byref(c_uint(22)))
def test_int_pointers(self): from ctypes import c_short, c_uint, c_int, c_long, POINTER, pointer LPINT = POINTER(c_int) x = LPINT.from_param(pointer(c_int(42))) self.assertEqual(x.contents.value, 42) self.assertEqual(LPINT(c_int(42)).contents.value, 42) self.assertEqual(LPINT.from_param(None), None) if c_int != c_long: self.assertRaises(TypeError, LPINT.from_param, pointer(c_long(42))) self.assertRaises(TypeError, LPINT.from_param, pointer(c_uint(42))) self.assertRaises(TypeError, LPINT.from_param, pointer(c_short(42))) return
def test_array_pointers(self): from ctypes import c_short, c_uint, c_int, c_long, POINTER INTARRAY = c_int * 3 ia = INTARRAY() self.assertEqual(len(ia), 3) self.assertEqual([ia[i] for i in range(3)], [0, 0, 0]) # Pointers are only compatible with arrays containing items of # the same type! LPINT = POINTER(c_int) LPINT.from_param((c_int*3)()) self.assertRaises(TypeError, LPINT.from_param, c_short*3) self.assertRaises(TypeError, LPINT.from_param, c_long*3) self.assertRaises(TypeError, LPINT.from_param, c_uint*3)
def test_int_pointers(self): from ctypes import c_short, c_uint, c_int, c_long, POINTER, pointer LPINT = POINTER(c_int) ## p = pointer(c_int(42)) ## x = LPINT.from_param(p) x = LPINT.from_param(pointer(c_int(42))) assert x.contents.value == 42 assert LPINT(c_int(42)).contents.value == 42 assert not LPINT.from_param(None) if c_int != c_long: raises(TypeError, LPINT.from_param, pointer(c_long(42))) raises(TypeError, LPINT.from_param, pointer(c_uint(42))) raises(TypeError, LPINT.from_param, pointer(c_short(42)))
def test_int_pointers(self): from ctypes import c_short, c_uint, c_int, c_long, POINTER, pointer LPINT = POINTER(c_int) ## p = pointer(c_int(42)) ## x = LPINT.from_param(p) x = LPINT.from_param(pointer(c_int(42))) self.assertEqual(x.contents.value, 42) self.assertEqual(LPINT(c_int(42)).contents.value, 42) if not due_to_ironpython_bug("http://www.codeplex.com/IronPython/WorkItem/View.aspx?WorkItemId=24755"): self.assertEqual(LPINT.from_param(None), None) if c_int != c_long: self.assertRaises(TypeError, LPINT.from_param, pointer(c_long(42))) self.assertRaises(TypeError, LPINT.from_param, pointer(c_uint(42))) self.assertRaises(TypeError, LPINT.from_param, pointer(c_short(42)))
def test_int_pointers(self): from ctypes import c_short, c_uint, c_int, c_long, POINTER, pointer LPINT = POINTER(c_int) ## p = pointer(c_int(42)) ## x = LPINT.from_param(p) x = LPINT.from_param(pointer(c_int(42))) self.assertEqual(x.contents.value, 42) self.assertEqual(LPINT(c_int(42)).contents.value, 42) if not due_to_ironpython_bug( "http://www.codeplex.com/IronPython/WorkItem/View.aspx?WorkItemId=24755" ): self.assertEqual(LPINT.from_param(None), None) if c_int != c_long: self.assertRaises(TypeError, LPINT.from_param, pointer(c_long(42))) self.assertRaises(TypeError, LPINT.from_param, pointer(c_uint(42))) self.assertRaises(TypeError, LPINT.from_param, pointer(c_short(42)))
def test_pointer_subclasses(): Void_pp = POINTER(c_void_p) class My_void_p(c_void_p): pass My_void_pp = POINTER(My_void_p) o = My_void_pp() assert Void_pp.from_param(o) is o
def test_pointer_subclasses(self): from ctypes import POINTER, c_void_p Void_pp = POINTER(c_void_p) class My_void_p(c_void_p): pass My_void_pp = POINTER(My_void_p) o = My_void_pp() assert Void_pp.from_param(o) is o
def test_pointer_subclasses(self): from ctypes import * Void_pp = POINTER(c_void_p) class My_void_p(c_void_p): pass My_void_pp = POINTER(My_void_p) o = My_void_pp() assert Void_pp.from_param(o) is o
def test_array_pointers(self): from ctypes import c_short, c_uint, c_int, c_long, POINTER INTARRAY = c_int * 3 ia = INTARRAY() assert len(ia) == 3 assert [ia[i] for i in range(3)] == [0, 0, 0] # Pointers are only compatible with arrays containing items of # the same type! LPINT = POINTER(c_int) LPINT.from_param((c_int * 3)()) with pytest.raises(TypeError): LPINT.from_param(c_short * 3) with pytest.raises(TypeError): LPINT.from_param(c_long * 3) with pytest.raises(TypeError): LPINT.from_param(c_uint * 3)
def test_struct(): return a = (Vector * 2)() a[0].d[0] = 0.1234 a[0].d[1] = 1.0 a[1].d[0] = 0.234 a[1].d[1] = 2.0 assert a[0].d[0] == 0.1234000027179718 assert a[0].d[1] == 1.0 assert a[1].d[0] == 0.23399999737739563 assert a[1].d[1] == 2.0 ptr = byref(a) a1 = cast(ptr, POINTER(Vector * 2)).contents assert a[0].d[0] == a1[0].d[0] VectorType = POINTER(Vector * 2) a2 = VectorType.from_param(pointer(a)).contents assert a[0].d[0] == a2[0].d[0] a3 = cast(ptr, VectorType).contents assert a[0].d[0] == a3[0].d[0] # print(f'sizeof {sizeof(Vector())}') # 8 for 2 c_float dimensions # print(f'sizeof {sizeof(a)}') # 16 for array of 2 Vectors each with 2 c_float dimensions # resize(a, 24) # print(f'sizeof {sizeof(a)}') # 24 for array of 3 Vectors each with 2 c_float dimensions a4 = customresize(a, 3) a4[2].d[0] = 3.0 a4[2].d[1] = 4.0 assert a4[2].d[0] == 3.0 assert a4[2].d[1] == 4.0 ptr2 = byref(a) a5 = cast(ptr2, POINTER(Vector * 3)).contents assert a4[0].d[0] == a5[0].d[0] assert a4[2].d[1] == a5[2].d[1]
def test_struct(): return r = ReqStruct() r.vector.d[0] = 0.1234 r.vector.d[1] = 0.234 assert r.vector.d[0] == 0.1234000027179718 assert r.vector.d[1] == 0.23399999737739563 ptr = byref(r) r1 = cast(ptr, POINTER(ReqStruct)).contents assert r.vector.d[0] == r1.vector.d[0] ReqStructType = POINTER(ReqStruct) r2 = ReqStructType.from_param(pointer(r)).contents assert r.vector.d[0] == r2.vector.d[0] r3 = cast(ptr, ReqStructType).contents assert r.vector.d[0] == r3.vector.d[0]
def _RAMPPTR(cls): cls = POINTER(cls) cls.from_param = classmethod(cast_from_tuple(cls.from_param)) return cls
def _POINTER(cls): cls = POINTER(cls) cls.from_param = classmethod(allow_void_p_param(cls.from_param)) cls.get_void_p = get_void_p return cls