/Lib/ctypes/test/test_find.py

http://unladen-swallow.googlecode.com/ · Python · 82 lines · 49 code · 11 blank · 22 comment · 20 complexity · 72dee0b10271e3c841383a9d5c21b8f4 MD5 · raw file

  1. import unittest
  2. import sys
  3. from ctypes import *
  4. from ctypes.util import find_library
  5. from ctypes.test import is_resource_enabled
  6. if sys.platform == "win32":
  7. lib_gl = find_library("OpenGL32")
  8. lib_glu = find_library("Glu32")
  9. lib_gle = None
  10. elif sys.platform == "darwin":
  11. lib_gl = lib_glu = find_library("OpenGL")
  12. lib_gle = None
  13. else:
  14. lib_gl = find_library("GL")
  15. lib_glu = find_library("GLU")
  16. lib_gle = find_library("gle")
  17. ## print, for debugging
  18. if is_resource_enabled("printing"):
  19. if lib_gl or lib_glu or lib_gle:
  20. print "OpenGL libraries:"
  21. for item in (("GL", lib_gl),
  22. ("GLU", lib_glu),
  23. ("gle", lib_gle)):
  24. print "\t", item
  25. # On some systems, loading the OpenGL libraries needs the RTLD_GLOBAL mode.
  26. class Test_OpenGL_libs(unittest.TestCase):
  27. def setUp(self):
  28. self.gl = self.glu = self.gle = None
  29. if lib_gl:
  30. self.gl = CDLL(lib_gl, mode=RTLD_GLOBAL)
  31. if lib_glu:
  32. self.glu = CDLL(lib_glu, RTLD_GLOBAL)
  33. if lib_gle:
  34. try:
  35. self.gle = CDLL(lib_gle)
  36. except OSError:
  37. pass
  38. if lib_gl:
  39. def test_gl(self):
  40. if self.gl:
  41. self.gl.glClearIndex
  42. if lib_glu:
  43. def test_glu(self):
  44. if self.glu:
  45. self.glu.gluBeginCurve
  46. if lib_gle:
  47. def test_gle(self):
  48. if self.gle:
  49. self.gle.gleGetJoinStyle
  50. ##if os.name == "posix" and sys.platform != "darwin":
  51. ## # On platforms where the default shared library suffix is '.so',
  52. ## # at least some libraries can be loaded as attributes of the cdll
  53. ## # object, since ctypes now tries loading the lib again
  54. ## # with '.so' appended of the first try fails.
  55. ## #
  56. ## # Won't work for libc, unfortunately. OTOH, it isn't
  57. ## # needed for libc since this is already mapped into the current
  58. ## # process (?)
  59. ## #
  60. ## # On MAC OSX, it won't work either, because dlopen() needs a full path,
  61. ## # and the default suffix is either none or '.dylib'.
  62. ## class LoadLibs(unittest.TestCase):
  63. ## def test_libm(self):
  64. ## import math
  65. ## libm = cdll.libm
  66. ## sqrt = libm.sqrt
  67. ## sqrt.argtypes = (c_double,)
  68. ## sqrt.restype = c_double
  69. ## self.failUnlessEqual(sqrt(2), math.sqrt(2))
  70. if __name__ == "__main__":
  71. unittest.main()