Update functools section
[python.git] / Lib / ctypes / util.py
blobd756c1c75fb8e657077a2994f19d8060b97c41f3
1 import sys, os
2 import ctypes
4 # find_library(name) returns the pathname of a library, or None.
5 if os.name == "nt":
6 def find_library(name):
7 # See MSDN for the REAL search order.
8 for directory in os.environ['PATH'].split(os.pathsep):
9 fname = os.path.join(directory, name)
10 if os.path.exists(fname):
11 return fname
12 if fname.lower().endswith(".dll"):
13 continue
14 fname = fname + ".dll"
15 if os.path.exists(fname):
16 return fname
17 return None
19 if os.name == "ce":
20 # search path according to MSDN:
21 # - absolute path specified by filename
22 # - The .exe launch directory
23 # - the Windows directory
24 # - ROM dll files (where are they?)
25 # - OEM specified search path: HKLM\Loader\SystemPath
26 def find_library(name):
27 return name
29 if os.name == "posix" and sys.platform == "darwin":
30 from ctypes.macholib.dyld import dyld_find as _dyld_find
31 def find_library(name):
32 possible = ['lib%s.dylib' % name,
33 '%s.dylib' % name,
34 '%s.framework/%s' % (name, name)]
35 for name in possible:
36 try:
37 return _dyld_find(name)
38 except ValueError:
39 continue
40 return None
42 elif os.name == "posix":
43 # Andreas Degert's find functions, using gcc, /sbin/ldconfig, objdump
44 import re, tempfile
46 def _findLib_gcc(name):
47 expr = '[^\(\)\s]*lib%s\.[^\(\)\s]*' % name
48 cmd = 'if type gcc &>/dev/null; then CC=gcc; else CC=cc; fi;' \
49 '$CC -Wl,-t -o /dev/null 2>&1 -l' + name
50 try:
51 fdout, outfile = tempfile.mkstemp()
52 fd = os.popen(cmd)
53 trace = fd.read()
54 err = fd.close()
55 finally:
56 try:
57 os.unlink(outfile)
58 except OSError, e:
59 if e.errno != errno.ENOENT:
60 raise
61 res = re.search(expr, trace)
62 if not res:
63 return None
64 return res.group(0)
66 def _findLib_ld(name):
67 expr = '/[^\(\)\s]*lib%s\.[^\(\)\s]*' % name
68 res = re.search(expr, os.popen('/sbin/ldconfig -p 2>/dev/null').read())
69 if not res:
70 # Hm, this works only for libs needed by the python executable.
71 cmd = 'ldd %s 2>/dev/null' % sys.executable
72 res = re.search(expr, os.popen(cmd).read())
73 if not res:
74 return None
75 return res.group(0)
77 def _get_soname(f):
78 cmd = "objdump -p -j .dynamic 2>/dev/null " + f
79 res = re.search(r'\sSONAME\s+([^\s]+)', os.popen(cmd).read())
80 if not res:
81 return None
82 return res.group(1)
84 def find_library(name):
85 lib = _findLib_ld(name) or _findLib_gcc(name)
86 if not lib:
87 return None
88 return _get_soname(lib)
90 ################################################################
91 # test code
93 def test():
94 from ctypes import cdll
95 if os.name == "nt":
96 print cdll.msvcrt
97 print cdll.load("msvcrt")
98 print find_library("msvcrt")
100 if os.name == "posix":
101 # find and load_version
102 print find_library("m")
103 print find_library("c")
104 print find_library("bz2")
106 # getattr
107 ## print cdll.m
108 ## print cdll.bz2
110 # load
111 if sys.platform == "darwin":
112 print cdll.LoadLibrary("libm.dylib")
113 print cdll.LoadLibrary("libcrypto.dylib")
114 print cdll.LoadLibrary("libSystem.dylib")
115 print cdll.LoadLibrary("System.framework/System")
116 else:
117 print cdll.LoadLibrary("libm.so")
118 print cdll.LoadLibrary("libcrypt.so")
119 print find_library("crypt")
121 if __name__ == "__main__":
122 test()