Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(120489)

Side by Side Diff: Lib/ctypes/test/test_pointers.py

Issue 23670: Modifications to support iOS as a development platform
Patch Set: Created 3 years, 8 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/ctypes/test/test_pickling.py ('k') | Lib/ctypes/test/test_prototypes.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 import os
1 import unittest, sys 2 import unittest, sys
2 3
3 from ctypes import * 4 from ctypes import *
4 import _ctypes_test 5 import _ctypes_test
5 6
6 ctype_types = [c_byte, c_ubyte, c_short, c_ushort, c_int, c_uint, 7 ctype_types = [c_byte, c_ubyte, c_short, c_ushort, c_int, c_uint,
7 c_long, c_ulong, c_longlong, c_ulonglong, c_double, c_float] 8 c_long, c_ulong, c_longlong, c_ulonglong, c_double, c_float]
8 python_types = [int, int, int, int, int, int, 9 python_types = [int, int, int, int, int, int,
9 int, int, int, int, float, float] 10 int, int, int, int, float, float]
10 11
11 class PointersTestCase(unittest.TestCase): 12 class PointersTestCase(unittest.TestCase):
12 13
13 def test_pointer_crash(self): 14 def test_pointer_crash(self):
14 15
15 class A(POINTER(c_ulong)): 16 class A(POINTER(c_ulong)):
16 pass 17 pass
17 18
18 POINTER(c_ulong)(c_ulong(22)) 19 POINTER(c_ulong)(c_ulong(22))
19 # Pointer can't set contents: has no _type_ 20 # Pointer can't set contents: has no _type_
20 self.assertRaises(TypeError, A, c_ulong(33)) 21 self.assertRaises(TypeError, A, c_ulong(33))
21 22
22 def test_pass_pointers(self): 23 def test_pass_pointers(self):
23 dll = CDLL(_ctypes_test.__file__) 24 dll = CDLL(getattr(_ctypes_test, '__file__', os.environ['TEST_EXECUTABLE ']))
24 func = dll._testfunc_p_p 25 func = dll._testfunc_p_p
25 if sizeof(c_longlong) == sizeof(c_void_p): 26 if sizeof(c_longlong) == sizeof(c_void_p):
26 func.restype = c_longlong 27 func.restype = c_longlong
27 else: 28 else:
28 func.restype = c_long 29 func.restype = c_long
29 30
30 i = c_int(12345678) 31 i = c_int(12345678)
31 ## func.argtypes = (POINTER(c_int),) 32 ## func.argtypes = (POINTER(c_int),)
32 address = func(byref(i)) 33 address = func(byref(i))
33 self.assertEqual(c_int.from_address(address).value, 12345678) 34 self.assertEqual(c_int.from_address(address).value, 12345678)
34 35
35 func.restype = POINTER(c_int) 36 func.restype = POINTER(c_int)
36 res = func(pointer(i)) 37 res = func(pointer(i))
37 self.assertEqual(res.contents.value, 12345678) 38 self.assertEqual(res.contents.value, 12345678)
38 self.assertEqual(res[0], 12345678) 39 self.assertEqual(res[0], 12345678)
39 40
40 def test_change_pointers(self): 41 def test_change_pointers(self):
41 dll = CDLL(_ctypes_test.__file__) 42 dll = CDLL(getattr(_ctypes_test, '__file__', os.environ['TEST_EXECUTABLE ']))
42 func = dll._testfunc_p_p 43 func = dll._testfunc_p_p
43 44
44 i = c_int(87654) 45 i = c_int(87654)
45 func.restype = POINTER(c_int) 46 func.restype = POINTER(c_int)
46 func.argtypes = (POINTER(c_int),) 47 func.argtypes = (POINTER(c_int),)
47 48
48 res = func(pointer(i)) 49 res = func(pointer(i))
49 self.assertEqual(res[0], 87654) 50 self.assertEqual(res[0], 87654)
50 self.assertEqual(res.contents.value, 87654) 51 self.assertEqual(res.contents.value, 87654)
51 52
(...skipping 18 matching lines...) Expand all
70 self.result = [] 71 self.result = []
71 72
72 def func(arg): 73 def func(arg):
73 for i in range(10): 74 for i in range(10):
74 ## print arg[i], 75 ## print arg[i],
75 self.result.append(arg[i]) 76 self.result.append(arg[i])
76 ## print 77 ## print
77 return 0 78 return 0
78 callback = PROTOTYPE(func) 79 callback = PROTOTYPE(func)
79 80
80 dll = CDLL(_ctypes_test.__file__) 81 dll = CDLL(getattr(_ctypes_test, '__file__', os.environ['TEST_EXECUTABLE ']))
81 # This function expects a function pointer, 82 # This function expects a function pointer,
82 # and calls this with an integer pointer as parameter. 83 # and calls this with an integer pointer as parameter.
83 # The int pointer points to a table containing the numbers 1..10 84 # The int pointer points to a table containing the numbers 1..10
84 doit = dll._testfunc_callback_with_pointer 85 doit = dll._testfunc_callback_with_pointer
85 86
86 ## i = c_int(42) 87 ## i = c_int(42)
87 ## callback(byref(i)) 88 ## callback(byref(i))
88 ## self.assertEqual(i.value, 84) 89 ## self.assertEqual(i.value, 84)
89 90
90 doit(callback) 91 doit(callback)
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
136 def test_basic(self): 137 def test_basic(self):
137 p = pointer(c_int(42)) 138 p = pointer(c_int(42))
138 # Although a pointer can be indexed, it has no length 139 # Although a pointer can be indexed, it has no length
139 self.assertRaises(TypeError, len, p) 140 self.assertRaises(TypeError, len, p)
140 self.assertEqual(p[0], 42) 141 self.assertEqual(p[0], 42)
141 self.assertEqual(p[0:1], [42]) 142 self.assertEqual(p[0:1], [42])
142 self.assertEqual(p.contents.value, 42) 143 self.assertEqual(p.contents.value, 42)
143 144
144 def test_charpp(self): 145 def test_charpp(self):
145 """Test that a character pointer-to-pointer is correctly passed""" 146 """Test that a character pointer-to-pointer is correctly passed"""
146 dll = CDLL(_ctypes_test.__file__) 147 dll = CDLL(getattr(_ctypes_test, '__file__', os.environ['TEST_EXECUTABLE ']))
147 func = dll._testfunc_c_p_p 148 func = dll._testfunc_c_p_p
148 func.restype = c_char_p 149 func.restype = c_char_p
149 argv = (c_char_p * 2)() 150 argv = (c_char_p * 2)()
150 argc = c_int( 2 ) 151 argc = c_int( 2 )
151 argv[0] = b'hello' 152 argv[0] = b'hello'
152 argv[1] = b'world' 153 argv[1] = b'world'
153 result = func( byref(argc), argv ) 154 result = func( byref(argc), argv )
154 self.assertEqual(result, b'world') 155 self.assertEqual(result, b'world')
155 156
156 def test_bug_1467852(self): 157 def test_bug_1467852(self):
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
209 P = POINTER(large_string) 210 P = POINTER(large_string)
210 self.assertTrue(P) 211 self.assertTrue(P)
211 212
212 # to not leak references, we must clean _pointer_type_cache 213 # to not leak references, we must clean _pointer_type_cache
213 from ctypes import _pointer_type_cache 214 from ctypes import _pointer_type_cache
214 del _pointer_type_cache[id(P)] 215 del _pointer_type_cache[id(P)]
215 216
216 217
217 if __name__ == '__main__': 218 if __name__ == '__main__':
218 unittest.main() 219 unittest.main()
OLDNEW
« no previous file with comments | « Lib/ctypes/test/test_pickling.py ('k') | Lib/ctypes/test/test_prototypes.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+