Debian Codegraph @

lib-python/3/ctypes/test/test_values.py

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