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

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

#countcontent
1n/a"""
2n/aA testcase which accesses *values* in a dll.
3n/a"""
4n/a
5n/aimport unittest
6n/aimport sys
7n/afrom ctypes import *
8n/a
9n/aimport _ctypes_test
10n/a
11n/aclass ValuesTestCase(unittest.TestCase):
12n/a
13n/a def test_an_integer(self):
14n/a # This test checks and changes an integer stored inside the
15n/a # _ctypes_test dll/shared lib.
16n/a ctdll = CDLL(_ctypes_test.__file__)
17n/a an_integer = c_int.in_dll(ctdll, "an_integer")
18n/a x = an_integer.value
19n/a self.assertEqual(x, ctdll.get_an_integer())
20n/a an_integer.value *= 2
21n/a self.assertEqual(x*2, ctdll.get_an_integer())
22n/a # To avoid test failures when this test is repeated several
23n/a # times the original value must be restored
24n/a an_integer.value = x
25n/a self.assertEqual(x, ctdll.get_an_integer())
26n/a
27n/a def test_undefined(self):
28n/a ctdll = CDLL(_ctypes_test.__file__)
29n/a self.assertRaises(ValueError, c_int.in_dll, ctdll, "Undefined_Symbol")
30n/a
31n/aclass PythonValuesTestCase(unittest.TestCase):
32n/a """This test only works when python itself is a dll/shared library"""
33n/a
34n/a def test_optimizeflag(self):
35n/a # This test accesses the Py_OptimizeFlag integer, which is
36n/a # exported by the Python dll and should match the sys.flags value
37n/a
38n/a opt = c_int.in_dll(pythonapi, "Py_OptimizeFlag").value
39n/a self.assertEqual(opt, sys.flags.optimize)
40n/a
41n/a def test_frozentable(self):
42n/a # Python exports a PyImport_FrozenModules symbol. This is a
43n/a # pointer to an array of struct _frozen entries. The end of the
44n/a # array is marked by an entry containing a NULL name and zero
45n/a # size.
46n/a
47n/a # In standard Python, this table contains a __hello__
48n/a # module, and a __phello__ package containing a spam
49n/a # module.
50n/a class struct_frozen(Structure):
51n/a _fields_ = [("name", c_char_p),
52n/a ("code", POINTER(c_ubyte)),
53n/a ("size", c_int)]
54n/a FrozenTable = POINTER(struct_frozen)
55n/a
56n/a ft = FrozenTable.in_dll(pythonapi, "PyImport_FrozenModules")
57n/a # ft is a pointer to the struct_frozen entries:
58n/a items = []
59n/a # _frozen_importlib changes size whenever importlib._bootstrap
60n/a # changes, so it gets a special case. We should make sure it's
61n/a # found, but don't worry about its size too much. The same
62n/a # applies to _frozen_importlib_external.
63n/a bootstrap_seen = []
64n/a bootstrap_expected = [
65n/a b'_frozen_importlib',
66n/a b'_frozen_importlib_external',
67n/a ]
68n/a for entry in ft:
69n/a # This is dangerous. We *can* iterate over a pointer, but
70n/a # the loop will not terminate (maybe with an access
71n/a # violation;-) because the pointer instance has no size.
72n/a if entry.name is None:
73n/a break
74n/a
75n/a if entry.name in bootstrap_expected:
76n/a bootstrap_seen.append(entry.name)
77n/a self.assertTrue(entry.size,
78n/a "{!r} was reported as having no size".format(entry.name))
79n/a continue
80n/a items.append((entry.name.decode("ascii"), entry.size))
81n/a
82n/a expected = [("__hello__", 139),
83n/a ("__phello__", -139),
84n/a ("__phello__.spam", 139),
85n/a ]
86n/a self.assertEqual(items, expected, "PyImport_FrozenModules example "
87n/a "in Doc/library/ctypes.rst may be out of date")
88n/a
89n/a self.assertEqual(sorted(bootstrap_seen), bootstrap_expected,
90n/a "frozen bootstrap modules did not match PyImport_FrozenModules")
91n/a
92n/a from ctypes import _pointer_type_cache
93n/a del _pointer_type_cache[struct_frozen]
94n/a
95n/a def test_undefined(self):
96n/a self.assertRaises(ValueError, c_int.in_dll, pythonapi,
97n/a "Undefined_Symbol")
98n/a
99n/aif __name__ == '__main__':
100n/a unittest.main()