Skip to content

Commit f57ac10

Browse files
committed
Issue #21622 Walk LD_LIBRARY_PATH for library
FIXES bpo-21622 for 3.8
1 parent 11f0f11 commit f57ac10

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

Lib/ctypes/util.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,10 +306,38 @@ def _findLib_ld(name):
306306
pass # result will be None
307307
return result
308308

309+
310+
def _findLib_walkldpath(name):
311+
312+
def _is_elf(filepath):
313+
try:
314+
with open(filepath, 'rb') as fh:
315+
return fh.read(4) == b'\x7fELF'
316+
except:
317+
return False
318+
319+
from glob import glob
320+
if os.path.isabs(name):
321+
return name
322+
# search LD_LIBRARY_PATH list
323+
paths = os.environ.get('LD_LIBRARY_PATH', '').split(':')
324+
if paths:
325+
for d in paths:
326+
f = os.path.join(d, name)
327+
if _is_elf(f):
328+
return os.path.basename(f)
329+
prefix = os.path.join(d, 'lib'+name)
330+
for suffix in ['.so', '.so.*']:
331+
for f in glob('{0}{1}'.format(prefix, suffix)):
332+
if _is_elf(f):
333+
return os.path.basename(f)
334+
309335
def find_library(name):
310336
# See issue #9998
311337
return _findSoname_ldconfig(name) or \
312-
_get_soname(_findLib_gcc(name) or _findLib_ld(name))
338+
_get_soname(_findLib_gcc(name) or \
339+
_findLib_ld(name)) or \
340+
_findLib_walkldpath(name)
313341

314342
################################################################
315343
# test code

0 commit comments

Comments
 (0)