Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(159034)

Delta Between Two Patch Sets: Lib/ctypes/util.py

Issue 21042: ctypes.util.find_library() should return full pathname instead of filename in linux
Left Patch Set: Created 3 years, 10 months ago
Right Patch Set: Created 3 years, 9 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/ctypes/test/test_find.py ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 import sys, os 1 import sys, os
2 import contextlib 2 import contextlib
3 import subprocess 3 import subprocess
4 4
5 # find_library(name) returns the pathname of a library, or None. 5 # find_library(name) returns the pathname of a library, or None.
6 if os.name == "nt": 6 if os.name == "nt":
7 7
8 def _get_build_version(): 8 def _get_build_version():
9 """Return the version of MSVC that was used to build Python. 9 """Return the version of MSVC that was used to build Python.
10 10
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after
215 'x86_64-64': 'libc6,x86-64', 215 'x86_64-64': 'libc6,x86-64',
216 'ppc64-64': 'libc6,64bit', 216 'ppc64-64': 'libc6,64bit',
217 'sparc64-64': 'libc6,64bit', 217 'sparc64-64': 'libc6,64bit',
218 's390x-64': 'libc6,64bit', 218 's390x-64': 'libc6,64bit',
219 'ia64-64': 'libc6,IA-64', 219 'ia64-64': 'libc6,IA-64',
220 } 220 }
221 abi_type = mach_map.get(machine, 'libc6') 221 abi_type = mach_map.get(machine, 'libc6')
222 222
223 # XXX assuming GLIBC's ldconfig (with option -p) 223 # XXX assuming GLIBC's ldconfig (with option -p)
224 regex = os.fsencode( 224 regex = os.fsencode(
225 '([^\s]+lib%s\.[^\s]+)\\n\\t[^\s]+\s\(%s' % (re.escape(name), ab i_type)) 225 r'lib%s\.[^\s]+\s\(%s(?:,\s.*)?\)\s=>\s(.*)' % (re.escape(name), abi_type))
Martin Panter 2016/02/07 10:27:06 I suggest a raw string (r'. . .') to avoid the dou
226 try: 226 try:
227 with subprocess.Popen(['/sbin/ldconfig', '-p'], 227 with subprocess.Popen(['/sbin/ldconfig', '-p'],
228 stdin=subprocess.DEVNULL, 228 stdin=subprocess.DEVNULL,
229 stderr=subprocess.DEVNULL, 229 stderr=subprocess.DEVNULL,
230 stdout=subprocess.PIPE, 230 stdout=subprocess.PIPE,
231 env={'LC_ALL': 'C', 'LANG': 'C'}) as p: 231 env={'LC_ALL': 'C', 'LANG': 'C'}) as p:
232 res = re.search(regex, p.stdout.read()) 232 res = re.search(regex, p.stdout.read())
233 if res: 233 if res:
234 return os.fsdecode(res.group(1)) 234 return os.fsdecode(res.group(1))
235 except OSError: 235 except OSError:
236 pass 236 pass
237 237
238 def find_library(name): 238 def find_library(name):
239 return _findSoname_ldconfig(name) or _get_soname(_findLib_gcc(name)) 239 return _findSoname_ldconfig(name) or _get_soname(_findLib_gcc(name))
240 240
241 ################################################################ 241 ################################################################
242 # test code 242 # test code
243 243
244 def test(): 244 def test():
245 from ctypes import cdll 245 from ctypes import cdll
246 from os.path import isabs
Martin Panter 2016/02/07 10:27:06 Not needed if you use the full os.path.isabs()
247 if os.name == "nt": 246 if os.name == "nt":
248 print(cdll.msvcrt) 247 print(cdll.msvcrt)
249 print(cdll.load("msvcrt")) 248 print(cdll.load("msvcrt"))
250 print(find_library("msvcrt")) 249 print(find_library("msvcrt"))
251 250
252 if os.name == "posix": 251 if os.name == "posix":
253 # find and load_version 252 # find and load_version
254 print(find_library("m"), os.path.isabs(find_library("m"))) 253 print(find_library("m"))
255 print(find_library("c"), os.path.isabs(find_library("c"))) 254 print(find_library("c"))
256 print(find_library("bz2"), os.path.isabs(find_library("bz2"))) 255 print(find_library("bz2"))
257 256
258 # getattr 257 # getattr
259 ## print cdll.m 258 ## print cdll.m
260 ## print cdll.bz2 259 ## print cdll.bz2
261 260
262 # load 261 # load
263 if sys.platform == "darwin": 262 if sys.platform == "darwin":
264 print(cdll.LoadLibrary("libm.dylib")) 263 print(cdll.LoadLibrary("libm.dylib"))
265 print(cdll.LoadLibrary("libcrypto.dylib")) 264 print(cdll.LoadLibrary("libcrypto.dylib"))
266 print(cdll.LoadLibrary("libSystem.dylib")) 265 print(cdll.LoadLibrary("libSystem.dylib"))
267 print(cdll.LoadLibrary("System.framework/System")) 266 print(cdll.LoadLibrary("System.framework/System"))
268 else: 267 else:
269 print(cdll.LoadLibrary("libm.so")) 268 print(cdll.LoadLibrary("libm.so"))
270 print(cdll.LoadLibrary("libcrypt.so")) 269 print(cdll.LoadLibrary("libcrypt.so"))
271 print(find_library("crypt")) 270 print(find_library("crypt"))
272 271
273 if __name__ == "__main__": 272 if __name__ == "__main__":
274 test() 273 test()
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+