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