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

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

#countcontent
1n/aimport unittest
2n/afrom ctypes import *
3n/a
4n/aclass MyInt(c_int):
5n/a def __eq__(self, other):
6n/a if type(other) != MyInt:
7n/a return NotImplementedError
8n/a return self.value == other.value
9n/a
10n/aclass Test(unittest.TestCase):
11n/a
12n/a def test_compare(self):
13n/a self.assertEqual(MyInt(3), MyInt(3))
14n/a self.assertNotEqual(MyInt(42), MyInt(43))
15n/a
16n/a def test_ignore_retval(self):
17n/a # Test if the return value of a callback is ignored
18n/a # if restype is None
19n/a proto = CFUNCTYPE(None)
20n/a def func():
21n/a return (1, "abc", None)
22n/a
23n/a cb = proto(func)
24n/a self.assertEqual(None, cb())
25n/a
26n/a
27n/a def test_int_callback(self):
28n/a args = []
29n/a def func(arg):
30n/a args.append(arg)
31n/a return arg
32n/a
33n/a cb = CFUNCTYPE(None, MyInt)(func)
34n/a
35n/a self.assertEqual(None, cb(42))
36n/a self.assertEqual(type(args[-1]), MyInt)
37n/a
38n/a cb = CFUNCTYPE(c_int, c_int)(func)
39n/a
40n/a self.assertEqual(42, cb(42))
41n/a self.assertEqual(type(args[-1]), int)
42n/a
43n/a def test_int_struct(self):
44n/a class X(Structure):
45n/a _fields_ = [("x", MyInt)]
46n/a
47n/a self.assertEqual(X().x, MyInt())
48n/a
49n/a s = X()
50n/a s.x = MyInt(42)
51n/a
52n/a self.assertEqual(s.x, MyInt(42))
53n/a
54n/aif __name__ == "__main__":
55n/a unittest.main()