Skip to content

Commit 9fe988b

Browse files
committed
Handle failure to call dlopen(NULL)
See the inline comment for the explanation. I have been running into this bug on PyOxidizer when using Python distributions built against musl libc. For reference: $ ldd python/install/bin/python3.7m not a dynamic executable $ python/install/bin/python3.7m Python 3.7.7 (default, Apr 5 2020, 06:02:52) [Clang 9.0.1 ] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import ctypes >>> ctypes.CDLL(None) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/gps/src/pyoxidizer.git/build/python_distributions/python.c1ffa330c730/python/install/lib/python3.7/ctypes/__init__.py", line 364, in __init__ self._handle = _dlopen(self._name, mode) OSError: Dynamic loading not supported Interestingly, OSError is incomplete (possibly a CPython ctypes bug?): >>> try: ... ctypes.CDLL(None) ... except OSError as e: ... err = e ... >>> err OSError('Dynamic loading not supported') >>> err.errno None >>> err.strerror None >>> err.args ('Dynamic loading not supported',)
1 parent 61672bf commit 9fe988b

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

CHANGELOG.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Changelog
44
*unreleased*
55
~~~~~~~~~~~~
66

7+
* Handle OSError on non-dynamic executables when attempting to resolve
8+
glibc version string.
79
* Canonicalize version before comparing specifiers. (:issue:`282`)
810
* Change type hint for ``canonicalize_name`` to return
911
``packaging.utils.NormalizedName``.

packaging/tags.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -474,8 +474,20 @@ def _glibc_version_string_ctypes():
474474
# main program". This way we can let the linker do the work to figure out
475475
# which libc our process is actually using.
476476
#
477-
# Note: typeshed is wrong here so we are ignoring this line.
478-
process_namespace = ctypes.CDLL(None) # type: ignore
477+
# We must also handle the special case where the executable is not a
478+
# dynamically linked executable. This can occur when using musl libc,
479+
# for example. In this situation, dlopen() will error, leading to an
480+
# OSError. Interestingly, at least in the case of musl, there is no
481+
# errno set on the OSError. The single string argument used to construct
482+
# OSError comes from libc itself and is therefore not portable to
483+
# hard code here. In any case, failure to call dlopen() means we
484+
# can proceed, so we bail on our attempt.
485+
try:
486+
# Note: typeshed is wrong here so we are ignoring this line.
487+
process_namespace = ctypes.CDLL(None) # type: ignore
488+
except OSError:
489+
return None
490+
479491
try:
480492
gnu_get_libc_version = process_namespace.gnu_get_libc_version
481493
except AttributeError:

0 commit comments

Comments
 (0)