Skip to content

Commit abefdce

Browse files
charliermarshpradyunsg
authored andcommitted
Handle dlopen(NULL) failure in glibc fallback (pypa#12716)
Co-authored-by: Pradyun Gedam <[email protected]>
1 parent 386a54f commit abefdce

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

news/12716.bugfix.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Avoid dlopen failure for glibc detection in musl builds

src/pip/_internal/utils/glibc.py

+15-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,20 @@ def glibc_version_string_ctypes():
4949
# manpage says, "If filename is NULL, then the returned handle is for the
5050
# main program". This way we can let the linker do the work to figure out
5151
# which libc our process is actually using.
52-
process_namespace = ctypes.CDLL(None)
52+
#
53+
# We must also handle the special case where the executable is not a
54+
# dynamically linked executable. This can occur when using musl libc,
55+
# for example. In this situation, dlopen() will error, leading to an
56+
# OSError. Interestingly, at least in the case of musl, there is no
57+
# errno set on the OSError. The single string argument used to construct
58+
# OSError comes from libc itself and is therefore not portable to
59+
# hard code here. In any case, failure to call dlopen() means we
60+
# can't proceed, so we bail on our attempt.
61+
try:
62+
process_namespace = ctypes.CDLL(None)
63+
except OSError:
64+
return None
65+
5366
try:
5467
gnu_get_libc_version = process_namespace.gnu_get_libc_version
5568
except AttributeError:
@@ -59,7 +72,7 @@ def glibc_version_string_ctypes():
5972

6073
# Call gnu_get_libc_version, which returns a string like "2.5"
6174
gnu_get_libc_version.restype = ctypes.c_char_p
62-
version_str = gnu_get_libc_version()
75+
version_str: str = gnu_get_libc_version()
6376
# py2 / py3 compatibility:
6477
if not isinstance(version_str, str):
6578
version_str = version_str.decode("ascii")

0 commit comments

Comments
 (0)