Debian Codegraph @

Lib/test/test_ctypes/test_values.py

raw
1 """
2 A testcase which accesses *values* in a dll.
3 """
4
5 import _imp
6 import importlib.util
7 import sys
8 import unittest
9 from ctypes import (Structure, CDLL, POINTER, pythonapi,
10 _pointer_type_cache,
11 c_ubyte, c_char_p, c_int)
12 from test.support import import_helper
13
14
15 class ValuesTestCase(unittest.TestCase):
16
17 def setUp(self):
18 _ctypes_test = import_helper.import_module("_ctypes_test")
19 self.ctdll = CDLL(_ctypes_test.__file__)
20
21 def test_an_integer(self):
22 # This test checks and changes an integer stored inside the
23 # _ctypes_test dll/shared lib.
24 ctdll = self.ctdll
25 an_integer = c_int.in_dll(ctdll, "an_integer")
26 x = an_integer.value
27 self.assertEqual(x, ctdll.get_an_integer())
28 an_integer.value *= 2
29 self.assertEqual(x*2, ctdll.get_an_integer())
30 # To avoid test failures when this test is repeated several
31 # times the original value must be restored
32 an_integer.value = x
33 self.assertEqual(x, ctdll.get_an_integer())
34
35 def test_undefined(self):
36 self.assertRaises(ValueError, c_int.in_dll, self.ctdll, "Undefined_Symbol")
37
38
39 class PythonValuesTestCase(unittest.TestCase):
40 """This test only works when python itself is a dll/shared library"""
41
42 def test_optimizeflag(self):
43 # This test accesses the Py_OptimizeFlag integer, which is
44 # exported by the Python dll and should match the sys.flags value
45
46 opt = c_int.in_dll(pythonapi, "Py_OptimizeFlag").value
47 self.assertEqual(opt, sys.flags.optimize)
48
49 def test_frozentable(self):
50 # Python exports a PyImport_FrozenModules symbol. This is a
51 # pointer to an array of struct _frozen entries. The end of the
52 # array is marked by an entry containing a NULL name and zero
53 # size.
54
55 # In standard Python, this table contains a __hello__
56 # module, and a __phello__ package containing a spam
57 # module.
58 class struct_frozen(Structure):
59 _fields_ = [("name", c_char_p),
60 ("code", POINTER(c_ubyte)),
61 ("size", c_int),
62 ("is_package", c_int),
63 ]
64 FrozenTable = POINTER(struct_frozen)
65
66 modules = []
67 for group in ["Bootstrap", "Stdlib", "Test"]:
68 ft = FrozenTable.in_dll(pythonapi, f"_PyImport_Frozen{group}")
69 # ft is a pointer to the struct_frozen entries:
70 for entry in ft:
71 # This is dangerous. We *can* iterate over a pointer, but
72 # the loop will not terminate (maybe with an access
73 # violation;-) because the pointer instance has no size.
74 if entry.name is None:
75 break
76 modname = entry.name.decode("ascii")
77 modules.append(modname)
78 with self.subTest(modname):
79 if entry.size != 0:
80 # Do a sanity check on entry.size and entry.code.
81 self.assertGreater(abs(entry.size), 10)
82 self.assertTrue([entry.code[i] for i in range(abs(entry.size))])
83 # Check the module's package-ness.
84 with import_helper.frozen_modules():
85 spec = importlib.util.find_spec(modname)
86 if entry.is_package:
87 # It's a package.
88 self.assertIsNotNone(spec.submodule_search_locations)
89 else:
90 self.assertIsNone(spec.submodule_search_locations)
91
92 with import_helper.frozen_modules():
93 expected = _imp._frozen_module_names()
94 self.maxDiff = None
95 self.assertEqual(modules, expected,
96 "_PyImport_FrozenBootstrap example "
97 "in Doc/library/ctypes.rst may be out of date")
98
99 del _pointer_type_cache[struct_frozen]
100
101 def test_undefined(self):
102 self.assertRaises(ValueError, c_int.in_dll, pythonapi,
103 "Undefined_Symbol")
104
105
106 if __name__ == '__main__':
107 unittest.main()