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