PageRenderTime 84ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/Lib/ctypes/test/test_functions.py

https://bitbucket.org/hacman/python
Python | 408 lines | 291 code | 81 blank | 36 comment | 20 complexity | 6b2d99be2ccb7f81562620b718e83bea MD5 | raw file
Possible License(s): 0BSD
  1. """
  2. Here is probably the place to write the docs, since the test-cases
  3. show how the type behave.
  4. Later...
  5. """
  6. from ctypes import *
  7. import sys, unittest
  8. try:
  9. WINFUNCTYPE
  10. except NameError:
  11. # fake to enable this test on Linux
  12. WINFUNCTYPE = CFUNCTYPE
  13. import _ctypes_test
  14. dll = CDLL(_ctypes_test.__file__)
  15. if sys.platform == "win32":
  16. windll = WinDLL(_ctypes_test.__file__)
  17. class POINT(Structure):
  18. _fields_ = [("x", c_int), ("y", c_int)]
  19. class RECT(Structure):
  20. _fields_ = [("left", c_int), ("top", c_int),
  21. ("right", c_int), ("bottom", c_int)]
  22. class FunctionTestCase(unittest.TestCase):
  23. def test_mro(self):
  24. # in Python 2.3, this raises TypeError: MRO conflict among bases classes,
  25. # in Python 2.2 it works.
  26. #
  27. # But in early versions of _ctypes.c, the result of tp_new
  28. # wasn't checked, and it even crashed Python.
  29. # Found by Greg Chapman.
  30. try:
  31. class X(object, Array):
  32. _length_ = 5
  33. _type_ = "i"
  34. except TypeError:
  35. pass
  36. from _ctypes import _Pointer
  37. try:
  38. class X(object, _Pointer):
  39. pass
  40. except TypeError:
  41. pass
  42. from _ctypes import _SimpleCData
  43. try:
  44. class X(object, _SimpleCData):
  45. _type_ = "i"
  46. except TypeError:
  47. pass
  48. try:
  49. class X(object, Structure):
  50. _fields_ = []
  51. except TypeError:
  52. pass
  53. def test_wchar_parm(self):
  54. try:
  55. c_wchar
  56. except NameError:
  57. return
  58. f = dll._testfunc_i_bhilfd
  59. f.argtypes = [c_byte, c_wchar, c_int, c_long, c_float, c_double]
  60. result = f(1, u"x", 3, 4, 5.0, 6.0)
  61. self.assertEqual(result, 139)
  62. self.assertEqual(type(result), int)
  63. def test_wchar_result(self):
  64. try:
  65. c_wchar
  66. except NameError:
  67. return
  68. f = dll._testfunc_i_bhilfd
  69. f.argtypes = [c_byte, c_short, c_int, c_long, c_float, c_double]
  70. f.restype = c_wchar
  71. result = f(0, 0, 0, 0, 0, 0)
  72. self.assertEqual(result, u'\x00')
  73. def test_voidresult(self):
  74. f = dll._testfunc_v
  75. f.restype = None
  76. f.argtypes = [c_int, c_int, POINTER(c_int)]
  77. result = c_int()
  78. self.assertEqual(None, f(1, 2, byref(result)))
  79. self.assertEqual(result.value, 3)
  80. def test_intresult(self):
  81. f = dll._testfunc_i_bhilfd
  82. f.argtypes = [c_byte, c_short, c_int, c_long, c_float, c_double]
  83. f.restype = c_int
  84. result = f(1, 2, 3, 4, 5.0, 6.0)
  85. self.assertEqual(result, 21)
  86. self.assertEqual(type(result), int)
  87. result = f(-1, -2, -3, -4, -5.0, -6.0)
  88. self.assertEqual(result, -21)
  89. self.assertEqual(type(result), int)
  90. # If we declare the function to return a short,
  91. # is the high part split off?
  92. f.restype = c_short
  93. result = f(1, 2, 3, 4, 5.0, 6.0)
  94. self.assertEqual(result, 21)
  95. self.assertEqual(type(result), int)
  96. result = f(1, 2, 3, 0x10004, 5.0, 6.0)
  97. self.assertEqual(result, 21)
  98. self.assertEqual(type(result), int)
  99. # You cannot assign character format codes as restype any longer
  100. self.assertRaises(TypeError, setattr, f, "restype", "i")
  101. def test_floatresult(self):
  102. f = dll._testfunc_f_bhilfd
  103. f.argtypes = [c_byte, c_short, c_int, c_long, c_float, c_double]
  104. f.restype = c_float
  105. result = f(1, 2, 3, 4, 5.0, 6.0)
  106. self.assertEqual(result, 21)
  107. self.assertEqual(type(result), float)
  108. result = f(-1, -2, -3, -4, -5.0, -6.0)
  109. self.assertEqual(result, -21)
  110. self.assertEqual(type(result), float)
  111. def test_doubleresult(self):
  112. f = dll._testfunc_d_bhilfd
  113. f.argtypes = [c_byte, c_short, c_int, c_long, c_float, c_double]
  114. f.restype = c_double
  115. result = f(1, 2, 3, 4, 5.0, 6.0)
  116. self.assertEqual(result, 21)
  117. self.assertEqual(type(result), float)
  118. result = f(-1, -2, -3, -4, -5.0, -6.0)
  119. self.assertEqual(result, -21)
  120. self.assertEqual(type(result), float)
  121. def test_longdoubleresult(self):
  122. f = dll._testfunc_D_bhilfD
  123. f.argtypes = [c_byte, c_short, c_int, c_long, c_float, c_longdouble]
  124. f.restype = c_longdouble
  125. result = f(1, 2, 3, 4, 5.0, 6.0)
  126. self.assertEqual(result, 21)
  127. self.assertEqual(type(result), float)
  128. result = f(-1, -2, -3, -4, -5.0, -6.0)
  129. self.assertEqual(result, -21)
  130. self.assertEqual(type(result), float)
  131. def test_longlongresult(self):
  132. try:
  133. c_longlong
  134. except NameError:
  135. return
  136. f = dll._testfunc_q_bhilfd
  137. f.restype = c_longlong
  138. f.argtypes = [c_byte, c_short, c_int, c_long, c_float, c_double]
  139. result = f(1, 2, 3, 4, 5.0, 6.0)
  140. self.assertEqual(result, 21)
  141. f = dll._testfunc_q_bhilfdq
  142. f.restype = c_longlong
  143. f.argtypes = [c_byte, c_short, c_int, c_long, c_float, c_double, c_longlong]
  144. result = f(1, 2, 3, 4, 5.0, 6.0, 21)
  145. self.assertEqual(result, 42)
  146. def test_stringresult(self):
  147. f = dll._testfunc_p_p
  148. f.argtypes = None
  149. f.restype = c_char_p
  150. result = f("123")
  151. self.assertEqual(result, "123")
  152. result = f(None)
  153. self.assertEqual(result, None)
  154. def test_pointers(self):
  155. f = dll._testfunc_p_p
  156. f.restype = POINTER(c_int)
  157. f.argtypes = [POINTER(c_int)]
  158. # This only works if the value c_int(42) passed to the
  159. # function is still alive while the pointer (the result) is
  160. # used.
  161. v = c_int(42)
  162. self.assertEqual(pointer(v).contents.value, 42)
  163. result = f(pointer(v))
  164. self.assertEqual(type(result), POINTER(c_int))
  165. self.assertEqual(result.contents.value, 42)
  166. # This on works...
  167. result = f(pointer(v))
  168. self.assertEqual(result.contents.value, v.value)
  169. p = pointer(c_int(99))
  170. result = f(p)
  171. self.assertEqual(result.contents.value, 99)
  172. arg = byref(v)
  173. result = f(arg)
  174. self.assertNotEqual(result.contents, v.value)
  175. self.assertRaises(ArgumentError, f, byref(c_short(22)))
  176. # It is dangerous, however, because you don't control the lifetime
  177. # of the pointer:
  178. result = f(byref(c_int(99)))
  179. self.assertNotEqual(result.contents, 99)
  180. def test_errors(self):
  181. f = dll._testfunc_p_p
  182. f.restype = c_int
  183. class X(Structure):
  184. _fields_ = [("y", c_int)]
  185. self.assertRaises(TypeError, f, X()) #cannot convert parameter
  186. ################################################################
  187. def test_shorts(self):
  188. f = dll._testfunc_callback_i_if
  189. args = []
  190. expected = [262144, 131072, 65536, 32768, 16384, 8192, 4096, 2048,
  191. 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1]
  192. def callback(v):
  193. args.append(v)
  194. return v
  195. CallBack = CFUNCTYPE(c_int, c_int)
  196. cb = CallBack(callback)
  197. f(2**18, cb)
  198. self.assertEqual(args, expected)
  199. ################################################################
  200. def test_callbacks(self):
  201. f = dll._testfunc_callback_i_if
  202. f.restype = c_int
  203. f.argtypes = None
  204. MyCallback = CFUNCTYPE(c_int, c_int)
  205. def callback(value):
  206. #print "called back with", value
  207. return value
  208. cb = MyCallback(callback)
  209. result = f(-10, cb)
  210. self.assertEqual(result, -18)
  211. # test with prototype
  212. f.argtypes = [c_int, MyCallback]
  213. cb = MyCallback(callback)
  214. result = f(-10, cb)
  215. self.assertEqual(result, -18)
  216. AnotherCallback = WINFUNCTYPE(c_int, c_int, c_int, c_int, c_int)
  217. # check that the prototype works: we call f with wrong
  218. # argument types
  219. cb = AnotherCallback(callback)
  220. self.assertRaises(ArgumentError, f, -10, cb)
  221. def test_callbacks_2(self):
  222. # Can also use simple datatypes as argument type specifiers
  223. # for the callback function.
  224. # In this case the call receives an instance of that type
  225. f = dll._testfunc_callback_i_if
  226. f.restype = c_int
  227. MyCallback = CFUNCTYPE(c_int, c_int)
  228. f.argtypes = [c_int, MyCallback]
  229. def callback(value):
  230. #print "called back with", value
  231. self.assertEqual(type(value), int)
  232. return value
  233. cb = MyCallback(callback)
  234. result = f(-10, cb)
  235. self.assertEqual(result, -18)
  236. def test_longlong_callbacks(self):
  237. f = dll._testfunc_callback_q_qf
  238. f.restype = c_longlong
  239. MyCallback = CFUNCTYPE(c_longlong, c_longlong)
  240. f.argtypes = [c_longlong, MyCallback]
  241. def callback(value):
  242. self.assertTrue(isinstance(value, (int, long)))
  243. return value & 0x7FFFFFFF
  244. cb = MyCallback(callback)
  245. self.assertEqual(13577625587, f(1000000000000, cb))
  246. def test_errors(self):
  247. self.assertRaises(AttributeError, getattr, dll, "_xxx_yyy")
  248. self.assertRaises(ValueError, c_int.in_dll, dll, "_xxx_yyy")
  249. def test_byval(self):
  250. # without prototype
  251. ptin = POINT(1, 2)
  252. ptout = POINT()
  253. # EXPORT int _testfunc_byval(point in, point *pout)
  254. result = dll._testfunc_byval(ptin, byref(ptout))
  255. got = result, ptout.x, ptout.y
  256. expected = 3, 1, 2
  257. self.assertEqual(got, expected)
  258. # with prototype
  259. ptin = POINT(101, 102)
  260. ptout = POINT()
  261. dll._testfunc_byval.argtypes = (POINT, POINTER(POINT))
  262. dll._testfunc_byval.restype = c_int
  263. result = dll._testfunc_byval(ptin, byref(ptout))
  264. got = result, ptout.x, ptout.y
  265. expected = 203, 101, 102
  266. self.assertEqual(got, expected)
  267. def test_struct_return_2H(self):
  268. class S2H(Structure):
  269. _fields_ = [("x", c_short),
  270. ("y", c_short)]
  271. dll.ret_2h_func.restype = S2H
  272. dll.ret_2h_func.argtypes = [S2H]
  273. inp = S2H(99, 88)
  274. s2h = dll.ret_2h_func(inp)
  275. self.assertEqual((s2h.x, s2h.y), (99*2, 88*3))
  276. if sys.platform == "win32":
  277. def test_struct_return_2H_stdcall(self):
  278. class S2H(Structure):
  279. _fields_ = [("x", c_short),
  280. ("y", c_short)]
  281. windll.s_ret_2h_func.restype = S2H
  282. windll.s_ret_2h_func.argtypes = [S2H]
  283. s2h = windll.s_ret_2h_func(S2H(99, 88))
  284. self.assertEqual((s2h.x, s2h.y), (99*2, 88*3))
  285. def test_struct_return_8H(self):
  286. class S8I(Structure):
  287. _fields_ = [("a", c_int),
  288. ("b", c_int),
  289. ("c", c_int),
  290. ("d", c_int),
  291. ("e", c_int),
  292. ("f", c_int),
  293. ("g", c_int),
  294. ("h", c_int)]
  295. dll.ret_8i_func.restype = S8I
  296. dll.ret_8i_func.argtypes = [S8I]
  297. inp = S8I(9, 8, 7, 6, 5, 4, 3, 2)
  298. s8i = dll.ret_8i_func(inp)
  299. self.assertEqual((s8i.a, s8i.b, s8i.c, s8i.d, s8i.e, s8i.f, s8i.g, s8i.h),
  300. (9*2, 8*3, 7*4, 6*5, 5*6, 4*7, 3*8, 2*9))
  301. if sys.platform == "win32":
  302. def test_struct_return_8H_stdcall(self):
  303. class S8I(Structure):
  304. _fields_ = [("a", c_int),
  305. ("b", c_int),
  306. ("c", c_int),
  307. ("d", c_int),
  308. ("e", c_int),
  309. ("f", c_int),
  310. ("g", c_int),
  311. ("h", c_int)]
  312. windll.s_ret_8i_func.restype = S8I
  313. windll.s_ret_8i_func.argtypes = [S8I]
  314. inp = S8I(9, 8, 7, 6, 5, 4, 3, 2)
  315. s8i = windll.s_ret_8i_func(inp)
  316. self.assertEqual((s8i.a, s8i.b, s8i.c, s8i.d, s8i.e, s8i.f, s8i.g, s8i.h),
  317. (9*2, 8*3, 7*4, 6*5, 5*6, 4*7, 3*8, 2*9))
  318. def test_sf1651235(self):
  319. # see http://www.python.org/sf/1651235
  320. proto = CFUNCTYPE(c_int, RECT, POINT)
  321. def callback(*args):
  322. return 0
  323. callback = proto(callback)
  324. self.assertRaises(ArgumentError, lambda: callback((1, 2, 3, 4), POINT()))
  325. if __name__ == '__main__':
  326. unittest.main()