|
4 | 4 | import unittest |
5 | 5 | from ctypes import (CDLL, Structure, Array, CFUNCTYPE, |
6 | 6 | byref, POINTER, pointer, ArgumentError, |
7 | | - c_char, c_wchar, c_byte, c_char_p, |
8 | | - c_short, c_int, c_long, c_longlong, |
| 7 | + c_char, c_wchar, c_byte, c_char_p, c_wchar_p, |
| 8 | + c_short, c_int, c_long, c_longlong, c_void_p, |
9 | 9 | c_float, c_double, c_longdouble) |
10 | 10 | from _ctypes import _Pointer, _SimpleCData |
11 | 11 |
|
@@ -92,6 +92,54 @@ def test_wchar_parm(self): |
92 | 92 | "argument 2: TypeError: one character unicode string " |
93 | 93 | "expected") |
94 | 94 |
|
| 95 | + def test_c_char_p_parm(self): |
| 96 | + """Test the error message when converting an incompatible type to c_char_p.""" |
| 97 | + proto = CFUNCTYPE(c_int, c_char_p) |
| 98 | + def callback(*args): |
| 99 | + return 0 |
| 100 | + |
| 101 | + callback = proto(callback) |
| 102 | + self.assertEqual(callback(b"abc"), 0) |
| 103 | + |
| 104 | + with self.assertRaises(ArgumentError) as cm: |
| 105 | + callback(10) |
| 106 | + |
| 107 | + self.assertEqual(str(cm.exception), |
| 108 | + "argument 1: TypeError: 'int' object cannot be " |
| 109 | + "interpreted as ctypes.c_char_p") |
| 110 | + |
| 111 | + def test_c_wchar_p_parm(self): |
| 112 | + """Test the error message when converting an incompatible type to c_wchar_p.""" |
| 113 | + proto = CFUNCTYPE(c_int, c_wchar_p) |
| 114 | + def callback(*args): |
| 115 | + return 0 |
| 116 | + |
| 117 | + callback = proto(callback) |
| 118 | + self.assertEqual(callback("abc"), 0) |
| 119 | + |
| 120 | + with self.assertRaises(ArgumentError) as cm: |
| 121 | + callback(10) |
| 122 | + |
| 123 | + self.assertEqual(str(cm.exception), |
| 124 | + "argument 1: TypeError: 'int' object cannot be " |
| 125 | + "interpreted as ctypes.c_wchar_p") |
| 126 | + |
| 127 | + def test_c_void_p_parm(self): |
| 128 | + """Test the error message when converting an incompatible type to c_void_p.""" |
| 129 | + proto = CFUNCTYPE(c_int, c_void_p) |
| 130 | + def callback(*args): |
| 131 | + return 0 |
| 132 | + |
| 133 | + callback = proto(callback) |
| 134 | + self.assertEqual(callback(5), 0) |
| 135 | + |
| 136 | + with self.assertRaises(ArgumentError) as cm: |
| 137 | + callback(2.5) |
| 138 | + |
| 139 | + self.assertEqual(str(cm.exception), |
| 140 | + "argument 1: TypeError: 'float' object cannot be " |
| 141 | + "interpreted as ctypes.c_void_p") |
| 142 | + |
95 | 143 | def test_wchar_result(self): |
96 | 144 | f = dll._testfunc_i_bhilfd |
97 | 145 | f.argtypes = [c_byte, c_short, c_int, c_long, c_float, c_double] |
|
0 commit comments