4
from ctypes.util import find_library
5
from ctypes.test import is_resource_enabled
9
libc_name = find_library("c")
12
elif sys.platform == "cygwin":
13
libc_name = "cygwin1.dll"
15
libc_name = find_library("c")
17
if is_resource_enabled("printing"):
18
print("libc_name is", libc_name)
20
class LoaderTest(unittest.TestCase):
22
unknowndll = "xxrandomnamexx"
24
if libc_name is not None:
27
CDLL(os.path.basename(libc_name))
28
self.assertRaises(OSError, CDLL, self.unknowndll)
30
if libc_name is not None and os.path.basename(libc_name) == "libc.so.6":
31
def test_load_version(self):
32
cdll.LoadLibrary("libc.so.6")
33
# linux uses version, libc 9 should not exist
34
self.assertRaises(OSError, cdll.LoadLibrary, "libc.so.9")
35
self.assertRaises(OSError, cdll.LoadLibrary, self.unknowndll)
38
for name in ("c", "m"):
39
lib = find_library(name)
44
if os.name in ("nt", "ce"):
45
def test_load_library(self):
46
self.failIf(libc_name is None)
47
if is_resource_enabled("printing"):
48
print(find_library("kernel32"))
49
print(find_library("user32"))
52
windll.kernel32.GetModuleHandleW
53
windll["kernel32"].GetModuleHandleW
54
windll.LoadLibrary("kernel32").GetModuleHandleW
55
WinDLL("kernel32").GetModuleHandleW
57
windll.coredll.GetModuleHandleW
58
windll["coredll"].GetModuleHandleW
59
windll.LoadLibrary("coredll").GetModuleHandleW
60
WinDLL("coredll").GetModuleHandleW
62
def test_load_ordinal_functions(self):
64
dll = WinDLL(_ctypes_test.__file__)
65
# We load the same function both via ordinal and name
67
func_name = dll.GetString
68
# addressof gets the address where the function pointer is stored
69
a_ord = addressof(func_ord)
70
a_name = addressof(func_name)
71
f_ord_addr = c_void_p.from_address(a_ord).value
72
f_name_addr = c_void_p.from_address(a_name).value
73
self.failUnlessEqual(hex(f_ord_addr), hex(f_name_addr))
75
self.failUnlessRaises(AttributeError, dll.__getitem__, 1234)
78
def test_1703286_A(self):
79
from _ctypes import LoadLibrary, FreeLibrary
80
# On winXP 64-bit, advapi32 loads at an address that does
81
# NOT fit into a 32-bit integer. FreeLibrary must be able
82
# to accept this address.
84
# These are tests for http://www.python.org/sf/1703286
85
handle = LoadLibrary("advapi32")
88
def test_1703286_B(self):
89
# Since on winXP 64-bit advapi32 loads like described
90
# above, the (arbitrarily selected) CloseEventLog function
91
# also has a high address. 'call_function' should accept
93
from _ctypes import call_function
94
advapi32 = windll.advapi32
95
# Calling CloseEventLog with a NULL argument should fail,
96
# but the call should not segfault or so.
97
self.failUnlessEqual(0, advapi32.CloseEventLog(None))
98
windll.kernel32.GetProcAddress.argtypes = c_void_p, c_char_p
99
windll.kernel32.GetProcAddress.restype = c_void_p
100
proc = windll.kernel32.GetProcAddress(advapi32._handle, "CloseEventLog")
101
self.failUnless(proc)
102
# This is the real test: call the function via 'call_function'
103
self.failUnlessEqual(0, call_function(proc, (None,)))
105
if __name__ == "__main__":