/Lib/ctypes/test/test_as_parameter.py

http://unladen-swallow.googlecode.com/ · Python · 215 lines · 143 code · 52 blank · 20 comment · 5 complexity · 60e8dd31042abdd4ca521848352f3732 MD5 · raw file

  1. import unittest
  2. from ctypes import *
  3. import _ctypes_test
  4. dll = CDLL(_ctypes_test.__file__)
  5. try:
  6. CALLBACK_FUNCTYPE = WINFUNCTYPE
  7. except NameError:
  8. # fake to enable this test on Linux
  9. CALLBACK_FUNCTYPE = CFUNCTYPE
  10. class POINT(Structure):
  11. _fields_ = [("x", c_int), ("y", c_int)]
  12. class BasicWrapTestCase(unittest.TestCase):
  13. def wrap(self, param):
  14. return param
  15. def test_wchar_parm(self):
  16. try:
  17. c_wchar
  18. except NameError:
  19. return
  20. f = dll._testfunc_i_bhilfd
  21. f.argtypes = [c_byte, c_wchar, c_int, c_long, c_float, c_double]
  22. result = f(self.wrap(1), self.wrap(u"x"), self.wrap(3), self.wrap(4), self.wrap(5.0), self.wrap(6.0))
  23. self.failUnlessEqual(result, 139)
  24. self.failUnless(type(result), int)
  25. def test_pointers(self):
  26. f = dll._testfunc_p_p
  27. f.restype = POINTER(c_int)
  28. f.argtypes = [POINTER(c_int)]
  29. # This only works if the value c_int(42) passed to the
  30. # function is still alive while the pointer (the result) is
  31. # used.
  32. v = c_int(42)
  33. self.failUnlessEqual(pointer(v).contents.value, 42)
  34. result = f(self.wrap(pointer(v)))
  35. self.failUnlessEqual(type(result), POINTER(c_int))
  36. self.failUnlessEqual(result.contents.value, 42)
  37. # This on works...
  38. result = f(self.wrap(pointer(v)))
  39. self.failUnlessEqual(result.contents.value, v.value)
  40. p = pointer(c_int(99))
  41. result = f(self.wrap(p))
  42. self.failUnlessEqual(result.contents.value, 99)
  43. def test_shorts(self):
  44. f = dll._testfunc_callback_i_if
  45. args = []
  46. expected = [262144, 131072, 65536, 32768, 16384, 8192, 4096, 2048,
  47. 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1]
  48. def callback(v):
  49. args.append(v)
  50. return v
  51. CallBack = CFUNCTYPE(c_int, c_int)
  52. cb = CallBack(callback)
  53. f(self.wrap(2**18), self.wrap(cb))
  54. self.failUnlessEqual(args, expected)
  55. ################################################################
  56. def test_callbacks(self):
  57. f = dll._testfunc_callback_i_if
  58. f.restype = c_int
  59. MyCallback = CFUNCTYPE(c_int, c_int)
  60. def callback(value):
  61. #print "called back with", value
  62. return value
  63. cb = MyCallback(callback)
  64. result = f(self.wrap(-10), self.wrap(cb))
  65. self.failUnlessEqual(result, -18)
  66. # test with prototype
  67. f.argtypes = [c_int, MyCallback]
  68. cb = MyCallback(callback)
  69. result = f(self.wrap(-10), self.wrap(cb))
  70. self.failUnlessEqual(result, -18)
  71. result = f(self.wrap(-10), self.wrap(cb))
  72. self.failUnlessEqual(result, -18)
  73. AnotherCallback = CALLBACK_FUNCTYPE(c_int, c_int, c_int, c_int, c_int)
  74. # check that the prototype works: we call f with wrong
  75. # argument types
  76. cb = AnotherCallback(callback)
  77. self.assertRaises(ArgumentError, f, self.wrap(-10), self.wrap(cb))
  78. def test_callbacks_2(self):
  79. # Can also use simple datatypes as argument type specifiers
  80. # for the callback function.
  81. # In this case the call receives an instance of that type
  82. f = dll._testfunc_callback_i_if
  83. f.restype = c_int
  84. MyCallback = CFUNCTYPE(c_int, c_int)
  85. f.argtypes = [c_int, MyCallback]
  86. def callback(value):
  87. #print "called back with", value
  88. self.failUnlessEqual(type(value), int)
  89. return value
  90. cb = MyCallback(callback)
  91. result = f(self.wrap(-10), self.wrap(cb))
  92. self.failUnlessEqual(result, -18)
  93. def test_longlong_callbacks(self):
  94. f = dll._testfunc_callback_q_qf
  95. f.restype = c_longlong
  96. MyCallback = CFUNCTYPE(c_longlong, c_longlong)
  97. f.argtypes = [c_longlong, MyCallback]
  98. def callback(value):
  99. self.failUnless(isinstance(value, (int, long)))
  100. return value & 0x7FFFFFFF
  101. cb = MyCallback(callback)
  102. self.failUnlessEqual(13577625587, int(f(self.wrap(1000000000000), self.wrap(cb))))
  103. def test_byval(self):
  104. # without prototype
  105. ptin = POINT(1, 2)
  106. ptout = POINT()
  107. # EXPORT int _testfunc_byval(point in, point *pout)
  108. result = dll._testfunc_byval(ptin, byref(ptout))
  109. got = result, ptout.x, ptout.y
  110. expected = 3, 1, 2
  111. self.failUnlessEqual(got, expected)
  112. # with prototype
  113. ptin = POINT(101, 102)
  114. ptout = POINT()
  115. dll._testfunc_byval.argtypes = (POINT, POINTER(POINT))
  116. dll._testfunc_byval.restype = c_int
  117. result = dll._testfunc_byval(self.wrap(ptin), byref(ptout))
  118. got = result, ptout.x, ptout.y
  119. expected = 203, 101, 102
  120. self.failUnlessEqual(got, expected)
  121. def test_struct_return_2H(self):
  122. class S2H(Structure):
  123. _fields_ = [("x", c_short),
  124. ("y", c_short)]
  125. dll.ret_2h_func.restype = S2H
  126. dll.ret_2h_func.argtypes = [S2H]
  127. inp = S2H(99, 88)
  128. s2h = dll.ret_2h_func(self.wrap(inp))
  129. self.failUnlessEqual((s2h.x, s2h.y), (99*2, 88*3))
  130. def test_struct_return_8H(self):
  131. class S8I(Structure):
  132. _fields_ = [("a", c_int),
  133. ("b", c_int),
  134. ("c", c_int),
  135. ("d", c_int),
  136. ("e", c_int),
  137. ("f", c_int),
  138. ("g", c_int),
  139. ("h", c_int)]
  140. dll.ret_8i_func.restype = S8I
  141. dll.ret_8i_func.argtypes = [S8I]
  142. inp = S8I(9, 8, 7, 6, 5, 4, 3, 2)
  143. s8i = dll.ret_8i_func(self.wrap(inp))
  144. self.failUnlessEqual((s8i.a, s8i.b, s8i.c, s8i.d, s8i.e, s8i.f, s8i.g, s8i.h),
  145. (9*2, 8*3, 7*4, 6*5, 5*6, 4*7, 3*8, 2*9))
  146. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  147. class AsParamWrapper(object):
  148. def __init__(self, param):
  149. self._as_parameter_ = param
  150. class AsParamWrapperTestCase(BasicWrapTestCase):
  151. wrap = AsParamWrapper
  152. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  153. class AsParamPropertyWrapper(object):
  154. def __init__(self, param):
  155. self._param = param
  156. def getParameter(self):
  157. return self._param
  158. _as_parameter_ = property(getParameter)
  159. class AsParamPropertyWrapperTestCase(BasicWrapTestCase):
  160. wrap = AsParamPropertyWrapper
  161. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  162. if __name__ == '__main__':
  163. unittest.main()