| 1 |
from ctypes import * |
| 2 |
import sys, unittest |
| 3 |
import os |
| 4 |
from ctypes.util import find_library |
| 5 |
from ctypes.test import is_resource_enabled |
| 6 |
import test.test_support as support |
| 7 |
|
| 8 |
libc_name = None |
| 9 |
if os.name == "nt": |
| 10 |
libc_name = find_library("c") |
| 11 |
elif os.name == "ce": |
| 12 |
libc_name = "coredll" |
| 13 |
elif sys.platform == "cygwin": |
| 14 |
libc_name = "cygwin1.dll" |
| 15 |
else: |
| 16 |
libc_name = find_library("c") |
| 17 |
|
| 18 |
if is_resource_enabled("printing"): |
| 19 |
print "libc_name is", libc_name |
| 20 |
|
| 21 |
class LoaderTest(unittest.TestCase): |
| 22 |
|
| 23 |
unknowndll = "xxrandomnamexx" |
| 24 |
|
| 25 |
@unittest.skipUnless(libc_name is not None, 'could not find libc') |
| 26 |
def test_load(self): |
| 27 |
CDLL(libc_name) |
| 28 |
CDLL(os.path.basename(libc_name)) |
| 29 |
self.assertRaises(OSError, CDLL, self.unknowndll) |
| 30 |
|
| 31 |
@support.requires_unicode |
| 32 |
@unittest.skipUnless(libc_name is not None, 'could not find libc') |
| 33 |
def test_load_unicode(self): |
| 34 |
CDLL(unicode(libc_name)) |
| 35 |
self.assertRaises(OSError, CDLL, unicode(self.unknowndll)) |
| 36 |
|
| 37 |
@unittest.skipUnless(libc_name is not None, 'could not find libc') |
| 38 |
@unittest.skipUnless(libc_name is not None and |
| 39 |
os.path.basename(libc_name) == "libc.so.6", |
| 40 |
'wrong libc path for test') |
| 41 |
def test_load_version(self): |
| 42 |
cdll.LoadLibrary("libc.so.6") |
| 43 |
|
| 44 |
self.assertRaises(OSError, cdll.LoadLibrary, "libc.so.9") |
| 45 |
self.assertRaises(OSError, cdll.LoadLibrary, self.unknowndll) |
| 46 |
|
| 47 |
def test_find(self): |
| 48 |
for name in ("c", "m"): |
| 49 |
lib = find_library(name) |
| 50 |
if lib: |
| 51 |
cdll.LoadLibrary(lib) |
| 52 |
CDLL(lib) |
| 53 |
|
| 54 |
@unittest.skipUnless(os.name in ("nt", "ce"), |
| 55 |
'test specific to Windows (NT/CE)') |
| 56 |
def test_load_library(self): |
| 57 |
self.assertIsNotNone(libc_name) |
| 58 |
if is_resource_enabled("printing"): |
| 59 |
print find_library("kernel32") |
| 60 |
print find_library("user32") |
| 61 |
|
| 62 |
if os.name == "nt": |
| 63 |
windll.kernel32.GetModuleHandleW |
| 64 |
windll["kernel32"].GetModuleHandleW |
| 65 |
windll.LoadLibrary("kernel32").GetModuleHandleW |
| 66 |
WinDLL("kernel32").GetModuleHandleW |
| 67 |
elif os.name == "ce": |
| 68 |
windll.coredll.GetModuleHandleW |
| 69 |
windll["coredll"].GetModuleHandleW |
| 70 |
windll.LoadLibrary("coredll").GetModuleHandleW |
| 71 |
WinDLL("coredll").GetModuleHandleW |
| 72 |
|
| 73 |
@unittest.skipUnless(os.name in ("nt", "ce"), |
| 74 |
'test specific to Windows (NT/CE)') |
| 75 |
def test_load_ordinal_functions(self): |
| 76 |
import _ctypes_test |
| 77 |
dll = WinDLL(_ctypes_test.__file__) |
| 78 |
|
| 79 |
func_ord = dll[2] |
| 80 |
func_name = dll.GetString |
| 81 |
|
| 82 |
a_ord = addressof(func_ord) |
| 83 |
a_name = addressof(func_name) |
| 84 |
f_ord_addr = c_void_p.from_address(a_ord).value |
| 85 |
f_name_addr = c_void_p.from_address(a_name).value |
| 86 |
self.assertEqual(hex(f_ord_addr), hex(f_name_addr)) |
| 87 |
|
| 88 |
self.assertRaises(AttributeError, dll.__getitem__, 1234) |
| 89 |
|
| 90 |
@unittest.skipUnless(os.name == "nt", 'Windows-specific test') |
| 91 |
def test_1703286_A(self): |
| 92 |
from _ctypes import LoadLibrary, FreeLibrary |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
handle = LoadLibrary("advapi32") |
| 99 |
FreeLibrary(handle) |
| 100 |
|
| 101 |
@unittest.skipUnless(os.name == "nt", 'Windows-specific test') |
| 102 |
def test_1703286_B(self): |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
from _ctypes import call_function |
| 108 |
advapi32 = windll.advapi32 |
| 109 |
|
| 110 |
|
| 111 |
self.assertEqual(0, advapi32.CloseEventLog(None)) |
| 112 |
windll.kernel32.GetProcAddress.argtypes = c_void_p, c_char_p |
| 113 |
windll.kernel32.GetProcAddress.restype = c_void_p |
| 114 |
proc = windll.kernel32.GetProcAddress(advapi32._handle, |
| 115 |
"CloseEventLog") |
| 116 |
self.assertTrue(proc) |
| 117 |
|
| 118 |
self.assertEqual(0, call_function(proc, (None,))) |
| 119 |
|
| 120 |
if __name__ == "__main__": |
| 121 |
unittest.main() |