Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle dlopen(NULL) failure in glibc fallback #12716

Merged
merged 4 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/12716.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Avoid dlopen failure for glibc detection in musl builds
17 changes: 15 additions & 2 deletions src/pip/_internal/utils/glibc.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,20 @@ def glibc_version_string_ctypes() -> Optional[str]:
# manpage says, "If filename is NULL, then the returned handle is for the
# main program". This way we can let the linker do the work to figure out
# which libc our process is actually using.
process_namespace = ctypes.CDLL(None)
#
# We must also handle the special case where the executable is not a
# dynamically linked executable. This can occur when using musl libc,
# for example. In this situation, dlopen() will error, leading to an
# OSError. Interestingly, at least in the case of musl, there is no
# errno set on the OSError. The single string argument used to construct
# OSError comes from libc itself and is therefore not portable to
# hard code here. In any case, failure to call dlopen() means we
# can't proceed, so we bail on our attempt.
try:
process_namespace = ctypes.CDLL(None)
except OSError:
return None

try:
gnu_get_libc_version = process_namespace.gnu_get_libc_version
except AttributeError:
Expand All @@ -50,7 +63,7 @@ def glibc_version_string_ctypes() -> Optional[str]:

# Call gnu_get_libc_version, which returns a string like "2.5"
gnu_get_libc_version.restype = ctypes.c_char_p
version_str = gnu_get_libc_version()
version_str: str = gnu_get_libc_version()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just copied over the full _glibc_version_string_ctypes from _manylinux.py, and this was the only other change. I can back it out if desired.

# py2 / py3 compatibility:
if not isinstance(version_str, str):
version_str = version_str.decode("ascii")
Expand Down
Loading