~ubuntu-branches/ubuntu/maverick/python3.1/maverick

« back to all changes in this revision

Viewing changes to Lib/ctypes/util.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2009-03-23 00:01:27 UTC
  • Revision ID: james.westby@ubuntu.com-20090323000127-5fstfxju4ufrhthq
Tags: upstream-3.1~a1+20090322
ImportĀ upstreamĀ versionĀ 3.1~a1+20090322

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import sys, os
 
2
 
 
3
# find_library(name) returns the pathname of a library, or None.
 
4
if os.name == "nt":
 
5
 
 
6
    def _get_build_version():
 
7
        """Return the version of MSVC that was used to build Python.
 
8
 
 
9
        For Python 2.3 and up, the version number is included in
 
10
        sys.version.  For earlier versions, assume the compiler is MSVC 6.
 
11
        """
 
12
        # This function was copied from Lib/distutils/msvccompiler.py
 
13
        prefix = "MSC v."
 
14
        i = sys.version.find(prefix)
 
15
        if i == -1:
 
16
            return 6
 
17
        i = i + len(prefix)
 
18
        s, rest = sys.version[i:].split(" ", 1)
 
19
        majorVersion = int(s[:-2]) - 6
 
20
        minorVersion = int(s[2:3]) / 10.0
 
21
        # I don't think paths are affected by minor version in version 6
 
22
        if majorVersion == 6:
 
23
            minorVersion = 0
 
24
        if majorVersion >= 6:
 
25
            return majorVersion + minorVersion
 
26
        # else we don't know what version of the compiler this is
 
27
        return None
 
28
 
 
29
    def find_msvcrt():
 
30
        """Return the name of the VC runtime dll"""
 
31
        version = _get_build_version()
 
32
        if version is None:
 
33
            # better be safe than sorry
 
34
            return None
 
35
        if version <= 6:
 
36
            clibname = 'msvcrt'
 
37
        else:
 
38
            clibname = 'msvcr%d' % (version * 10)
 
39
 
 
40
        # If python was built with in debug mode
 
41
        import imp
 
42
        if imp.get_suffixes()[0][0] == '_d.pyd':
 
43
            clibname += 'd'
 
44
        return clibname+'.dll'
 
45
 
 
46
    def find_library(name):
 
47
        if name in ('c', 'm'):
 
48
            return find_msvcrt()
 
49
        # See MSDN for the REAL search order.
 
50
        for directory in os.environ['PATH'].split(os.pathsep):
 
51
            fname = os.path.join(directory, name)
 
52
            if os.path.exists(fname):
 
53
                return fname
 
54
            if fname.lower().endswith(".dll"):
 
55
                continue
 
56
            fname = fname + ".dll"
 
57
            if os.path.exists(fname):
 
58
                return fname
 
59
        return None
 
60
 
 
61
if os.name == "ce":
 
62
    # search path according to MSDN:
 
63
    # - absolute path specified by filename
 
64
    # - The .exe launch directory
 
65
    # - the Windows directory
 
66
    # - ROM dll files (where are they?)
 
67
    # - OEM specified search path: HKLM\Loader\SystemPath
 
68
    def find_library(name):
 
69
        return name
 
70
 
 
71
if os.name == "posix" and sys.platform == "darwin":
 
72
    from ctypes.macholib.dyld import dyld_find as _dyld_find
 
73
    def find_library(name):
 
74
        possible = ['lib%s.dylib' % name,
 
75
                    '%s.dylib' % name,
 
76
                    '%s.framework/%s' % (name, name)]
 
77
        for name in possible:
 
78
            try:
 
79
                return _dyld_find(name)
 
80
            except ValueError:
 
81
                continue
 
82
        return None
 
83
 
 
84
elif os.name == "posix":
 
85
    # Andreas Degert's find functions, using gcc, /sbin/ldconfig, objdump
 
86
    import re, tempfile, errno
 
87
 
 
88
    def _findLib_gcc(name):
 
89
        expr = r'[^\(\)\s]*lib%s\.[^\(\)\s]*' % re.escape(name)
 
90
        fdout, ccout = tempfile.mkstemp()
 
91
        os.close(fdout)
 
92
        cmd = 'if type gcc >/dev/null 2>&1; then CC=gcc; elif type cc >/dev/null 2>&1; then CC=cc;else exit 10; fi;' \
 
93
              '$CC -Wl,-t -o ' + ccout + ' 2>&1 -l' + name
 
94
        try:
 
95
            f = os.popen(cmd)
 
96
            try:
 
97
                trace = f.read()
 
98
            finally:
 
99
                rv = f.close()
 
100
        finally:
 
101
            try:
 
102
                os.unlink(ccout)
 
103
            except OSError as e:
 
104
                if e.errno != errno.ENOENT:
 
105
                    raise
 
106
        if rv == 10:
 
107
            raise OSError('gcc or cc command not found')
 
108
        res = re.search(expr, trace)
 
109
        if not res:
 
110
            return None
 
111
        return res.group(0)
 
112
 
 
113
 
 
114
    if sys.platform == "sunos5":
 
115
        # use /usr/ccs/bin/dump on solaris
 
116
        def _get_soname(f):
 
117
            if not f:
 
118
                return None
 
119
            cmd = "/usr/ccs/bin/dump -Lpv 2>/dev/null " + f
 
120
            f = os.popen(cmd)
 
121
            try:
 
122
                data = f.read()
 
123
            finally:
 
124
                f.close()
 
125
            res = re.search(r'\[.*\]\sSONAME\s+([^\s]+)', data)
 
126
            if not res:
 
127
                return None
 
128
            return res.group(1)
 
129
    else:
 
130
        def _get_soname(f):
 
131
            # assuming GNU binutils / ELF
 
132
            if not f:
 
133
                return None
 
134
            cmd = 'if ! type objdump >/dev/null 2>&1; then exit 10; fi;' \
 
135
                  "objdump -p -j .dynamic 2>/dev/null " + f
 
136
            f = os.popen(cmd)
 
137
            dump = f.read()
 
138
            rv = f.close()
 
139
            if rv == 10:
 
140
                raise OSError('objdump command not found')
 
141
            f = os.popen(cmd)
 
142
            try:
 
143
                data = f.read()
 
144
            finally:
 
145
                f.close()
 
146
            res = re.search(r'\sSONAME\s+([^\s]+)', data)
 
147
            if not res:
 
148
                return None
 
149
            return res.group(1)
 
150
 
 
151
    if (sys.platform.startswith("freebsd")
 
152
        or sys.platform.startswith("openbsd")
 
153
        or sys.platform.startswith("dragonfly")):
 
154
 
 
155
        def _num_version(libname):
 
156
            # "libxyz.so.MAJOR.MINOR" => [ MAJOR, MINOR ]
 
157
            parts = libname.split(".")
 
158
            nums = []
 
159
            try:
 
160
                while parts:
 
161
                    nums.insert(0, int(parts.pop()))
 
162
            except ValueError:
 
163
                pass
 
164
            return nums or [ sys.maxsize ]
 
165
 
 
166
        def find_library(name):
 
167
            ename = re.escape(name)
 
168
            expr = r':-l%s\.\S+ => \S*/(lib%s\.\S+)' % (ename, ename)
 
169
            f = os.popen('/sbin/ldconfig -r 2>/dev/null')
 
170
            try:
 
171
                data = f.read()
 
172
            finally:
 
173
                f.close()
 
174
            res = re.findall(expr, data)
 
175
            if not res:
 
176
                return _get_soname(_findLib_gcc(name))
 
177
            res.sort(key=_num_version)
 
178
            return res[-1]
 
179
 
 
180
    else:
 
181
 
 
182
        def _findLib_ldconfig(name):
 
183
            # XXX assuming GLIBC's ldconfig (with option -p)
 
184
            expr = r'/[^\(\)\s]*lib%s\.[^\(\)\s]*' % re.escape(name)
 
185
            f = os.popen('/sbin/ldconfig -p 2>/dev/null')
 
186
            try:
 
187
                data = f.read()
 
188
            finally:
 
189
                f.close()
 
190
            res = re.search(expr, data)
 
191
            if not res:
 
192
                # Hm, this works only for libs needed by the python executable.
 
193
                cmd = 'ldd %s 2>/dev/null' % sys.executable
 
194
                f = os.popen(cmd)
 
195
                try:
 
196
                    data = f.read()
 
197
                finally:
 
198
                    f.close()
 
199
                res = re.search(expr, data)
 
200
                if not res:
 
201
                    return None
 
202
            return res.group(0)
 
203
 
 
204
        def _findSoname_ldconfig(name):
 
205
            import struct
 
206
            if struct.calcsize('l') == 4:
 
207
                machine = os.uname()[4] + '-32'
 
208
            else:
 
209
                machine = os.uname()[4] + '-64'
 
210
            mach_map = {
 
211
                'x86_64-64': 'libc6,x86-64',
 
212
                'ppc64-64': 'libc6,64bit',
 
213
                'sparc64-64': 'libc6,64bit',
 
214
                's390x-64': 'libc6,64bit',
 
215
                'ia64-64': 'libc6,IA-64',
 
216
                }
 
217
            abi_type = mach_map.get(machine, 'libc6')
 
218
 
 
219
            # XXX assuming GLIBC's ldconfig (with option -p)
 
220
            expr = r'(\S+)\s+\((%s(?:, OS ABI:[^\)]*)?)\)[^/]*(/[^\(\)\s]*lib%s\.[^\(\)\s]*)' \
 
221
                   % (abi_type, re.escape(name))
 
222
            res = re.search(expr,
 
223
                            os.popen('/sbin/ldconfig -p 2>/dev/null').read())
 
224
            if not res:
 
225
                return None
 
226
            return res.group(1)
 
227
 
 
228
        def find_library(name):
 
229
            return _findSoname_ldconfig(name) or _get_soname(_findLib_gcc(name))
 
230
 
 
231
################################################################
 
232
# test code
 
233
 
 
234
def test():
 
235
    from ctypes import cdll
 
236
    if os.name == "nt":
 
237
        print(cdll.msvcrt)
 
238
        print(cdll.load("msvcrt"))
 
239
        print(find_library("msvcrt"))
 
240
 
 
241
    if os.name == "posix":
 
242
        # find and load_version
 
243
        print(find_library("m"))
 
244
        print(find_library("c"))
 
245
        print(find_library("bz2"))
 
246
 
 
247
        # getattr
 
248
##        print cdll.m
 
249
##        print cdll.bz2
 
250
 
 
251
        # load
 
252
        if sys.platform == "darwin":
 
253
            print(cdll.LoadLibrary("libm.dylib"))
 
254
            print(cdll.LoadLibrary("libcrypto.dylib"))
 
255
            print(cdll.LoadLibrary("libSystem.dylib"))
 
256
            print(cdll.LoadLibrary("System.framework/System"))
 
257
        else:
 
258
            print(cdll.LoadLibrary("libm.so"))
 
259
            print(cdll.LoadLibrary("libcrypt.so"))
 
260
            print(find_library("crypt"))
 
261
 
 
262
if __name__ == "__main__":
 
263
    test()