ยปCore Development>Code coverage>Lib/ctypes/test/test_bytes.py

Python code coverage for Lib/ctypes/test/test_bytes.py

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