annotate Lib/ctypes/util.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents be41abd74949
children d76fb24d79c3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
38780
381a72ab5fb8 Add pep-291 compatibility markers.
Thomas Heller <theller@ctypes.org>
parents: 38743
diff changeset
1 ######################################################################
381a72ab5fb8 Add pep-291 compatibility markers.
Thomas Heller <theller@ctypes.org>
parents: 38743
diff changeset
2 # This file should be kept compatible with Python 2.3, see PEP 291. #
381a72ab5fb8 Add pep-291 compatibility markers.
Thomas Heller <theller@ctypes.org>
parents: 38743
diff changeset
3 ######################################################################
38166
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
4 import sys, os
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
5
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
6 # find_library(name) returns the pathname of a library, or None.
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
7 if os.name == "nt":
47113
6af64512294a Issue 1793: Add ctypes.util.find_msvcrt() function (on Windows).
Thomas Heller <theller@ctypes.org>
parents: 43371
diff changeset
8
6af64512294a Issue 1793: Add ctypes.util.find_msvcrt() function (on Windows).
Thomas Heller <theller@ctypes.org>
parents: 43371
diff changeset
9 def _get_build_version():
6af64512294a Issue 1793: Add ctypes.util.find_msvcrt() function (on Windows).
Thomas Heller <theller@ctypes.org>
parents: 43371
diff changeset
10 """Return the version of MSVC that was used to build Python.
6af64512294a Issue 1793: Add ctypes.util.find_msvcrt() function (on Windows).
Thomas Heller <theller@ctypes.org>
parents: 43371
diff changeset
11
6af64512294a Issue 1793: Add ctypes.util.find_msvcrt() function (on Windows).
Thomas Heller <theller@ctypes.org>
parents: 43371
diff changeset
12 For Python 2.3 and up, the version number is included in
6af64512294a Issue 1793: Add ctypes.util.find_msvcrt() function (on Windows).
Thomas Heller <theller@ctypes.org>
parents: 43371
diff changeset
13 sys.version. For earlier versions, assume the compiler is MSVC 6.
6af64512294a Issue 1793: Add ctypes.util.find_msvcrt() function (on Windows).
Thomas Heller <theller@ctypes.org>
parents: 43371
diff changeset
14 """
6af64512294a Issue 1793: Add ctypes.util.find_msvcrt() function (on Windows).
Thomas Heller <theller@ctypes.org>
parents: 43371
diff changeset
15 # This function was copied from Lib/distutils/msvccompiler.py
6af64512294a Issue 1793: Add ctypes.util.find_msvcrt() function (on Windows).
Thomas Heller <theller@ctypes.org>
parents: 43371
diff changeset
16 prefix = "MSC v."
6af64512294a Issue 1793: Add ctypes.util.find_msvcrt() function (on Windows).
Thomas Heller <theller@ctypes.org>
parents: 43371
diff changeset
17 i = sys.version.find(prefix)
6af64512294a Issue 1793: Add ctypes.util.find_msvcrt() function (on Windows).
Thomas Heller <theller@ctypes.org>
parents: 43371
diff changeset
18 if i == -1:
6af64512294a Issue 1793: Add ctypes.util.find_msvcrt() function (on Windows).
Thomas Heller <theller@ctypes.org>
parents: 43371
diff changeset
19 return 6
6af64512294a Issue 1793: Add ctypes.util.find_msvcrt() function (on Windows).
Thomas Heller <theller@ctypes.org>
parents: 43371
diff changeset
20 i = i + len(prefix)
6af64512294a Issue 1793: Add ctypes.util.find_msvcrt() function (on Windows).
Thomas Heller <theller@ctypes.org>
parents: 43371
diff changeset
21 s, rest = sys.version[i:].split(" ", 1)
6af64512294a Issue 1793: Add ctypes.util.find_msvcrt() function (on Windows).
Thomas Heller <theller@ctypes.org>
parents: 43371
diff changeset
22 majorVersion = int(s[:-2]) - 6
6af64512294a Issue 1793: Add ctypes.util.find_msvcrt() function (on Windows).
Thomas Heller <theller@ctypes.org>
parents: 43371
diff changeset
23 minorVersion = int(s[2:3]) / 10.0
6af64512294a Issue 1793: Add ctypes.util.find_msvcrt() function (on Windows).
Thomas Heller <theller@ctypes.org>
parents: 43371
diff changeset
24 # I don't think paths are affected by minor version in version 6
6af64512294a Issue 1793: Add ctypes.util.find_msvcrt() function (on Windows).
Thomas Heller <theller@ctypes.org>
parents: 43371
diff changeset
25 if majorVersion == 6:
6af64512294a Issue 1793: Add ctypes.util.find_msvcrt() function (on Windows).
Thomas Heller <theller@ctypes.org>
parents: 43371
diff changeset
26 minorVersion = 0
6af64512294a Issue 1793: Add ctypes.util.find_msvcrt() function (on Windows).
Thomas Heller <theller@ctypes.org>
parents: 43371
diff changeset
27 if majorVersion >= 6:
6af64512294a Issue 1793: Add ctypes.util.find_msvcrt() function (on Windows).
Thomas Heller <theller@ctypes.org>
parents: 43371
diff changeset
28 return majorVersion + minorVersion
6af64512294a Issue 1793: Add ctypes.util.find_msvcrt() function (on Windows).
Thomas Heller <theller@ctypes.org>
parents: 43371
diff changeset
29 # else we don't know what version of the compiler this is
6af64512294a Issue 1793: Add ctypes.util.find_msvcrt() function (on Windows).
Thomas Heller <theller@ctypes.org>
parents: 43371
diff changeset
30 return None
6af64512294a Issue 1793: Add ctypes.util.find_msvcrt() function (on Windows).
Thomas Heller <theller@ctypes.org>
parents: 43371
diff changeset
31
6af64512294a Issue 1793: Add ctypes.util.find_msvcrt() function (on Windows).
Thomas Heller <theller@ctypes.org>
parents: 43371
diff changeset
32 def find_msvcrt():
6af64512294a Issue 1793: Add ctypes.util.find_msvcrt() function (on Windows).
Thomas Heller <theller@ctypes.org>
parents: 43371
diff changeset
33 """Return the name of the VC runtime dll"""
6af64512294a Issue 1793: Add ctypes.util.find_msvcrt() function (on Windows).
Thomas Heller <theller@ctypes.org>
parents: 43371
diff changeset
34 version = _get_build_version()
6af64512294a Issue 1793: Add ctypes.util.find_msvcrt() function (on Windows).
Thomas Heller <theller@ctypes.org>
parents: 43371
diff changeset
35 if version is None:
6af64512294a Issue 1793: Add ctypes.util.find_msvcrt() function (on Windows).
Thomas Heller <theller@ctypes.org>
parents: 43371
diff changeset
36 # better be safe than sorry
6af64512294a Issue 1793: Add ctypes.util.find_msvcrt() function (on Windows).
Thomas Heller <theller@ctypes.org>
parents: 43371
diff changeset
37 return None
6af64512294a Issue 1793: Add ctypes.util.find_msvcrt() function (on Windows).
Thomas Heller <theller@ctypes.org>
parents: 43371
diff changeset
38 if version <= 6:
6af64512294a Issue 1793: Add ctypes.util.find_msvcrt() function (on Windows).
Thomas Heller <theller@ctypes.org>
parents: 43371
diff changeset
39 clibname = 'msvcrt'
6af64512294a Issue 1793: Add ctypes.util.find_msvcrt() function (on Windows).
Thomas Heller <theller@ctypes.org>
parents: 43371
diff changeset
40 else:
6af64512294a Issue 1793: Add ctypes.util.find_msvcrt() function (on Windows).
Thomas Heller <theller@ctypes.org>
parents: 43371
diff changeset
41 clibname = 'msvcr%d' % (version * 10)
6af64512294a Issue 1793: Add ctypes.util.find_msvcrt() function (on Windows).
Thomas Heller <theller@ctypes.org>
parents: 43371
diff changeset
42
6af64512294a Issue 1793: Add ctypes.util.find_msvcrt() function (on Windows).
Thomas Heller <theller@ctypes.org>
parents: 43371
diff changeset
43 # If python was built with in debug mode
6af64512294a Issue 1793: Add ctypes.util.find_msvcrt() function (on Windows).
Thomas Heller <theller@ctypes.org>
parents: 43371
diff changeset
44 import imp
6af64512294a Issue 1793: Add ctypes.util.find_msvcrt() function (on Windows).
Thomas Heller <theller@ctypes.org>
parents: 43371
diff changeset
45 if imp.get_suffixes()[0][0] == '_d.pyd':
6af64512294a Issue 1793: Add ctypes.util.find_msvcrt() function (on Windows).
Thomas Heller <theller@ctypes.org>
parents: 43371
diff changeset
46 clibname += 'd'
6af64512294a Issue 1793: Add ctypes.util.find_msvcrt() function (on Windows).
Thomas Heller <theller@ctypes.org>
parents: 43371
diff changeset
47 return clibname+'.dll'
6af64512294a Issue 1793: Add ctypes.util.find_msvcrt() function (on Windows).
Thomas Heller <theller@ctypes.org>
parents: 43371
diff changeset
48
38166
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
49 def find_library(name):
47113
6af64512294a Issue 1793: Add ctypes.util.find_msvcrt() function (on Windows).
Thomas Heller <theller@ctypes.org>
parents: 43371
diff changeset
50 if name in ('c', 'm'):
6af64512294a Issue 1793: Add ctypes.util.find_msvcrt() function (on Windows).
Thomas Heller <theller@ctypes.org>
parents: 43371
diff changeset
51 return find_msvcrt()
38166
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
52 # See MSDN for the REAL search order.
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
53 for directory in os.environ['PATH'].split(os.pathsep):
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
54 fname = os.path.join(directory, name)
53807
a85e6756ded2 Merged revisions 72352 via svnmerge from
Thomas Heller <theller@ctypes.org>
parents: 50704
diff changeset
55 if os.path.isfile(fname):
38166
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
56 return fname
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
57 if fname.lower().endswith(".dll"):
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
58 continue
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
59 fname = fname + ".dll"
53807
a85e6756ded2 Merged revisions 72352 via svnmerge from
Thomas Heller <theller@ctypes.org>
parents: 50704
diff changeset
60 if os.path.isfile(fname):
38166
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
61 return fname
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
62 return None
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
63
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
64 if os.name == "ce":
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
65 # search path according to MSDN:
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
66 # - absolute path specified by filename
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
67 # - The .exe launch directory
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
68 # - the Windows directory
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
69 # - ROM dll files (where are they?)
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
70 # - OEM specified search path: HKLM\Loader\SystemPath
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
71 def find_library(name):
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
72 return name
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
73
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
74 if os.name == "posix" and sys.platform == "darwin":
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
75 from ctypes.macholib.dyld import dyld_find as _dyld_find
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
76 def find_library(name):
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
77 possible = ['lib%s.dylib' % name,
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
78 '%s.dylib' % name,
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
79 '%s.framework/%s' % (name, name)]
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
80 for name in possible:
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
81 try:
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
82 return _dyld_find(name)
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
83 except ValueError:
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
84 continue
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
85 return None
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
86
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
87 elif os.name == "posix":
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
88 # Andreas Degert's find functions, using gcc, /sbin/ldconfig, objdump
38743
e8464afef4a4 Fix errors found by pychecker
Neal Norwitz <nnorwitz@gmail.com>
parents: 38166
diff changeset
89 import re, tempfile, errno
38166
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
90
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
91 def _findLib_gcc(name):
40612
cf8af6b7598b patch #1610795: BSD version of ctypes.util.find_library, by Martin
Thomas Heller <theller@ctypes.org>
parents: 39075
diff changeset
92 expr = r'[^\(\)\s]*lib%s\.[^\(\)\s]*' % re.escape(name)
39075
e4b6a4f2ce14 Fix #1521375. When running with root priviledges, 'gcc -o /dev/null'
Thomas Heller <theller@ctypes.org>
parents: 38780
diff changeset
93 fdout, ccout = tempfile.mkstemp()
e4b6a4f2ce14 Fix #1521375. When running with root priviledges, 'gcc -o /dev/null'
Thomas Heller <theller@ctypes.org>
parents: 38780
diff changeset
94 os.close(fdout)
50704
3b5be0ee32ea Merge r68487 from svn+ssh://pythondev@svn.python.org/python/trunk:
Matthias Klose <doko@ubuntu.com>
parents: 47113
diff changeset
95 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;' \
39075
e4b6a4f2ce14 Fix #1521375. When running with root priviledges, 'gcc -o /dev/null'
Thomas Heller <theller@ctypes.org>
parents: 38780
diff changeset
96 '$CC -Wl,-t -o ' + ccout + ' 2>&1 -l' + name
38166
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
97 try:
40612
cf8af6b7598b patch #1610795: BSD version of ctypes.util.find_library, by Martin
Thomas Heller <theller@ctypes.org>
parents: 39075
diff changeset
98 f = os.popen(cmd)
56682
db21c3c009db Per the discussion in issue6882, backport the try/finally work that was done to the py3k version (mostly in r59477, I think).
Eric Smith <eric@trueblade.com>
parents: 53807
diff changeset
99 try:
db21c3c009db Per the discussion in issue6882, backport the try/finally work that was done to the py3k version (mostly in r59477, I think).
Eric Smith <eric@trueblade.com>
parents: 53807
diff changeset
100 trace = f.read()
db21c3c009db Per the discussion in issue6882, backport the try/finally work that was done to the py3k version (mostly in r59477, I think).
Eric Smith <eric@trueblade.com>
parents: 53807
diff changeset
101 finally:
db21c3c009db Per the discussion in issue6882, backport the try/finally work that was done to the py3k version (mostly in r59477, I think).
Eric Smith <eric@trueblade.com>
parents: 53807
diff changeset
102 rv = f.close()
38166
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
103 finally:
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
104 try:
39075
e4b6a4f2ce14 Fix #1521375. When running with root priviledges, 'gcc -o /dev/null'
Thomas Heller <theller@ctypes.org>
parents: 38780
diff changeset
105 os.unlink(ccout)
e4b6a4f2ce14 Fix #1521375. When running with root priviledges, 'gcc -o /dev/null'
Thomas Heller <theller@ctypes.org>
parents: 38780
diff changeset
106 except OSError, e:
e4b6a4f2ce14 Fix #1521375. When running with root priviledges, 'gcc -o /dev/null'
Thomas Heller <theller@ctypes.org>
parents: 38780
diff changeset
107 if e.errno != errno.ENOENT:
e4b6a4f2ce14 Fix #1521375. When running with root priviledges, 'gcc -o /dev/null'
Thomas Heller <theller@ctypes.org>
parents: 38780
diff changeset
108 raise
50704
3b5be0ee32ea Merge r68487 from svn+ssh://pythondev@svn.python.org/python/trunk:
Matthias Klose <doko@ubuntu.com>
parents: 47113
diff changeset
109 if rv == 10:
3b5be0ee32ea Merge r68487 from svn+ssh://pythondev@svn.python.org/python/trunk:
Matthias Klose <doko@ubuntu.com>
parents: 47113
diff changeset
110 raise OSError, 'gcc or cc command not found'
38166
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
111 res = re.search(expr, trace)
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
112 if not res:
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
113 return None
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
114 return res.group(0)
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
115
43371
fdff48f87f35 ctypes.util.find_library uses dump(1) instead of objdump(1) on Solaris.
Thomas Heller <theller@ctypes.org>
parents: 40612
diff changeset
116
fdff48f87f35 ctypes.util.find_library uses dump(1) instead of objdump(1) on Solaris.
Thomas Heller <theller@ctypes.org>
parents: 40612
diff changeset
117 if sys.platform == "sunos5":
fdff48f87f35 ctypes.util.find_library uses dump(1) instead of objdump(1) on Solaris.
Thomas Heller <theller@ctypes.org>
parents: 40612
diff changeset
118 # use /usr/ccs/bin/dump on solaris
fdff48f87f35 ctypes.util.find_library uses dump(1) instead of objdump(1) on Solaris.
Thomas Heller <theller@ctypes.org>
parents: 40612
diff changeset
119 def _get_soname(f):
fdff48f87f35 ctypes.util.find_library uses dump(1) instead of objdump(1) on Solaris.
Thomas Heller <theller@ctypes.org>
parents: 40612
diff changeset
120 if not f:
fdff48f87f35 ctypes.util.find_library uses dump(1) instead of objdump(1) on Solaris.
Thomas Heller <theller@ctypes.org>
parents: 40612
diff changeset
121 return None
fdff48f87f35 ctypes.util.find_library uses dump(1) instead of objdump(1) on Solaris.
Thomas Heller <theller@ctypes.org>
parents: 40612
diff changeset
122 cmd = "/usr/ccs/bin/dump -Lpv 2>/dev/null " + f
56682
db21c3c009db Per the discussion in issue6882, backport the try/finally work that was done to the py3k version (mostly in r59477, I think).
Eric Smith <eric@trueblade.com>
parents: 53807
diff changeset
123 f = os.popen(cmd)
db21c3c009db Per the discussion in issue6882, backport the try/finally work that was done to the py3k version (mostly in r59477, I think).
Eric Smith <eric@trueblade.com>
parents: 53807
diff changeset
124 try:
db21c3c009db Per the discussion in issue6882, backport the try/finally work that was done to the py3k version (mostly in r59477, I think).
Eric Smith <eric@trueblade.com>
parents: 53807
diff changeset
125 data = f.read()
db21c3c009db Per the discussion in issue6882, backport the try/finally work that was done to the py3k version (mostly in r59477, I think).
Eric Smith <eric@trueblade.com>
parents: 53807
diff changeset
126 finally:
db21c3c009db Per the discussion in issue6882, backport the try/finally work that was done to the py3k version (mostly in r59477, I think).
Eric Smith <eric@trueblade.com>
parents: 53807
diff changeset
127 f.close()
db21c3c009db Per the discussion in issue6882, backport the try/finally work that was done to the py3k version (mostly in r59477, I think).
Eric Smith <eric@trueblade.com>
parents: 53807
diff changeset
128 res = re.search(r'\[.*\]\sSONAME\s+([^\s]+)', data)
43371
fdff48f87f35 ctypes.util.find_library uses dump(1) instead of objdump(1) on Solaris.
Thomas Heller <theller@ctypes.org>
parents: 40612
diff changeset
129 if not res:
fdff48f87f35 ctypes.util.find_library uses dump(1) instead of objdump(1) on Solaris.
Thomas Heller <theller@ctypes.org>
parents: 40612
diff changeset
130 return None
fdff48f87f35 ctypes.util.find_library uses dump(1) instead of objdump(1) on Solaris.
Thomas Heller <theller@ctypes.org>
parents: 40612
diff changeset
131 return res.group(1)
fdff48f87f35 ctypes.util.find_library uses dump(1) instead of objdump(1) on Solaris.
Thomas Heller <theller@ctypes.org>
parents: 40612
diff changeset
132 else:
fdff48f87f35 ctypes.util.find_library uses dump(1) instead of objdump(1) on Solaris.
Thomas Heller <theller@ctypes.org>
parents: 40612
diff changeset
133 def _get_soname(f):
fdff48f87f35 ctypes.util.find_library uses dump(1) instead of objdump(1) on Solaris.
Thomas Heller <theller@ctypes.org>
parents: 40612
diff changeset
134 # assuming GNU binutils / ELF
fdff48f87f35 ctypes.util.find_library uses dump(1) instead of objdump(1) on Solaris.
Thomas Heller <theller@ctypes.org>
parents: 40612
diff changeset
135 if not f:
fdff48f87f35 ctypes.util.find_library uses dump(1) instead of objdump(1) on Solaris.
Thomas Heller <theller@ctypes.org>
parents: 40612
diff changeset
136 return None
50704
3b5be0ee32ea Merge r68487 from svn+ssh://pythondev@svn.python.org/python/trunk:
Matthias Klose <doko@ubuntu.com>
parents: 47113
diff changeset
137 cmd = 'if ! type objdump >/dev/null 2>&1; then exit 10; fi;' \
3b5be0ee32ea Merge r68487 from svn+ssh://pythondev@svn.python.org/python/trunk:
Matthias Klose <doko@ubuntu.com>
parents: 47113
diff changeset
138 "objdump -p -j .dynamic 2>/dev/null " + f
3b5be0ee32ea Merge r68487 from svn+ssh://pythondev@svn.python.org/python/trunk:
Matthias Klose <doko@ubuntu.com>
parents: 47113
diff changeset
139 f = os.popen(cmd)
3b5be0ee32ea Merge r68487 from svn+ssh://pythondev@svn.python.org/python/trunk:
Matthias Klose <doko@ubuntu.com>
parents: 47113
diff changeset
140 dump = f.read()
3b5be0ee32ea Merge r68487 from svn+ssh://pythondev@svn.python.org/python/trunk:
Matthias Klose <doko@ubuntu.com>
parents: 47113
diff changeset
141 rv = f.close()
3b5be0ee32ea Merge r68487 from svn+ssh://pythondev@svn.python.org/python/trunk:
Matthias Klose <doko@ubuntu.com>
parents: 47113
diff changeset
142 if rv == 10:
3b5be0ee32ea Merge r68487 from svn+ssh://pythondev@svn.python.org/python/trunk:
Matthias Klose <doko@ubuntu.com>
parents: 47113
diff changeset
143 raise OSError, 'objdump command not found'
56682
db21c3c009db Per the discussion in issue6882, backport the try/finally work that was done to the py3k version (mostly in r59477, I think).
Eric Smith <eric@trueblade.com>
parents: 53807
diff changeset
144 f = os.popen(cmd)
db21c3c009db Per the discussion in issue6882, backport the try/finally work that was done to the py3k version (mostly in r59477, I think).
Eric Smith <eric@trueblade.com>
parents: 53807
diff changeset
145 try:
db21c3c009db Per the discussion in issue6882, backport the try/finally work that was done to the py3k version (mostly in r59477, I think).
Eric Smith <eric@trueblade.com>
parents: 53807
diff changeset
146 data = f.read()
db21c3c009db Per the discussion in issue6882, backport the try/finally work that was done to the py3k version (mostly in r59477, I think).
Eric Smith <eric@trueblade.com>
parents: 53807
diff changeset
147 finally:
db21c3c009db Per the discussion in issue6882, backport the try/finally work that was done to the py3k version (mostly in r59477, I think).
Eric Smith <eric@trueblade.com>
parents: 53807
diff changeset
148 f.close()
db21c3c009db Per the discussion in issue6882, backport the try/finally work that was done to the py3k version (mostly in r59477, I think).
Eric Smith <eric@trueblade.com>
parents: 53807
diff changeset
149 res = re.search(r'\sSONAME\s+([^\s]+)', data)
43371
fdff48f87f35 ctypes.util.find_library uses dump(1) instead of objdump(1) on Solaris.
Thomas Heller <theller@ctypes.org>
parents: 40612
diff changeset
150 if not res:
fdff48f87f35 ctypes.util.find_library uses dump(1) instead of objdump(1) on Solaris.
Thomas Heller <theller@ctypes.org>
parents: 40612
diff changeset
151 return None
fdff48f87f35 ctypes.util.find_library uses dump(1) instead of objdump(1) on Solaris.
Thomas Heller <theller@ctypes.org>
parents: 40612
diff changeset
152 return res.group(1)
38166
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
153
40612
cf8af6b7598b patch #1610795: BSD version of ctypes.util.find_library, by Martin
Thomas Heller <theller@ctypes.org>
parents: 39075
diff changeset
154 if (sys.platform.startswith("freebsd")
cf8af6b7598b patch #1610795: BSD version of ctypes.util.find_library, by Martin
Thomas Heller <theller@ctypes.org>
parents: 39075
diff changeset
155 or sys.platform.startswith("openbsd")
cf8af6b7598b patch #1610795: BSD version of ctypes.util.find_library, by Martin
Thomas Heller <theller@ctypes.org>
parents: 39075
diff changeset
156 or sys.platform.startswith("dragonfly")):
cf8af6b7598b patch #1610795: BSD version of ctypes.util.find_library, by Martin
Thomas Heller <theller@ctypes.org>
parents: 39075
diff changeset
157
cf8af6b7598b patch #1610795: BSD version of ctypes.util.find_library, by Martin
Thomas Heller <theller@ctypes.org>
parents: 39075
diff changeset
158 def _num_version(libname):
cf8af6b7598b patch #1610795: BSD version of ctypes.util.find_library, by Martin
Thomas Heller <theller@ctypes.org>
parents: 39075
diff changeset
159 # "libxyz.so.MAJOR.MINOR" => [ MAJOR, MINOR ]
cf8af6b7598b patch #1610795: BSD version of ctypes.util.find_library, by Martin
Thomas Heller <theller@ctypes.org>
parents: 39075
diff changeset
160 parts = libname.split(".")
cf8af6b7598b patch #1610795: BSD version of ctypes.util.find_library, by Martin
Thomas Heller <theller@ctypes.org>
parents: 39075
diff changeset
161 nums = []
cf8af6b7598b patch #1610795: BSD version of ctypes.util.find_library, by Martin
Thomas Heller <theller@ctypes.org>
parents: 39075
diff changeset
162 try:
cf8af6b7598b patch #1610795: BSD version of ctypes.util.find_library, by Martin
Thomas Heller <theller@ctypes.org>
parents: 39075
diff changeset
163 while parts:
cf8af6b7598b patch #1610795: BSD version of ctypes.util.find_library, by Martin
Thomas Heller <theller@ctypes.org>
parents: 39075
diff changeset
164 nums.insert(0, int(parts.pop()))
cf8af6b7598b patch #1610795: BSD version of ctypes.util.find_library, by Martin
Thomas Heller <theller@ctypes.org>
parents: 39075
diff changeset
165 except ValueError:
cf8af6b7598b patch #1610795: BSD version of ctypes.util.find_library, by Martin
Thomas Heller <theller@ctypes.org>
parents: 39075
diff changeset
166 pass
cf8af6b7598b patch #1610795: BSD version of ctypes.util.find_library, by Martin
Thomas Heller <theller@ctypes.org>
parents: 39075
diff changeset
167 return nums or [ sys.maxint ]
cf8af6b7598b patch #1610795: BSD version of ctypes.util.find_library, by Martin
Thomas Heller <theller@ctypes.org>
parents: 39075
diff changeset
168
cf8af6b7598b patch #1610795: BSD version of ctypes.util.find_library, by Martin
Thomas Heller <theller@ctypes.org>
parents: 39075
diff changeset
169 def find_library(name):
cf8af6b7598b patch #1610795: BSD version of ctypes.util.find_library, by Martin
Thomas Heller <theller@ctypes.org>
parents: 39075
diff changeset
170 ename = re.escape(name)
cf8af6b7598b patch #1610795: BSD version of ctypes.util.find_library, by Martin
Thomas Heller <theller@ctypes.org>
parents: 39075
diff changeset
171 expr = r':-l%s\.\S+ => \S*/(lib%s\.\S+)' % (ename, ename)
56682
db21c3c009db Per the discussion in issue6882, backport the try/finally work that was done to the py3k version (mostly in r59477, I think).
Eric Smith <eric@trueblade.com>
parents: 53807
diff changeset
172 f = os.popen('/sbin/ldconfig -r 2>/dev/null')
db21c3c009db Per the discussion in issue6882, backport the try/finally work that was done to the py3k version (mostly in r59477, I think).
Eric Smith <eric@trueblade.com>
parents: 53807
diff changeset
173 try:
db21c3c009db Per the discussion in issue6882, backport the try/finally work that was done to the py3k version (mostly in r59477, I think).
Eric Smith <eric@trueblade.com>
parents: 53807
diff changeset
174 data = f.read()
db21c3c009db Per the discussion in issue6882, backport the try/finally work that was done to the py3k version (mostly in r59477, I think).
Eric Smith <eric@trueblade.com>
parents: 53807
diff changeset
175 finally:
db21c3c009db Per the discussion in issue6882, backport the try/finally work that was done to the py3k version (mostly in r59477, I think).
Eric Smith <eric@trueblade.com>
parents: 53807
diff changeset
176 f.close()
db21c3c009db Per the discussion in issue6882, backport the try/finally work that was done to the py3k version (mostly in r59477, I think).
Eric Smith <eric@trueblade.com>
parents: 53807
diff changeset
177 res = re.findall(expr, data)
40612
cf8af6b7598b patch #1610795: BSD version of ctypes.util.find_library, by Martin
Thomas Heller <theller@ctypes.org>
parents: 39075
diff changeset
178 if not res:
cf8af6b7598b patch #1610795: BSD version of ctypes.util.find_library, by Martin
Thomas Heller <theller@ctypes.org>
parents: 39075
diff changeset
179 return _get_soname(_findLib_gcc(name))
cf8af6b7598b patch #1610795: BSD version of ctypes.util.find_library, by Martin
Thomas Heller <theller@ctypes.org>
parents: 39075
diff changeset
180 res.sort(cmp= lambda x,y: cmp(_num_version(x), _num_version(y)))
cf8af6b7598b patch #1610795: BSD version of ctypes.util.find_library, by Martin
Thomas Heller <theller@ctypes.org>
parents: 39075
diff changeset
181 return res[-1]
cf8af6b7598b patch #1610795: BSD version of ctypes.util.find_library, by Martin
Thomas Heller <theller@ctypes.org>
parents: 39075
diff changeset
182
cf8af6b7598b patch #1610795: BSD version of ctypes.util.find_library, by Martin
Thomas Heller <theller@ctypes.org>
parents: 39075
diff changeset
183 else:
cf8af6b7598b patch #1610795: BSD version of ctypes.util.find_library, by Martin
Thomas Heller <theller@ctypes.org>
parents: 39075
diff changeset
184
50704
3b5be0ee32ea Merge r68487 from svn+ssh://pythondev@svn.python.org/python/trunk:
Matthias Klose <doko@ubuntu.com>
parents: 47113
diff changeset
185 def _findSoname_ldconfig(name):
3b5be0ee32ea Merge r68487 from svn+ssh://pythondev@svn.python.org/python/trunk:
Matthias Klose <doko@ubuntu.com>
parents: 47113
diff changeset
186 import struct
3b5be0ee32ea Merge r68487 from svn+ssh://pythondev@svn.python.org/python/trunk:
Matthias Klose <doko@ubuntu.com>
parents: 47113
diff changeset
187 if struct.calcsize('l') == 4:
3b5be0ee32ea Merge r68487 from svn+ssh://pythondev@svn.python.org/python/trunk:
Matthias Klose <doko@ubuntu.com>
parents: 47113
diff changeset
188 machine = os.uname()[4] + '-32'
3b5be0ee32ea Merge r68487 from svn+ssh://pythondev@svn.python.org/python/trunk:
Matthias Klose <doko@ubuntu.com>
parents: 47113
diff changeset
189 else:
3b5be0ee32ea Merge r68487 from svn+ssh://pythondev@svn.python.org/python/trunk:
Matthias Klose <doko@ubuntu.com>
parents: 47113
diff changeset
190 machine = os.uname()[4] + '-64'
3b5be0ee32ea Merge r68487 from svn+ssh://pythondev@svn.python.org/python/trunk:
Matthias Klose <doko@ubuntu.com>
parents: 47113
diff changeset
191 mach_map = {
3b5be0ee32ea Merge r68487 from svn+ssh://pythondev@svn.python.org/python/trunk:
Matthias Klose <doko@ubuntu.com>
parents: 47113
diff changeset
192 'x86_64-64': 'libc6,x86-64',
3b5be0ee32ea Merge r68487 from svn+ssh://pythondev@svn.python.org/python/trunk:
Matthias Klose <doko@ubuntu.com>
parents: 47113
diff changeset
193 'ppc64-64': 'libc6,64bit',
3b5be0ee32ea Merge r68487 from svn+ssh://pythondev@svn.python.org/python/trunk:
Matthias Klose <doko@ubuntu.com>
parents: 47113
diff changeset
194 'sparc64-64': 'libc6,64bit',
3b5be0ee32ea Merge r68487 from svn+ssh://pythondev@svn.python.org/python/trunk:
Matthias Klose <doko@ubuntu.com>
parents: 47113
diff changeset
195 's390x-64': 'libc6,64bit',
3b5be0ee32ea Merge r68487 from svn+ssh://pythondev@svn.python.org/python/trunk:
Matthias Klose <doko@ubuntu.com>
parents: 47113
diff changeset
196 'ia64-64': 'libc6,IA-64',
3b5be0ee32ea Merge r68487 from svn+ssh://pythondev@svn.python.org/python/trunk:
Matthias Klose <doko@ubuntu.com>
parents: 47113
diff changeset
197 }
3b5be0ee32ea Merge r68487 from svn+ssh://pythondev@svn.python.org/python/trunk:
Matthias Klose <doko@ubuntu.com>
parents: 47113
diff changeset
198 abi_type = mach_map.get(machine, 'libc6')
3b5be0ee32ea Merge r68487 from svn+ssh://pythondev@svn.python.org/python/trunk:
Matthias Klose <doko@ubuntu.com>
parents: 47113
diff changeset
199
3b5be0ee32ea Merge r68487 from svn+ssh://pythondev@svn.python.org/python/trunk:
Matthias Klose <doko@ubuntu.com>
parents: 47113
diff changeset
200 # XXX assuming GLIBC's ldconfig (with option -p)
74931
be41abd74949 Issue #13979: Fix ctypes.util.find_library ldconfig regex
Meador Inge <meadori@gmail.com>
parents: 68256
diff changeset
201 expr = r'\s+(lib%s\.[^\s]+)\s+\(%s' % (re.escape(name), abi_type)
56682
db21c3c009db Per the discussion in issue6882, backport the try/finally work that was done to the py3k version (mostly in r59477, I think).
Eric Smith <eric@trueblade.com>
parents: 53807
diff changeset
202 f = os.popen('/sbin/ldconfig -p 2>/dev/null')
db21c3c009db Per the discussion in issue6882, backport the try/finally work that was done to the py3k version (mostly in r59477, I think).
Eric Smith <eric@trueblade.com>
parents: 53807
diff changeset
203 try:
db21c3c009db Per the discussion in issue6882, backport the try/finally work that was done to the py3k version (mostly in r59477, I think).
Eric Smith <eric@trueblade.com>
parents: 53807
diff changeset
204 data = f.read()
db21c3c009db Per the discussion in issue6882, backport the try/finally work that was done to the py3k version (mostly in r59477, I think).
Eric Smith <eric@trueblade.com>
parents: 53807
diff changeset
205 finally:
db21c3c009db Per the discussion in issue6882, backport the try/finally work that was done to the py3k version (mostly in r59477, I think).
Eric Smith <eric@trueblade.com>
parents: 53807
diff changeset
206 f.close()
db21c3c009db Per the discussion in issue6882, backport the try/finally work that was done to the py3k version (mostly in r59477, I think).
Eric Smith <eric@trueblade.com>
parents: 53807
diff changeset
207 res = re.search(expr, data)
50704
3b5be0ee32ea Merge r68487 from svn+ssh://pythondev@svn.python.org/python/trunk:
Matthias Klose <doko@ubuntu.com>
parents: 47113
diff changeset
208 if not res:
3b5be0ee32ea Merge r68487 from svn+ssh://pythondev@svn.python.org/python/trunk:
Matthias Klose <doko@ubuntu.com>
parents: 47113
diff changeset
209 return None
3b5be0ee32ea Merge r68487 from svn+ssh://pythondev@svn.python.org/python/trunk:
Matthias Klose <doko@ubuntu.com>
parents: 47113
diff changeset
210 return res.group(1)
3b5be0ee32ea Merge r68487 from svn+ssh://pythondev@svn.python.org/python/trunk:
Matthias Klose <doko@ubuntu.com>
parents: 47113
diff changeset
211
40612
cf8af6b7598b patch #1610795: BSD version of ctypes.util.find_library, by Martin
Thomas Heller <theller@ctypes.org>
parents: 39075
diff changeset
212 def find_library(name):
50704
3b5be0ee32ea Merge r68487 from svn+ssh://pythondev@svn.python.org/python/trunk:
Matthias Klose <doko@ubuntu.com>
parents: 47113
diff changeset
213 return _findSoname_ldconfig(name) or _get_soname(_findLib_gcc(name))
38166
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
214
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
215 ################################################################
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
216 # test code
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
217
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
218 def test():
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
219 from ctypes import cdll
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
220 if os.name == "nt":
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
221 print cdll.msvcrt
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
222 print cdll.load("msvcrt")
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
223 print find_library("msvcrt")
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
224
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
225 if os.name == "posix":
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
226 # find and load_version
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
227 print find_library("m")
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
228 print find_library("c")
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
229 print find_library("bz2")
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
230
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
231 # getattr
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
232 ## print cdll.m
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
233 ## print cdll.bz2
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
234
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
235 # load
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
236 if sys.platform == "darwin":
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
237 print cdll.LoadLibrary("libm.dylib")
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
238 print cdll.LoadLibrary("libcrypto.dylib")
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
239 print cdll.LoadLibrary("libSystem.dylib")
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
240 print cdll.LoadLibrary("System.framework/System")
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
241 else:
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
242 print cdll.LoadLibrary("libm.so")
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
243 print cdll.LoadLibrary("libcrypt.so")
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
244 print find_library("crypt")
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
245
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
246 if __name__ == "__main__":
574ffc4d7bf6 Add missing svn properties.
Thomas Heller <theller@ctypes.org>
parents: 37906
diff changeset
247 test()