PageRenderTime 52ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Windows/Python3.8/WPy64-3830/WPy64-3830/python-3.8.3.amd64/Lib/ctypes/test/test_pointers.py

https://gitlab.com/abhi1tb/build
Python | 223 lines | 145 code | 45 blank | 33 comment | 9 complexity | cc84c4a5707b83587f6b1244fc0b4734 MD5 | raw file
  1. import unittest, sys
  2. from ctypes import *
  3. import _ctypes_test
  4. ctype_types = [c_byte, c_ubyte, c_short, c_ushort, c_int, c_uint,
  5. c_long, c_ulong, c_longlong, c_ulonglong, c_double, c_float]
  6. python_types = [int, int, int, int, int, int,
  7. int, int, int, int, float, float]
  8. class PointersTestCase(unittest.TestCase):
  9. def test_pointer_crash(self):
  10. class A(POINTER(c_ulong)):
  11. pass
  12. POINTER(c_ulong)(c_ulong(22))
  13. # Pointer can't set contents: has no _type_
  14. self.assertRaises(TypeError, A, c_ulong(33))
  15. def test_pass_pointers(self):
  16. dll = CDLL(_ctypes_test.__file__)
  17. func = dll._testfunc_p_p
  18. if sizeof(c_longlong) == sizeof(c_void_p):
  19. func.restype = c_longlong
  20. else:
  21. func.restype = c_long
  22. i = c_int(12345678)
  23. ## func.argtypes = (POINTER(c_int),)
  24. address = func(byref(i))
  25. self.assertEqual(c_int.from_address(address).value, 12345678)
  26. func.restype = POINTER(c_int)
  27. res = func(pointer(i))
  28. self.assertEqual(res.contents.value, 12345678)
  29. self.assertEqual(res[0], 12345678)
  30. def test_change_pointers(self):
  31. dll = CDLL(_ctypes_test.__file__)
  32. func = dll._testfunc_p_p
  33. i = c_int(87654)
  34. func.restype = POINTER(c_int)
  35. func.argtypes = (POINTER(c_int),)
  36. res = func(pointer(i))
  37. self.assertEqual(res[0], 87654)
  38. self.assertEqual(res.contents.value, 87654)
  39. # C code: *res = 54345
  40. res[0] = 54345
  41. self.assertEqual(i.value, 54345)
  42. # C code:
  43. # int x = 12321;
  44. # res = &x
  45. x = c_int(12321)
  46. res.contents = x
  47. self.assertEqual(i.value, 54345)
  48. x.value = -99
  49. self.assertEqual(res.contents.value, -99)
  50. def test_callbacks_with_pointers(self):
  51. # a function type receiving a pointer
  52. PROTOTYPE = CFUNCTYPE(c_int, POINTER(c_int))
  53. self.result = []
  54. def func(arg):
  55. for i in range(10):
  56. ## print arg[i],
  57. self.result.append(arg[i])
  58. ## print
  59. return 0
  60. callback = PROTOTYPE(func)
  61. dll = CDLL(_ctypes_test.__file__)
  62. # This function expects a function pointer,
  63. # and calls this with an integer pointer as parameter.
  64. # The int pointer points to a table containing the numbers 1..10
  65. doit = dll._testfunc_callback_with_pointer
  66. ## i = c_int(42)
  67. ## callback(byref(i))
  68. ## self.assertEqual(i.value, 84)
  69. doit(callback)
  70. ## print self.result
  71. doit(callback)
  72. ## print self.result
  73. def test_basics(self):
  74. from operator import delitem
  75. for ct, pt in zip(ctype_types, python_types):
  76. i = ct(42)
  77. p = pointer(i)
  78. ## print type(p.contents), ct
  79. self.assertIs(type(p.contents), ct)
  80. # p.contents is the same as p[0]
  81. ## print p.contents
  82. ## self.assertEqual(p.contents, 42)
  83. ## self.assertEqual(p[0], 42)
  84. self.assertRaises(TypeError, delitem, p, 0)
  85. def test_from_address(self):
  86. from array import array
  87. a = array('i', [100, 200, 300, 400, 500])
  88. addr = a.buffer_info()[0]
  89. p = POINTER(POINTER(c_int))
  90. ## print dir(p)
  91. ## print p.from_address
  92. ## print p.from_address(addr)[0][0]
  93. def test_other(self):
  94. class Table(Structure):
  95. _fields_ = [("a", c_int),
  96. ("b", c_int),
  97. ("c", c_int)]
  98. pt = pointer(Table(1, 2, 3))
  99. self.assertEqual(pt.contents.a, 1)
  100. self.assertEqual(pt.contents.b, 2)
  101. self.assertEqual(pt.contents.c, 3)
  102. pt.contents.c = 33
  103. from ctypes import _pointer_type_cache
  104. del _pointer_type_cache[Table]
  105. def test_basic(self):
  106. p = pointer(c_int(42))
  107. # Although a pointer can be indexed, it has no length
  108. self.assertRaises(TypeError, len, p)
  109. self.assertEqual(p[0], 42)
  110. self.assertEqual(p[0:1], [42])
  111. self.assertEqual(p.contents.value, 42)
  112. def test_charpp(self):
  113. """Test that a character pointer-to-pointer is correctly passed"""
  114. dll = CDLL(_ctypes_test.__file__)
  115. func = dll._testfunc_c_p_p
  116. func.restype = c_char_p
  117. argv = (c_char_p * 2)()
  118. argc = c_int( 2 )
  119. argv[0] = b'hello'
  120. argv[1] = b'world'
  121. result = func( byref(argc), argv )
  122. self.assertEqual(result, b'world')
  123. def test_bug_1467852(self):
  124. # http://sourceforge.net/tracker/?func=detail&atid=532154&aid=1467852&group_id=71702
  125. x = c_int(5)
  126. dummy = []
  127. for i in range(32000):
  128. dummy.append(c_int(i))
  129. y = c_int(6)
  130. p = pointer(x)
  131. pp = pointer(p)
  132. q = pointer(y)
  133. pp[0] = q # <==
  134. self.assertEqual(p[0], 6)
  135. def test_c_void_p(self):
  136. # http://sourceforge.net/tracker/?func=detail&aid=1518190&group_id=5470&atid=105470
  137. if sizeof(c_void_p) == 4:
  138. self.assertEqual(c_void_p(0xFFFFFFFF).value,
  139. c_void_p(-1).value)
  140. self.assertEqual(c_void_p(0xFFFFFFFFFFFFFFFF).value,
  141. c_void_p(-1).value)
  142. elif sizeof(c_void_p) == 8:
  143. self.assertEqual(c_void_p(0xFFFFFFFF).value,
  144. 0xFFFFFFFF)
  145. self.assertEqual(c_void_p(0xFFFFFFFFFFFFFFFF).value,
  146. c_void_p(-1).value)
  147. self.assertEqual(c_void_p(0xFFFFFFFFFFFFFFFFFFFFFFFF).value,
  148. c_void_p(-1).value)
  149. self.assertRaises(TypeError, c_void_p, 3.14) # make sure floats are NOT accepted
  150. self.assertRaises(TypeError, c_void_p, object()) # nor other objects
  151. def test_pointers_bool(self):
  152. # NULL pointers have a boolean False value, non-NULL pointers True.
  153. self.assertEqual(bool(POINTER(c_int)()), False)
  154. self.assertEqual(bool(pointer(c_int())), True)
  155. self.assertEqual(bool(CFUNCTYPE(None)(0)), False)
  156. self.assertEqual(bool(CFUNCTYPE(None)(42)), True)
  157. # COM methods are boolean True:
  158. if sys.platform == "win32":
  159. mth = WINFUNCTYPE(None)(42, "name", (), None)
  160. self.assertEqual(bool(mth), True)
  161. def test_pointer_type_name(self):
  162. LargeNamedType = type('T' * 2 ** 25, (Structure,), {})
  163. self.assertTrue(POINTER(LargeNamedType))
  164. # to not leak references, we must clean _pointer_type_cache
  165. from ctypes import _pointer_type_cache
  166. del _pointer_type_cache[LargeNamedType]
  167. def test_pointer_type_str_name(self):
  168. large_string = 'T' * 2 ** 25
  169. P = POINTER(large_string)
  170. self.assertTrue(P)
  171. # to not leak references, we must clean _pointer_type_cache
  172. from ctypes import _pointer_type_cache
  173. del _pointer_type_cache[id(P)]
  174. def test_abstract(self):
  175. from ctypes import _Pointer
  176. self.assertRaises(TypeError, _Pointer.set_type, 42)
  177. if __name__ == '__main__':
  178. unittest.main()