From 0e3163f48ce5cce50fd8e7a0f70c90881aa75fc2 Mon Sep 17 00:00:00 2001 From: Javier Castillo II Date: Sat, 10 Nov 2018 13:35:21 -0600 Subject: [PATCH 1/2] Issue #21622 Walk LD_LIBRARY_PATH for library FIXES bpo-21622 for 2.7 --- Lib/ctypes/util.py | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/Lib/ctypes/util.py b/Lib/ctypes/util.py index ab10ec52ee8c35..dffa1500f468ef 100644 --- a/Lib/ctypes/util.py +++ b/Lib/ctypes/util.py @@ -270,8 +270,34 @@ def _findSoname_ldconfig(name): return None return res.group(1) + def _findWalk_ldpath(name): + def _is_elf(filepath): + try: + with open(filepath, 'rb') as fh: + return fh.read(4) == b'\x7fELF' + except: + return False + from glob import glob + + if os.path.isabs(name): + return name + # search LD_LIBRARY_PATH list + paths = os.environ.get('LD_LIBRARY_PATH', '').split(':') + if 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.*']: + 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 _findSoname_ldconfig(name) or \ + _get_soname(_findLib_gcc(name)) or \ + _findWalk_ldpath(name) ################################################################ # test code From 14096d171b52bbd1f95cb01214b19b8e360f3050 Mon Sep 17 00:00:00 2001 From: Javier Castillo II Date: Sat, 10 Nov 2018 15:15:20 -0600 Subject: [PATCH 2/2] Style update --- Lib/ctypes/util.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Lib/ctypes/util.py b/Lib/ctypes/util.py index dffa1500f468ef..3d7d24d0722387 100644 --- a/Lib/ctypes/util.py +++ b/Lib/ctypes/util.py @@ -271,12 +271,14 @@ def _findSoname_ldconfig(name): return res.group(1) def _findWalk_ldpath(name): + def _is_elf(filepath): try: with open(filepath, 'rb') as fh: return fh.read(4) == b'\x7fELF' except: return False + from glob import glob if os.path.isabs(name):