/Lib/ctypes/test/test_loading.py

http://unladen-swallow.googlecode.com/ · Python · 106 lines · 77 code · 15 blank · 14 comment · 16 complexity · 215b4c73dd8ace52ef3498694b06555b MD5 · raw file

  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. libc_name = None
  7. if os.name == "nt":
  8. libc_name = find_library("c")
  9. elif os.name == "ce":
  10. libc_name = "coredll"
  11. elif sys.platform == "cygwin":
  12. libc_name = "cygwin1.dll"
  13. else:
  14. libc_name = find_library("c")
  15. if is_resource_enabled("printing"):
  16. print "libc_name is", libc_name
  17. class LoaderTest(unittest.TestCase):
  18. unknowndll = "xxrandomnamexx"
  19. if libc_name is not None:
  20. def test_load(self):
  21. CDLL(libc_name)
  22. CDLL(os.path.basename(libc_name))
  23. self.assertRaises(OSError, CDLL, self.unknowndll)
  24. if libc_name is not None and os.path.basename(libc_name) == "libc.so.6":
  25. def test_load_version(self):
  26. cdll.LoadLibrary("libc.so.6")
  27. # linux uses version, libc 9 should not exist
  28. self.assertRaises(OSError, cdll.LoadLibrary, "libc.so.9")
  29. self.assertRaises(OSError, cdll.LoadLibrary, self.unknowndll)
  30. def test_find(self):
  31. for name in ("c", "m"):
  32. lib = find_library(name)
  33. if lib:
  34. cdll.LoadLibrary(lib)
  35. CDLL(lib)
  36. if os.name in ("nt", "ce"):
  37. def test_load_library(self):
  38. self.failIf(libc_name is None)
  39. if is_resource_enabled("printing"):
  40. print find_library("kernel32")
  41. print find_library("user32")
  42. if os.name == "nt":
  43. windll.kernel32.GetModuleHandleW
  44. windll["kernel32"].GetModuleHandleW
  45. windll.LoadLibrary("kernel32").GetModuleHandleW
  46. WinDLL("kernel32").GetModuleHandleW
  47. elif os.name == "ce":
  48. windll.coredll.GetModuleHandleW
  49. windll["coredll"].GetModuleHandleW
  50. windll.LoadLibrary("coredll").GetModuleHandleW
  51. WinDLL("coredll").GetModuleHandleW
  52. def test_load_ordinal_functions(self):
  53. import _ctypes_test
  54. dll = WinDLL(_ctypes_test.__file__)
  55. # We load the same function both via ordinal and name
  56. func_ord = dll[2]
  57. func_name = dll.GetString
  58. # addressof gets the address where the function pointer is stored
  59. a_ord = addressof(func_ord)
  60. a_name = addressof(func_name)
  61. f_ord_addr = c_void_p.from_address(a_ord).value
  62. f_name_addr = c_void_p.from_address(a_name).value
  63. self.failUnlessEqual(hex(f_ord_addr), hex(f_name_addr))
  64. self.failUnlessRaises(AttributeError, dll.__getitem__, 1234)
  65. if os.name == "nt":
  66. def test_1703286_A(self):
  67. from _ctypes import LoadLibrary, FreeLibrary
  68. # On winXP 64-bit, advapi32 loads at an address that does
  69. # NOT fit into a 32-bit integer. FreeLibrary must be able
  70. # to accept this address.
  71. # These are tests for http://www.python.org/sf/1703286
  72. handle = LoadLibrary("advapi32")
  73. FreeLibrary(handle)
  74. def test_1703286_B(self):
  75. # Since on winXP 64-bit advapi32 loads like described
  76. # above, the (arbitrarily selected) CloseEventLog function
  77. # also has a high address. 'call_function' should accept
  78. # addresses so large.
  79. from _ctypes import call_function
  80. advapi32 = windll.advapi32
  81. # Calling CloseEventLog with a NULL argument should fail,
  82. # but the call should not segfault or so.
  83. self.failUnlessEqual(0, advapi32.CloseEventLog(None))
  84. windll.kernel32.GetProcAddress.argtypes = c_void_p, c_char_p
  85. windll.kernel32.GetProcAddress.restype = c_void_p
  86. proc = windll.kernel32.GetProcAddress(advapi32._handle, "CloseEventLog")
  87. self.failUnless(proc)
  88. # This is the real test: call the function via 'call_function'
  89. self.failUnlessEqual(0, call_function(proc, (None,)))
  90. if __name__ == "__main__":
  91. unittest.main()