Debian Codegraph @

Lib/ctypes/util.py

raw
1 import os
2 import shutil
3 import subprocess
4 import sys
5
6 # find_library(name) returns the pathname of a library, or None.
7 if os.name == "nt":
8
9 def _get_build_version():
10 """Return the version of MSVC that was used to build Python.
11
12 For Python 2.3 and up, the version number is included in
13 sys.version. For earlier versions, assume the compiler is MSVC 6.
14 """
15 # This function was copied from Lib/distutils/msvccompiler.py
16 prefix = "MSC v."
17 i = sys.version.find(prefix)
18 if i == -1:
19 return 6
20 i = i + len(prefix)
21 s, rest = sys.version[i:].split(" ", 1)
22 majorVersion = int(s[:-2]) - 6
23 if majorVersion >= 13:
24 majorVersion += 1
25 minorVersion = int(s[2:3]) / 10.0
26 # I don't think paths are affected by minor version in version 6
27 if majorVersion == 6:
28 minorVersion = 0
29 if majorVersion >= 6:
30 return majorVersion + minorVersion
31 # else we don't know what version of the compiler this is
32 return None
33
34 def find_msvcrt():
35 """Return the name of the VC runtime dll"""
36 version = _get_build_version()
37 if version is None:
38 # better be safe than sorry
39 return None
40 if version <= 6:
41 clibname = 'msvcrt'
42 elif version <= 13:
43 clibname = 'msvcr%d' % (version * 10)
44 else:
45 # CRT is no longer directly loadable. See issue23606 for the
46 # discussion about alternative approaches.
47 return None
48
49 # If python was built with in debug mode
50 import importlib.machinery
51 if '_d.pyd' in importlib.machinery.EXTENSION_SUFFIXES:
52 clibname += 'd'
53 return clibname+'.dll'
54
55 def find_library(name):
56 if name in ('c', 'm'):
57 return find_msvcrt()
58 # See MSDN for the REAL search order.
59 for directory in os.environ['PATH'].split(os.pathsep):
60 fname = os.path.join(directory, name)
61 if os.path.isfile(fname):
62 return fname
63 if fname.lower().endswith(".dll"):
64 continue
65 fname = fname + ".dll"
66 if os.path.isfile(fname):
67 return fname
68 return None
69
70 # Listing loaded DLLs on Windows relies on the following APIs:
71 # https://learn.microsoft.com/windows/win32/api/psapi/nf-psapi-enumprocessmodules
72 # https://learn.microsoft.com/windows/win32/api/libloaderapi/nf-libloaderapi-getmodulefilenamew
73 import ctypes
74 from ctypes import wintypes
75
76 _kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)
77 _get_current_process = _kernel32["GetCurrentProcess"]
78 _get_current_process.restype = wintypes.HANDLE
79
80 _k32_get_module_file_name = _kernel32["GetModuleFileNameW"]
81 _k32_get_module_file_name.restype = wintypes.DWORD
82 _k32_get_module_file_name.argtypes = (
83 wintypes.HMODULE,
84 wintypes.LPWSTR,
85 wintypes.DWORD,
86 )
87
88 # gh-145307: We defer loading psapi.dll until _get_module_handles is called.
89 # Loading additional DLLs at startup for functionality that may never be
90 # used is wasteful.
91 _enum_process_modules = None
92
93 def _get_module_filename(module: wintypes.HMODULE):
94 name = (wintypes.WCHAR * 32767)() # UNICODE_STRING_MAX_CHARS
95 if _k32_get_module_file_name(module, name, len(name)):
96 return name.value
97 return None
98
99 def _get_module_handles():
100 global _enum_process_modules
101 if _enum_process_modules is None:
102 _psapi = ctypes.WinDLL('psapi', use_last_error=True)
103 _enum_process_modules = _psapi["EnumProcessModules"]
104 _enum_process_modules.restype = wintypes.BOOL
105 _enum_process_modules.argtypes = (
106 wintypes.HANDLE,
107 ctypes.POINTER(wintypes.HMODULE),
108 wintypes.DWORD,
109 wintypes.LPDWORD,
110 )
111
112 process = _get_current_process()
113 space_needed = wintypes.DWORD()
114 n = 1024
115 while True:
116 modules = (wintypes.HMODULE * n)()
117 if not _enum_process_modules(process,
118 modules,
119 ctypes.sizeof(modules),
120 ctypes.byref(space_needed)):
121 err = ctypes.get_last_error()
122 msg = ctypes.FormatError(err).strip()
123 raise ctypes.WinError(err, f"EnumProcessModules failed: {msg}")
124 n = space_needed.value // ctypes.sizeof(wintypes.HMODULE)
125 if n <= len(modules):
126 return modules[:n]
127
128 def dllist():
129 """Return a list of loaded shared libraries in the current process."""
130 modules = _get_module_handles()
131 libraries = [name for h in modules
132 if (name := _get_module_filename(h)) is not None]
133 return libraries
134
135 elif os.name == "posix" and sys.platform in {"darwin", "ios", "tvos", "watchos"}:
136 from ctypes.macholib.dyld import dyld_find as _dyld_find
137 def find_library(name):
138 possible = ['lib%s.dylib' % name,
139 '%s.dylib' % name,
140 '%s.framework/%s' % (name, name)]
141 for name in possible:
142 try:
143 return _dyld_find(name)
144 except ValueError:
145 continue
146 return None
147
148 # Listing loaded libraries on Apple systems relies on the following API:
149 # https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/dyld.3.html
150 import ctypes
151
152 _libc = ctypes.CDLL(find_library("c"))
153 _dyld_get_image_name = _libc["_dyld_get_image_name"]
154 _dyld_get_image_name.restype = ctypes.c_char_p
155
156 def dllist():
157 """Return a list of loaded shared libraries in the current process."""
158 num_images = _libc._dyld_image_count()
159 libraries = [os.fsdecode(name) for i in range(num_images)
160 if (name := _dyld_get_image_name(i)) is not None]
161
162 return libraries
163
164 elif sys.platform.startswith("aix"):
165 # AIX has two styles of storing shared libraries
166 # GNU auto_tools refer to these as svr4 and aix
167 # svr4 (System V Release 4) is a regular file, often with .so as suffix
168 # AIX style uses an archive (suffix .a) with members (e.g., shr.o, libssl.so)
169 # see issue#26439 and _aix.py for more details
170
171 from ctypes._aix import find_library
172
173 elif sys.platform == "android":
174 def find_library(name):
175 directory = "/system/lib"
176 if "64" in os.uname().machine:
177 directory += "64"
178
179 fname = f"{directory}/lib{name}.so"
180 return fname if os.path.isfile(fname) else None
181
182 elif sys.platform == "emscripten":
183 def _is_wasm(filename):
184 # Return True if the given file is an WASM module
185 wasm_header = b"\x00asm"
186 with open(filename, 'br') as thefile:
187 return thefile.read(4) == wasm_header
188
189 def find_library(name):
190 candidates = [f"lib{name}.so", f"lib{name}.wasm"]
191 paths = os.environ.get("LD_LIBRARY_PATH", "")
192 for libdir in paths.split(":"):
193 for name in candidates:
194 libfile = os.path.join(libdir, name)
195
196 if os.path.isfile(libfile) and _is_wasm(libfile):
197 return libfile
198
199 return None
200
201 elif os.name == "posix":
202 # Andreas Degert's find functions, using gcc, /sbin/ldconfig, objdump
203 import re, tempfile
204
205 def _is_elf(filename):
206 "Return True if the given file is an ELF file"
207 elf_header = b'\x7fELF'
208 try:
209 with open(filename, 'br') as thefile:
210 return thefile.read(4) == elf_header
211 except FileNotFoundError:
212 return False
213
214 def _findLib_gcc(name):
215 # Run GCC's linker with the -t (aka --trace) option and examine the
216 # library name it prints out. The GCC command will fail because we
217 # haven't supplied a proper program with main(), but that does not
218 # matter.
219 expr = os.fsencode(r'[^\(\)\s]*lib%s\.[^\(\)\s]*' % re.escape(name))
220
221 c_compiler = shutil.which('gcc')
222 if not c_compiler:
223 c_compiler = shutil.which('cc')
224 if not c_compiler:
225 # No C compiler available, give up
226 return None
227
228 temp = tempfile.NamedTemporaryFile()
229 try:
230 args = [c_compiler, '-Wl,-t', '-o', temp.name, '-l' + name]
231
232 env = dict(os.environ)
233 env['LC_ALL'] = 'C'
234 env['LANG'] = 'C'
235 try:
236 proc = subprocess.Popen(args,
237 stdout=subprocess.PIPE,
238 stderr=subprocess.STDOUT,
239 env=env)
240 except OSError: # E.g. bad executable
241 return None
242 with proc:
243 trace = proc.stdout.read()
244 finally:
245 try:
246 temp.close()
247 except FileNotFoundError:
248 # Raised if the file was already removed, which is the normal
249 # behaviour of GCC if linking fails
250 pass
251 res = re.findall(expr, trace)
252 if not res:
253 return None
254
255 for file in res:
256 # Check if the given file is an elf file: gcc can report
257 # some files that are linker scripts and not actual
258 # shared objects. See bpo-41976 for more details
259 if not _is_elf(file):
260 continue
261 return os.fsdecode(file)
262
263
264 if sys.platform == "sunos5":
265 # use /usr/ccs/bin/dump on solaris
266 def _get_soname(f):
267 if not f:
268 return None
269
270 try:
271 proc = subprocess.Popen(("/usr/ccs/bin/dump", "-Lpv", f),
272 stdout=subprocess.PIPE,
273 stderr=subprocess.DEVNULL)
274 except OSError: # E.g. command not found
275 return None
276 with proc:
277 data = proc.stdout.read()
278 res = re.search(br'\[.*\]\sSONAME\s+([^\s]+)', data)
279 if not res:
280 return None
281 return os.fsdecode(res.group(1))
282 else:
283 def _get_soname(f):
284 # assuming GNU binutils / ELF
285 if not f:
286 return None
287 objdump = shutil.which('objdump')
288 if not objdump:
289 # objdump is not available, give up
290 return None
291
292 try:
293 proc = subprocess.Popen((objdump, '-p', '-j', '.dynamic', f),
294 stdout=subprocess.PIPE,
295 stderr=subprocess.DEVNULL)
296 except OSError: # E.g. bad executable
297 return None
298 with proc:
299 dump = proc.stdout.read()
300 res = re.search(br'\sSONAME\s+([^\s]+)', dump)
301 if not res:
302 return None
303 return os.fsdecode(res.group(1))
304
305 if sys.platform.startswith(("freebsd", "openbsd", "dragonfly")):
306
307 def _num_version(libname):
308 # "libxyz.so.MAJOR.MINOR" => [ MAJOR, MINOR ]
309 parts = libname.split(b".")
310 nums = []
311 try:
312 while parts:
313 nums.insert(0, int(parts.pop()))
314 except ValueError:
315 pass
316 return nums or [sys.maxsize]
317
318 def find_library(name):
319 ename = re.escape(name)
320 expr = r':-l%s\.\S+ => \S*/(lib%s\.\S+)' % (ename, ename)
321 expr = os.fsencode(expr)
322
323 try:
324 proc = subprocess.Popen(('/sbin/ldconfig', '-r'),
325 stdout=subprocess.PIPE,
326 stderr=subprocess.DEVNULL)
327 except OSError: # E.g. command not found
328 data = b''
329 else:
330 with proc:
331 data = proc.stdout.read()
332
333 res = re.findall(expr, data)
334 if not res:
335 return _get_soname(_findLib_gcc(name))
336 res.sort(key=_num_version)
337 return os.fsdecode(res[-1])
338
339 elif sys.platform == "sunos5":
340
341 def _findLib_crle(name, is64):
342 if not os.path.exists('/usr/bin/crle'):
343 return None
344
345 env = dict(os.environ)
346 env['LC_ALL'] = 'C'
347
348 if is64:
349 args = ('/usr/bin/crle', '-64')
350 else:
351 args = ('/usr/bin/crle',)
352
353 paths = None
354 try:
355 proc = subprocess.Popen(args,
356 stdout=subprocess.PIPE,
357 stderr=subprocess.DEVNULL,
358 env=env)
359 except OSError: # E.g. bad executable
360 return None
361 with proc:
362 for line in proc.stdout:
363 line = line.strip()
364 if line.startswith(b'Default Library Path (ELF):'):
365 paths = os.fsdecode(line).split()[4]
366
367 if not paths:
368 return None
369
370 for dir in paths.split(":"):
371 libfile = os.path.join(dir, "lib%s.so" % name)
372 if os.path.exists(libfile):
373 return libfile
374
375 return None
376
377 def find_library(name, is64 = False):
378 return _get_soname(_findLib_crle(name, is64) or _findLib_gcc(name))
379
380 else:
381
382 def _findSoname_ldconfig(name):
383 import struct
384 if struct.calcsize('l') == 4:
385 machine = os.uname().machine + '-32'
386 else:
387 machine = os.uname().machine + '-64'
388 mach_map = {
389 'x86_64-64': 'libc6,x86-64',
390 'ppc64-64': 'libc6,64bit',
391 'sparc64-64': 'libc6,64bit',
392 's390x-64': 'libc6,64bit',
393 'ia64-64': 'libc6,IA-64',
394 }
395 abi_type = mach_map.get(machine, 'libc6')
396
397 # XXX assuming GLIBC's ldconfig (with option -p)
398 regex = r'\s+(lib%s\.[^\s]+)\s+\(%s'
399 regex = os.fsencode(regex % (re.escape(name), abi_type))
400 try:
401 with subprocess.Popen(['/sbin/ldconfig', '-p'],
402 stdin=subprocess.DEVNULL,
403 stderr=subprocess.DEVNULL,
404 stdout=subprocess.PIPE,
405 env={'LC_ALL': 'C', 'LANG': 'C'}) as p:
406 res = re.search(regex, p.stdout.read())
407 if res:
408 return os.fsdecode(res.group(1))
409 except OSError:
410 pass
411
412 def _findLib_ld(name):
413 # See issue #9998 for why this is needed
414 expr = r'[^\(\)\s]*lib%s\.[^\(\)\s]*' % re.escape(name)
415 cmd = ['ld', '-t']
416 libpath = os.environ.get('LD_LIBRARY_PATH')
417 if libpath:
418 for d in libpath.split(':'):
419 cmd.extend(['-L', d])
420 cmd.extend(['-o', os.devnull, '-l%s' % name])
421 result = None
422 try:
423 p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
424 stderr=subprocess.PIPE)
425 out, _ = p.communicate()
426 res = re.findall(expr, os.fsdecode(out))
427 for file in res:
428 # Check if the given file is an elf file: gcc can report
429 # some files that are linker scripts and not actual
430 # shared objects. See bpo-41976 for more details
431 if not _is_elf(file):
432 continue
433 return os.fsdecode(file)
434 except Exception:
435 pass # result will be None
436 return result
437
438 def find_library(name):
439 # See issue #9998
440 return _findSoname_ldconfig(name) or \
441 _get_soname(_findLib_gcc(name)) or _get_soname(_findLib_ld(name))
442
443
444 # On platforms which provide dl_iterate_phdr(), dllist() is implemented
445 # in _ctypes.
446 try:
447 from _ctypes import dllist
448 except ImportError:
449 pass
450
451 ################################################################
452 # test code
453
454 def test():
455 from ctypes import cdll
456 if os.name == "nt":
457 print(cdll.msvcrt)
458 print(cdll.load("msvcrt"))
459 print(find_library("msvcrt"))
460
461 if os.name == "posix":
462 # find and load_version
463 print(find_library("m"))
464 print(find_library("c"))
465 print(find_library("bz2"))
466
467 # load
468 if sys.platform == "darwin":
469 print(cdll.LoadLibrary("libm.dylib"))
470 print(cdll.LoadLibrary("libcrypto.dylib"))
471 print(cdll.LoadLibrary("libSystem.dylib"))
472 print(cdll.LoadLibrary("System.framework/System"))
473 # issue-26439 - fix broken test call for AIX
474 elif sys.platform.startswith("aix"):
475 from ctypes import CDLL
476 if sys.maxsize < 2**32:
477 print(f"Using CDLL(name, os.RTLD_MEMBER): {CDLL('libc.a(shr.o)', os.RTLD_MEMBER)}")
478 print(f"Using cdll.LoadLibrary(): {cdll.LoadLibrary('libc.a(shr.o)')}")
479 # librpm.so is only available as 32-bit shared library
480 print(find_library("rpm"))
481 print(cdll.LoadLibrary("librpm.so"))
482 else:
483 print(f"Using CDLL(name, os.RTLD_MEMBER): {CDLL('libc.a(shr_64.o)', os.RTLD_MEMBER)}")
484 print(f"Using cdll.LoadLibrary(): {cdll.LoadLibrary('libc.a(shr_64.o)')}")
485 print(f"crypt\t:: {find_library('crypt')}")
486 print(f"crypt\t:: {cdll.LoadLibrary(find_library('crypt'))}")
487 print(f"crypto\t:: {find_library('crypto')}")
488 print(f"crypto\t:: {cdll.LoadLibrary(find_library('crypto'))}")
489 else:
490 print(cdll.LoadLibrary("libm.so"))
491 print(cdll.LoadLibrary("libcrypt.so"))
492 print(find_library("crypt"))
493
494 try:
495 dllist
496 except NameError:
497 print('dllist() not available')
498 else:
499 print(dllist())
500
501 if __name__ == "__main__":
502 test()