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

Side by Side Diff: Lib/ctypes/test/test_returnfuncptrs.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_refcounts.py ('k') | Lib/ctypes/test/test_slicing.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 unittest 1 import unittest
2 from ctypes import * 2 from ctypes import *
3 import os 3 import os
4 4
5 import _ctypes_test 5 import _ctypes_test
6 6
7 class ReturnFuncPtrTestCase(unittest.TestCase): 7 class ReturnFuncPtrTestCase(unittest.TestCase):
8 8
9 def test_with_prototype(self): 9 def test_with_prototype(self):
10 # The _ctypes_test shared lib/dll exports quite some functions for testi ng. 10 # The _ctypes_test shared lib/dll exports quite some functions for testi ng.
11 # The get_strchr function returns a *pointer* to the C strchr function. 11 # The get_strchr function returns a *pointer* to the C strchr function.
12 dll = CDLL(_ctypes_test.__file__) 12 dll = CDLL(getattr(_ctypes_test, '__file__', os.environ['TEST_EXECUTABLE ']))
13 get_strchr = dll.get_strchr 13 get_strchr = dll.get_strchr
14 get_strchr.restype = CFUNCTYPE(c_char_p, c_char_p, c_char) 14 get_strchr.restype = CFUNCTYPE(c_char_p, c_char_p, c_char)
15 strchr = get_strchr() 15 strchr = get_strchr()
16 self.assertEqual(strchr(b"abcdef", b"b"), b"bcdef") 16 self.assertEqual(strchr(b"abcdef", b"b"), b"bcdef")
17 self.assertEqual(strchr(b"abcdef", b"x"), None) 17 self.assertEqual(strchr(b"abcdef", b"x"), None)
18 self.assertEqual(strchr(b"abcdef", 98), b"bcdef") 18 self.assertEqual(strchr(b"abcdef", 98), b"bcdef")
19 self.assertEqual(strchr(b"abcdef", 107), None) 19 self.assertEqual(strchr(b"abcdef", 107), None)
20 self.assertRaises(ArgumentError, strchr, b"abcdef", 3.0) 20 self.assertRaises(ArgumentError, strchr, b"abcdef", 3.0)
21 self.assertRaises(TypeError, strchr, b"abcdef") 21 self.assertRaises(TypeError, strchr, b"abcdef")
22 22
23 def test_without_prototype(self): 23 def test_without_prototype(self):
24 dll = CDLL(_ctypes_test.__file__) 24 dll = CDLL(getattr(_ctypes_test, '__file__', os.environ['TEST_EXECUTABLE ']))
25 get_strchr = dll.get_strchr 25 get_strchr = dll.get_strchr
26 # the default 'c_int' would not work on systems where sizeof(int) != siz eof(void *) 26 # the default 'c_int' would not work on systems where sizeof(int) != siz eof(void *)
27 get_strchr.restype = c_void_p 27 get_strchr.restype = c_void_p
28 addr = get_strchr() 28 addr = get_strchr()
29 # _CFuncPtr instances are now callable with an integer argument 29 # _CFuncPtr instances are now callable with an integer argument
30 # which denotes a function address: 30 # which denotes a function address:
31 strchr = CFUNCTYPE(c_char_p, c_char_p, c_char)(addr) 31 strchr = CFUNCTYPE(c_char_p, c_char_p, c_char)(addr)
32 self.assertTrue(strchr(b"abcdef", b"b"), "bcdef") 32 self.assertTrue(strchr(b"abcdef", b"b"), "bcdef")
33 self.assertEqual(strchr(b"abcdef", b"x"), None) 33 self.assertEqual(strchr(b"abcdef", b"x"), None)
34 self.assertRaises(ArgumentError, strchr, b"abcdef", 3.0) 34 self.assertRaises(ArgumentError, strchr, b"abcdef", 3.0)
35 self.assertRaises(TypeError, strchr, b"abcdef") 35 self.assertRaises(TypeError, strchr, b"abcdef")
36 36
37 def test_from_dll(self): 37 def test_from_dll(self):
38 dll = CDLL(_ctypes_test.__file__) 38 dll = CDLL(getattr(_ctypes_test, '__file__', os.environ['TEST_EXECUTABLE ']))
39 # _CFuncPtr instances are now callable with a tuple argument 39 # _CFuncPtr instances are now callable with a tuple argument
40 # which denotes a function name and a dll: 40 # which denotes a function name and a dll:
41 strchr = CFUNCTYPE(c_char_p, c_char_p, c_char)(("my_strchr", dll)) 41 strchr = CFUNCTYPE(c_char_p, c_char_p, c_char)(("my_strchr", dll))
42 self.assertTrue(strchr(b"abcdef", b"b"), "bcdef") 42 self.assertTrue(strchr(b"abcdef", b"b"), "bcdef")
43 self.assertEqual(strchr(b"abcdef", b"x"), None) 43 self.assertEqual(strchr(b"abcdef", b"x"), None)
44 self.assertRaises(ArgumentError, strchr, b"abcdef", 3.0) 44 self.assertRaises(ArgumentError, strchr, b"abcdef", 3.0)
45 self.assertRaises(TypeError, strchr, b"abcdef") 45 self.assertRaises(TypeError, strchr, b"abcdef")
46 46
47 # Issue 6083: Reference counting bug 47 # Issue 6083: Reference counting bug
48 def test_from_dll_refcount(self): 48 def test_from_dll_refcount(self):
49 class BadSequence(tuple): 49 class BadSequence(tuple):
50 def __getitem__(self, key): 50 def __getitem__(self, key):
51 if key == 0: 51 if key == 0:
52 return "my_strchr" 52 return "my_strchr"
53 if key == 1: 53 if key == 1:
54 return CDLL(_ctypes_test.__file__) 54 return CDLL(getattr(_ctypes_test, '__file__', os.environ['TE ST_EXECUTABLE']))
55 raise IndexError 55 raise IndexError
56 56
57 # _CFuncPtr instances are now callable with a tuple argument 57 # _CFuncPtr instances are now callable with a tuple argument
58 # which denotes a function name and a dll: 58 # which denotes a function name and a dll:
59 strchr = CFUNCTYPE(c_char_p, c_char_p, c_char)( 59 strchr = CFUNCTYPE(c_char_p, c_char_p, c_char)(
60 BadSequence(("my_strchr", CDLL(_ctypes_test.__file__)))) 60 BadSequence(("my_strchr", CDLL(getattr(_ctypes_test, '__file__', os.environ['TEST_EXECUTABLE'])))))
61 self.assertTrue(strchr(b"abcdef", b"b"), "bcdef") 61 self.assertTrue(strchr(b"abcdef", b"b"), "bcdef")
62 self.assertEqual(strchr(b"abcdef", b"x"), None) 62 self.assertEqual(strchr(b"abcdef", b"x"), None)
63 self.assertRaises(ArgumentError, strchr, b"abcdef", 3.0) 63 self.assertRaises(ArgumentError, strchr, b"abcdef", 3.0)
64 self.assertRaises(TypeError, strchr, b"abcdef") 64 self.assertRaises(TypeError, strchr, b"abcdef")
65 65
66 if __name__ == "__main__": 66 if __name__ == "__main__":
67 unittest.main() 67 unittest.main()
OLDNEW
« no previous file with comments | « Lib/ctypes/test/test_refcounts.py ('k') | Lib/ctypes/test/test_slicing.py » ('j') | no next file with comments »

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