Updates of recent changes to logging.
[python.git] / Lib / ctypes / util.py
blobf7133538bfaf31e38373cd86c3401cac5bc8ab30
1 ######################################################################
2 # This file should be kept compatible with Python 2.3, see PEP 291. #
3 ######################################################################
4 import sys, os
6 # find_library(name) returns the pathname of a library, or None.
7 if os.name == "nt":
8 def find_library(name):
9 # See MSDN for the REAL search order.
10 for directory in os.environ['PATH'].split(os.pathsep):
11 fname = os.path.join(directory, name)
12 if os.path.exists(fname):
13 return fname
14 if fname.lower().endswith(".dll"):
15 continue
16 fname = fname + ".dll"
17 if os.path.exists(fname):
18 return fname
19 return None
21 if os.name == "ce":
22 # search path according to MSDN:
23 # - absolute path specified by filename
24 # - The .exe launch directory
25 # - the Windows directory
26 # - ROM dll files (where are they?)
27 # - OEM specified search path: HKLM\Loader\SystemPath
28 def find_library(name):
29 return name
31 if os.name == "posix" and sys.platform == "darwin":
32 from ctypes.macholib.dyld import dyld_find as _dyld_find
33 def find_library(name):
34 possible = ['lib%s.dylib' % name,
35 '%s.dylib' % name,
36 '%s.framework/%s' % (name, name)]
37 for name in possible:
38 try:
39 return _dyld_find(name)
40 except ValueError:
41 continue
42 return None
44 elif os.name == "posix":
45 # Andreas Degert's find functions, using gcc, /sbin/ldconfig, objdump
46 import re, tempfile, errno
48 def _findLib_gcc(name):
49 expr = r'[^\(\)\s]*lib%s\.[^\(\)\s]*' % re.escape(name)
50 fdout, ccout = tempfile.mkstemp()
51 os.close(fdout)
52 cmd = 'if type gcc >/dev/null 2>&1; then CC=gcc; else CC=cc; fi;' \
53 '$CC -Wl,-t -o ' + ccout + ' 2>&1 -l' + name
54 try:
55 f = os.popen(cmd)
56 trace = f.read()
57 f.close()
58 finally:
59 try:
60 os.unlink(ccout)
61 except OSError, e:
62 if e.errno != errno.ENOENT:
63 raise
64 res = re.search(expr, trace)
65 if not res:
66 return None
67 return res.group(0)
69 def _get_soname(f):
70 # assuming GNU binutils / ELF
71 if not f:
72 return None
73 cmd = "objdump -p -j .dynamic 2>/dev/null " + f
74 res = re.search(r'\sSONAME\s+([^\s]+)', os.popen(cmd).read())
75 if not res:
76 return None
77 return res.group(1)
79 if (sys.platform.startswith("freebsd")
80 or sys.platform.startswith("openbsd")
81 or sys.platform.startswith("dragonfly")):
83 def _num_version(libname):
84 # "libxyz.so.MAJOR.MINOR" => [ MAJOR, MINOR ]
85 parts = libname.split(".")
86 nums = []
87 try:
88 while parts:
89 nums.insert(0, int(parts.pop()))
90 except ValueError:
91 pass
92 return nums or [ sys.maxint ]
94 def find_library(name):
95 ename = re.escape(name)
96 expr = r':-l%s\.\S+ => \S*/(lib%s\.\S+)' % (ename, ename)
97 res = re.findall(expr,
98 os.popen('/sbin/ldconfig -r 2>/dev/null').read())
99 if not res:
100 return _get_soname(_findLib_gcc(name))
101 res.sort(cmp= lambda x,y: cmp(_num_version(x), _num_version(y)))
102 return res[-1]
104 else:
106 def _findLib_ldconfig(name):
107 # XXX assuming GLIBC's ldconfig (with option -p)
108 expr = r'/[^\(\)\s]*lib%s\.[^\(\)\s]*' % re.escape(name)
109 res = re.search(expr,
110 os.popen('/sbin/ldconfig -p 2>/dev/null').read())
111 if not res:
112 # Hm, this works only for libs needed by the python executable.
113 cmd = 'ldd %s 2>/dev/null' % sys.executable
114 res = re.search(expr, os.popen(cmd).read())
115 if not res:
116 return None
117 return res.group(0)
119 def find_library(name):
120 return _get_soname(_findLib_ldconfig(name) or _findLib_gcc(name))
122 ################################################################
123 # test code
125 def test():
126 from ctypes import cdll
127 if os.name == "nt":
128 print cdll.msvcrt
129 print cdll.load("msvcrt")
130 print find_library("msvcrt")
132 if os.name == "posix":
133 # find and load_version
134 print find_library("m")
135 print find_library("c")
136 print find_library("bz2")
138 # getattr
139 ## print cdll.m
140 ## print cdll.bz2
142 # load
143 if sys.platform == "darwin":
144 print cdll.LoadLibrary("libm.dylib")
145 print cdll.LoadLibrary("libcrypto.dylib")
146 print cdll.LoadLibrary("libSystem.dylib")
147 print cdll.LoadLibrary("System.framework/System")
148 else:
149 print cdll.LoadLibrary("libm.so")
150 print cdll.LoadLibrary("libcrypt.so")
151 print find_library("crypt")
153 if __name__ == "__main__":
154 test()