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

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

#countcontent
1n/afrom ctypes import *
2n/aimport os
3n/aimport sys
4n/aimport unittest
5n/aimport test.support
6n/afrom ctypes.util import find_library
7n/a
8n/alibc_name = None
9n/a
10n/adef setUpModule():
11n/a global libc_name
12n/a if os.name == "nt":
13n/a libc_name = find_library("c")
14n/a elif sys.platform == "cygwin":
15n/a libc_name = "cygwin1.dll"
16n/a else:
17n/a libc_name = find_library("c")
18n/a
19n/a if test.support.verbose:
20n/a print("libc_name is", libc_name)
21n/a
22n/aclass LoaderTest(unittest.TestCase):
23n/a
24n/a unknowndll = "xxrandomnamexx"
25n/a
26n/a def test_load(self):
27n/a if libc_name is None:
28n/a self.skipTest('could not find libc')
29n/a CDLL(libc_name)
30n/a CDLL(os.path.basename(libc_name))
31n/a self.assertRaises(OSError, CDLL, self.unknowndll)
32n/a
33n/a def test_load_version(self):
34n/a if libc_name is None:
35n/a self.skipTest('could not find libc')
36n/a if os.path.basename(libc_name) != 'libc.so.6':
37n/a self.skipTest('wrong libc path for test')
38n/a cdll.LoadLibrary("libc.so.6")
39n/a # linux uses version, libc 9 should not exist
40n/a self.assertRaises(OSError, cdll.LoadLibrary, "libc.so.9")
41n/a self.assertRaises(OSError, cdll.LoadLibrary, self.unknowndll)
42n/a
43n/a def test_find(self):
44n/a for name in ("c", "m"):
45n/a lib = find_library(name)
46n/a if lib:
47n/a cdll.LoadLibrary(lib)
48n/a CDLL(lib)
49n/a
50n/a @unittest.skipUnless(os.name == "nt",
51n/a 'test specific to Windows')
52n/a def test_load_library(self):
53n/a # CRT is no longer directly loadable. See issue23606 for the
54n/a # discussion about alternative approaches.
55n/a #self.assertIsNotNone(libc_name)
56n/a if test.support.verbose:
57n/a print(find_library("kernel32"))
58n/a print(find_library("user32"))
59n/a
60n/a if os.name == "nt":
61n/a windll.kernel32.GetModuleHandleW
62n/a windll["kernel32"].GetModuleHandleW
63n/a windll.LoadLibrary("kernel32").GetModuleHandleW
64n/a WinDLL("kernel32").GetModuleHandleW
65n/a
66n/a @unittest.skipUnless(os.name == "nt",
67n/a 'test specific to Windows')
68n/a def test_load_ordinal_functions(self):
69n/a import _ctypes_test
70n/a dll = WinDLL(_ctypes_test.__file__)
71n/a # We load the same function both via ordinal and name
72n/a func_ord = dll[2]
73n/a func_name = dll.GetString
74n/a # addressof gets the address where the function pointer is stored
75n/a a_ord = addressof(func_ord)
76n/a a_name = addressof(func_name)
77n/a f_ord_addr = c_void_p.from_address(a_ord).value
78n/a f_name_addr = c_void_p.from_address(a_name).value
79n/a self.assertEqual(hex(f_ord_addr), hex(f_name_addr))
80n/a
81n/a self.assertRaises(AttributeError, dll.__getitem__, 1234)
82n/a
83n/a @unittest.skipUnless(os.name == "nt", 'Windows-specific test')
84n/a def test_1703286_A(self):
85n/a from _ctypes import LoadLibrary, FreeLibrary
86n/a # On winXP 64-bit, advapi32 loads at an address that does
87n/a # NOT fit into a 32-bit integer. FreeLibrary must be able
88n/a # to accept this address.
89n/a
90n/a # These are tests for http://www.python.org/sf/1703286
91n/a handle = LoadLibrary("advapi32")
92n/a FreeLibrary(handle)
93n/a
94n/a @unittest.skipUnless(os.name == "nt", 'Windows-specific test')
95n/a def test_1703286_B(self):
96n/a # Since on winXP 64-bit advapi32 loads like described
97n/a # above, the (arbitrarily selected) CloseEventLog function
98n/a # also has a high address. 'call_function' should accept
99n/a # addresses so large.
100n/a from _ctypes import call_function
101n/a advapi32 = windll.advapi32
102n/a # Calling CloseEventLog with a NULL argument should fail,
103n/a # but the call should not segfault or so.
104n/a self.assertEqual(0, advapi32.CloseEventLog(None))
105n/a windll.kernel32.GetProcAddress.argtypes = c_void_p, c_char_p
106n/a windll.kernel32.GetProcAddress.restype = c_void_p
107n/a proc = windll.kernel32.GetProcAddress(advapi32._handle,
108n/a b"CloseEventLog")
109n/a self.assertTrue(proc)
110n/a # This is the real test: call the function via 'call_function'
111n/a self.assertEqual(0, call_function(proc, (None,)))
112n/a
113n/aif __name__ == "__main__":
114n/a unittest.main()