diff --git a/Lib/ctypes/util.py b/Lib/ctypes/util.py index 1e882e3..b265cc0 100644 --- a/Lib/ctypes/util.py +++ b/Lib/ctypes/util.py @@ -238,8 +238,37 @@ elif os.name == "posix": return None return res.group(1) + def _is_elf(filepath): + try: + with open(filepath, 'rb') as fh: + return fh.read(4) == b'\x7fELF' + except: + return False + + def _find_libfile(name): + from glob import glob + # special case for libm, libcrypt and libpthread and musl + if name in ['m', 'crypt', 'pthread']: + name = 'c' + elif name in ['libm.so', 'libcrypt.so', 'libpthread.so']: + name = 'libc.so' + # search in standard locations + paths = ['/lib', '/usr/lib', '/usr/local/lib'] + if 'LD_LIBRARY_PATH' in os.environ: + paths = os.environ['LD_LIBRARY_PATH'].split(':') + paths + for d in paths: + f = os.path.join(d, name) + if _is_elf(f): + return os.path.basename(f) + + prefix = os.path.join(d, 'lib'+name) + for suffix in ['.so', '.so.*', '.*.so.*']: + for f in glob('{0}{1}'.format(prefix, suffix)): + if _is_elf(f): + return os.path.basename(f) + def find_library(name): - return _findSoname_ldconfig(name) or _get_soname(_findLib_gcc(name)) + return _find_libfile(name) or _findSoname_ldconfig(name) or _get_soname(_findLib_gcc(name)) ################################################################ # test code