Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 1 | import unittest, sys |
| 2 | |
| 3 | class SimpleTypesTestCase(unittest.TestCase): |
| 4 | |
| 5 | def setUp(self): |
| 6 | import ctypes |
| 7 | try: |
| 8 | from _ctypes import set_conversion_mode |
| 9 | except ImportError: |
| 10 | pass |
| 11 | else: |
| 12 | self.prev_conv_mode = set_conversion_mode("ascii", "strict") |
| 13 | |
| 14 | def tearDown(self): |
| 15 | try: |
| 16 | from _ctypes import set_conversion_mode |
| 17 | except ImportError: |
| 18 | pass |
| 19 | else: |
| 20 | set_conversion_mode(*self.prev_conv_mode) |
Tim Peters | e8d09e5 | 2006-03-09 01:15:05 +0000 | [diff] [blame] | 21 | |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 22 | def test_subclasses(self): |
| 23 | from ctypes import c_void_p, c_char_p |
| 24 | # ctypes 0.9.5 and before did overwrite from_param in SimpleType_new |
| 25 | class CVOIDP(c_void_p): |
| 26 | def from_param(cls, value): |
| 27 | return value * 2 |
| 28 | from_param = classmethod(from_param) |
| 29 | |
| 30 | class CCHARP(c_char_p): |
| 31 | def from_param(cls, value): |
| 32 | return value * 4 |
| 33 | from_param = classmethod(from_param) |
| 34 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 35 | self.assertEqual(CVOIDP.from_param("abc"), "abcabc") |
| 36 | self.assertEqual(CCHARP.from_param("abc"), "abcabcabcabc") |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 37 | |
| 38 | try: |
| 39 | from ctypes import c_wchar_p |
| 40 | except ImportError: |
| 41 | return |
| 42 | |
| 43 | class CWCHARP(c_wchar_p): |
| 44 | def from_param(cls, value): |
| 45 | return value * 3 |
| 46 | from_param = classmethod(from_param) |
| 47 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 48 | self.assertEqual(CWCHARP.from_param("abc"), "abcabcabc") |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 49 | |
| 50 | # XXX Replace by c_char_p tests |
| 51 | def test_cstrings(self): |
| 52 | from ctypes import c_char_p, byref |
| 53 | |
| 54 | # c_char_p.from_param on a Python String packs the string |
| 55 | # into a cparam object |
Thomas Heller | 3d79dd9 | 2007-07-12 19:33:24 +0000 | [diff] [blame] | 56 | s = b"123" |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 57 | self.assertTrue(c_char_p.from_param(s)._obj is s) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 58 | |
| 59 | # new in 0.9.1: convert (encode) unicode to ascii |
Victor Stinner | cf44883 | 2010-07-28 00:15:03 +0000 | [diff] [blame] | 60 | self.assertEqual(c_char_p.from_param(b"123")._obj, b"123") |
| 61 | self.assertRaises(TypeError, c_char_p.from_param, "123\377") |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 62 | self.assertRaises(TypeError, c_char_p.from_param, 42) |
| 63 | |
| 64 | # calling c_char_p.from_param with a c_char_p instance |
| 65 | # returns the argument itself: |
Victor Stinner | 42746df | 2010-07-27 23:36:41 +0000 | [diff] [blame] | 66 | a = c_char_p(b"123") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 67 | self.assertTrue(c_char_p.from_param(a) is a) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 68 | |
| 69 | def test_cw_strings(self): |
Daniel Stutzbach | 061b14a | 2010-08-31 15:45:04 +0000 | [diff] [blame] | 70 | from ctypes import byref |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 71 | try: |
Daniel Stutzbach | 061b14a | 2010-08-31 15:45:04 +0000 | [diff] [blame] | 72 | from ctypes import c_wchar_p |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 73 | except ImportError: |
| 74 | ## print "(No c_wchar_p)" |
| 75 | return |
Daniel Stutzbach | 061b14a | 2010-08-31 15:45:04 +0000 | [diff] [blame] | 76 | |
Victor Stinner | d83f226 | 2011-09-28 13:36:20 +0200 | [diff] [blame] | 77 | c_wchar_p.from_param("123") |
| 78 | |
| 79 | self.assertRaises(TypeError, c_wchar_p.from_param, 42) |
Victor Stinner | cf44883 | 2010-07-28 00:15:03 +0000 | [diff] [blame] | 80 | self.assertRaises(TypeError, c_wchar_p.from_param, b"123\377") |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 81 | |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 82 | pa = c_wchar_p.from_param(c_wchar_p("123")) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 83 | self.assertEqual(type(pa), c_wchar_p) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 84 | |
| 85 | def test_int_pointers(self): |
| 86 | from ctypes import c_short, c_uint, c_int, c_long, POINTER, pointer |
| 87 | LPINT = POINTER(c_int) |
| 88 | |
| 89 | ## p = pointer(c_int(42)) |
| 90 | ## x = LPINT.from_param(p) |
| 91 | x = LPINT.from_param(pointer(c_int(42))) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 92 | self.assertEqual(x.contents.value, 42) |
| 93 | self.assertEqual(LPINT(c_int(42)).contents.value, 42) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 94 | |
Thomas Heller | 69b639f | 2009-09-18 20:08:39 +0000 | [diff] [blame] | 95 | self.assertEqual(LPINT.from_param(None), None) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 96 | |
| 97 | if c_int != c_long: |
| 98 | self.assertRaises(TypeError, LPINT.from_param, pointer(c_long(42))) |
| 99 | self.assertRaises(TypeError, LPINT.from_param, pointer(c_uint(42))) |
| 100 | self.assertRaises(TypeError, LPINT.from_param, pointer(c_short(42))) |
| 101 | |
| 102 | def test_byref_pointer(self): |
| 103 | # The from_param class method of POINTER(typ) classes accepts what is |
| 104 | # returned by byref(obj), it type(obj) == typ |
| 105 | from ctypes import c_short, c_uint, c_int, c_long, pointer, POINTER, byref |
| 106 | LPINT = POINTER(c_int) |
| 107 | |
| 108 | LPINT.from_param(byref(c_int(42))) |
| 109 | |
| 110 | self.assertRaises(TypeError, LPINT.from_param, byref(c_short(22))) |
| 111 | if c_int != c_long: |
| 112 | self.assertRaises(TypeError, LPINT.from_param, byref(c_long(22))) |
| 113 | self.assertRaises(TypeError, LPINT.from_param, byref(c_uint(22))) |
| 114 | |
| 115 | def test_byref_pointerpointer(self): |
| 116 | # See above |
| 117 | from ctypes import c_short, c_uint, c_int, c_long, pointer, POINTER, byref |
| 118 | |
| 119 | LPLPINT = POINTER(POINTER(c_int)) |
| 120 | LPLPINT.from_param(byref(pointer(c_int(42)))) |
| 121 | |
| 122 | self.assertRaises(TypeError, LPLPINT.from_param, byref(pointer(c_short(22)))) |
| 123 | if c_int != c_long: |
| 124 | self.assertRaises(TypeError, LPLPINT.from_param, byref(pointer(c_long(22)))) |
| 125 | self.assertRaises(TypeError, LPLPINT.from_param, byref(pointer(c_uint(22)))) |
| 126 | |
| 127 | def test_array_pointers(self): |
| 128 | from ctypes import c_short, c_uint, c_int, c_long, POINTER |
| 129 | INTARRAY = c_int * 3 |
| 130 | ia = INTARRAY() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 131 | self.assertEqual(len(ia), 3) |
| 132 | self.assertEqual([ia[i] for i in range(3)], [0, 0, 0]) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 133 | |
| 134 | # Pointers are only compatible with arrays containing items of |
| 135 | # the same type! |
| 136 | LPINT = POINTER(c_int) |
| 137 | LPINT.from_param((c_int*3)()) |
| 138 | self.assertRaises(TypeError, LPINT.from_param, c_short*3) |
| 139 | self.assertRaises(TypeError, LPINT.from_param, c_long*3) |
| 140 | self.assertRaises(TypeError, LPINT.from_param, c_uint*3) |
| 141 | |
| 142 | ## def test_performance(self): |
| 143 | ## check_perf() |
| 144 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 145 | def test_noctypes_argtype(self): |
| 146 | import _ctypes_test |
| 147 | from ctypes import CDLL, c_void_p, ArgumentError |
| 148 | |
| 149 | func = CDLL(_ctypes_test.__file__)._testfunc_p_p |
| 150 | func.restype = c_void_p |
| 151 | # TypeError: has no from_param method |
| 152 | self.assertRaises(TypeError, setattr, func, "argtypes", (object,)) |
| 153 | |
| 154 | class Adapter(object): |
| 155 | def from_param(cls, obj): |
| 156 | return None |
| 157 | |
| 158 | func.argtypes = (Adapter(),) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 159 | self.assertEqual(func(None), None) |
| 160 | self.assertEqual(func(object()), None) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 161 | |
| 162 | class Adapter(object): |
| 163 | def from_param(cls, obj): |
| 164 | return obj |
| 165 | |
| 166 | func.argtypes = (Adapter(),) |
| 167 | # don't know how to convert parameter 1 |
| 168 | self.assertRaises(ArgumentError, func, object()) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 169 | self.assertEqual(func(c_void_p(42)), 42) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 170 | |
| 171 | class Adapter(object): |
| 172 | def from_param(cls, obj): |
| 173 | raise ValueError(obj) |
| 174 | |
| 175 | func.argtypes = (Adapter(),) |
| 176 | # ArgumentError: argument 1: ValueError: 99 |
| 177 | self.assertRaises(ArgumentError, func, 99) |
| 178 | |
| 179 | |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 180 | ################################################################ |
| 181 | |
| 182 | if __name__ == '__main__': |
| 183 | unittest.main() |