From 24c63f39c491ee711ca87af94c3ae039f699bd1b Mon Sep 17 00:00:00 2001 From: Raf Gemmail Date: Sun, 26 Nov 2023 17:39:04 +1300 Subject: [PATCH 1/5] find_library uses ldconfig from $PATH on linux --- Lib/ctypes/util.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Lib/ctypes/util.py b/Lib/ctypes/util.py index 0c2510e1619c8ed..751ef16bf7df484 100644 --- a/Lib/ctypes/util.py +++ b/Lib/ctypes/util.py @@ -90,7 +90,7 @@ def find_library(name): from ctypes._aix import find_library elif os.name == "posix": - # Andreas Degert's find functions, using gcc, /sbin/ldconfig, objdump + # Andreas Degert's find functions, using gcc, ldconfig, objdump import re, tempfile def _is_elf(filename): @@ -266,8 +266,9 @@ def find_library(name, is64 = False): return _get_soname(_findLib_crle(name, is64) or _findLib_gcc(name)) else: - + # Linux distros and any others which did not match on sys.platform def _findSoname_ldconfig(name): + ldconfig_bin = shutil.which("ldconfig") or "/sbin/ldconfig" import struct if struct.calcsize('l') == 4: machine = os.uname().machine + '-32' @@ -281,12 +282,11 @@ def _findSoname_ldconfig(name): 'ia64-64': 'libc6,IA-64', } abi_type = mach_map.get(machine, 'libc6') - # XXX assuming GLIBC's ldconfig (with option -p) regex = r'\s+(lib%s\.[^\s]+)\s+\(%s' regex = os.fsencode(regex % (re.escape(name), abi_type)) try: - with subprocess.Popen(['/sbin/ldconfig', '-p'], + with subprocess.Popen([ldconfig_bin, '-p'], stdin=subprocess.DEVNULL, stderr=subprocess.DEVNULL, stdout=subprocess.PIPE, From 8529d9167090c304cfd321af9d5c2eedafcf3943 Mon Sep 17 00:00:00 2001 From: Raf Gemmail Date: Sun, 26 Nov 2023 18:23:48 +1300 Subject: [PATCH 2/5] find_library ldconfig from $PATH on BSDs --- Lib/ctypes/util.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/ctypes/util.py b/Lib/ctypes/util.py index 751ef16bf7df484..aa15231ed3da2a5 100644 --- a/Lib/ctypes/util.py +++ b/Lib/ctypes/util.py @@ -204,12 +204,13 @@ def _num_version(libname): return nums or [sys.maxsize] def find_library(name): + ldconfig_bin = shutil.which('ldconfig') or '/sbin/ldconfig' ename = re.escape(name) expr = r':-l%s\.\S+ => \S*/(lib%s\.\S+)' % (ename, ename) expr = os.fsencode(expr) try: - proc = subprocess.Popen(('/sbin/ldconfig', '-r'), + proc = subprocess.Popen((ldconfig_bin, '-r'), stdout=subprocess.PIPE, stderr=subprocess.DEVNULL) except OSError: # E.g. command not found From e8d7c9224b3bdaa6e02fe4731343c67226cb8fd2 Mon Sep 17 00:00:00 2001 From: Raf Gemmail Date: Sun, 26 Nov 2023 18:57:38 +1300 Subject: [PATCH 3/5] chore - standardised quotes --- Lib/ctypes/util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/ctypes/util.py b/Lib/ctypes/util.py index aa15231ed3da2a5..dce51eff266abfe 100644 --- a/Lib/ctypes/util.py +++ b/Lib/ctypes/util.py @@ -269,7 +269,7 @@ def find_library(name, is64 = False): else: # Linux distros and any others which did not match on sys.platform def _findSoname_ldconfig(name): - ldconfig_bin = shutil.which("ldconfig") or "/sbin/ldconfig" + ldconfig_bin = shutil.which('ldconfig') or '/sbin/ldconfig' import struct if struct.calcsize('l') == 4: machine = os.uname().machine + '-32' From db6220c4f6f89175d5a7365bf31714750de26533 Mon Sep 17 00:00:00 2001 From: Raf Gemmail Date: Sun, 26 Nov 2023 19:01:26 +1300 Subject: [PATCH 4/5] docs - clarified behaviour around find_location in Linux --- Doc/library/ctypes.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Doc/library/ctypes.rst b/Doc/library/ctypes.rst index ef3a9a0f5898af4..ec36d24e154e9dc 100644 --- a/Doc/library/ctypes.rst +++ b/Doc/library/ctypes.rst @@ -1317,8 +1317,9 @@ the library to load. The exact functionality is system dependent. On Linux, :func:`~ctypes.util.find_library` tries to run external programs -(``/sbin/ldconfig``, ``gcc``, ``objdump`` and ``ld``) to find the library file. -It returns the filename of the library file. +(``ldconfig``, ``gcc``, ``objdump`` and ``ld``) to find the library file. +Note that ``ldconfig`` falls back to using ``/sbin/ldconfig`` if it is not found on ``$PATH``. +Unlike other distributions it returns only the filename of the library file. .. versionchanged:: 3.6 On Linux, the value of the environment variable ``LD_LIBRARY_PATH`` is used From 287cf1c1b191539981a1e5ce9598809d017e50e6 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Mon, 27 Nov 2023 01:07:28 +0000 Subject: [PATCH 5/5] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2023-11-27-01-07-27.gh-issue-112417.6UU0jx.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2023-11-27-01-07-27.gh-issue-112417.6UU0jx.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-11-27-01-07-27.gh-issue-112417.6UU0jx.rst b/Misc/NEWS.d/next/Core and Builtins/2023-11-27-01-07-27.gh-issue-112417.6UU0jx.rst new file mode 100644 index 000000000000000..cdf52e1e22640c7 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2023-11-27-01-07-27.gh-issue-112417.6UU0jx.rst @@ -0,0 +1 @@ +For compatibility across Linux distros ``ctypes.util.find_library`` has been modified to favour a version of ``ldconfig`` on the current ``$PATH``, falling back to ``/sbin/ldconfig`` if there isn't another version available.