1 | n/a | """Test where byte objects are accepted""" |
---|
2 | n/a | import unittest |
---|
3 | n/a | import sys |
---|
4 | n/a | from ctypes import * |
---|
5 | n/a | |
---|
6 | n/a | class BytesTest(unittest.TestCase): |
---|
7 | n/a | def test_c_char(self): |
---|
8 | n/a | x = c_char(b"x") |
---|
9 | n/a | self.assertRaises(TypeError, c_char, "x") |
---|
10 | n/a | x.value = b"y" |
---|
11 | n/a | with self.assertRaises(TypeError): |
---|
12 | n/a | x.value = "y" |
---|
13 | n/a | c_char.from_param(b"x") |
---|
14 | n/a | self.assertRaises(TypeError, c_char.from_param, "x") |
---|
15 | n/a | (c_char * 3)(b"a", b"b", b"c") |
---|
16 | n/a | self.assertRaises(TypeError, c_char * 3, "a", "b", "c") |
---|
17 | n/a | |
---|
18 | n/a | def test_c_wchar(self): |
---|
19 | n/a | x = c_wchar("x") |
---|
20 | n/a | self.assertRaises(TypeError, c_wchar, b"x") |
---|
21 | n/a | x.value = "y" |
---|
22 | n/a | with self.assertRaises(TypeError): |
---|
23 | n/a | x.value = b"y" |
---|
24 | n/a | c_wchar.from_param("x") |
---|
25 | n/a | self.assertRaises(TypeError, c_wchar.from_param, b"x") |
---|
26 | n/a | (c_wchar * 3)("a", "b", "c") |
---|
27 | n/a | self.assertRaises(TypeError, c_wchar * 3, b"a", b"b", b"c") |
---|
28 | n/a | |
---|
29 | n/a | def test_c_char_p(self): |
---|
30 | n/a | c_char_p(b"foo bar") |
---|
31 | n/a | self.assertRaises(TypeError, c_char_p, "foo bar") |
---|
32 | n/a | |
---|
33 | n/a | def test_c_wchar_p(self): |
---|
34 | n/a | c_wchar_p("foo bar") |
---|
35 | n/a | self.assertRaises(TypeError, c_wchar_p, b"foo bar") |
---|
36 | n/a | |
---|
37 | n/a | def test_struct(self): |
---|
38 | n/a | class X(Structure): |
---|
39 | n/a | _fields_ = [("a", c_char * 3)] |
---|
40 | n/a | |
---|
41 | n/a | x = X(b"abc") |
---|
42 | n/a | self.assertRaises(TypeError, X, "abc") |
---|
43 | n/a | self.assertEqual(x.a, b"abc") |
---|
44 | n/a | self.assertEqual(type(x.a), bytes) |
---|
45 | n/a | |
---|
46 | n/a | def test_struct_W(self): |
---|
47 | n/a | class X(Structure): |
---|
48 | n/a | _fields_ = [("a", c_wchar * 3)] |
---|
49 | n/a | |
---|
50 | n/a | x = X("abc") |
---|
51 | n/a | self.assertRaises(TypeError, X, b"abc") |
---|
52 | n/a | self.assertEqual(x.a, "abc") |
---|
53 | n/a | self.assertEqual(type(x.a), str) |
---|
54 | n/a | |
---|
55 | n/a | @unittest.skipUnless(sys.platform == "win32", 'Windows-specific test') |
---|
56 | n/a | def test_BSTR(self): |
---|
57 | n/a | from _ctypes import _SimpleCData |
---|
58 | n/a | class BSTR(_SimpleCData): |
---|
59 | n/a | _type_ = "X" |
---|
60 | n/a | |
---|
61 | n/a | BSTR("abc") |
---|
62 | n/a | |
---|
63 | n/a | |
---|
64 | n/a | if __name__ == '__main__': |
---|
65 | n/a | unittest.main() |
---|