Skip to content

Commit b6813a6

Browse files
authored
FEA Support static and / or alternative libc (#171)
1 parent fef2303 commit b6813a6

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
3.4.0 (TDB)
22
===========
33

4+
- Added support for Python interpreters statically linked against libc or linked against
5+
alternative implementations of libc like musl (on Alpine Linux for instance).
6+
https://github.com/joblib/threadpoolctl/pull/171
7+
48
- Added support for Pyodide
59
https://github.com/joblib/threadpoolctl/pull/169
610

threadpoolctl.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -996,6 +996,10 @@ def _find_libraries_with_dl_iterate_phdr(self):
996996
"""
997997
libc = self._get_libc()
998998
if not hasattr(libc, "dl_iterate_phdr"): # pragma: no cover
999+
warnings.warn(
1000+
"Could not find dl_iterate_phdr in the C standard library.",
1001+
RuntimeWarning,
1002+
)
9991003
return []
10001004

10011005
# Callback function for `dl_iterate_phdr` which is called for every
@@ -1028,6 +1032,10 @@ def _find_libraries_with_dyld(self):
10281032
"""
10291033
libc = self._get_libc()
10301034
if not hasattr(libc, "_dyld_image_count"): # pragma: no cover
1035+
warnings.warn(
1036+
"Could not find _dyld_image_count in the C standard library.",
1037+
RuntimeWarning,
1038+
)
10311039
return []
10321040

10331041
n_dyld = libc._dyld_image_count()
@@ -1219,16 +1227,13 @@ def _get_libc(cls):
12191227
"""Load the lib-C for unix systems."""
12201228
libc = cls._system_libraries.get("libc")
12211229
if libc is None:
1222-
libc_name = find_library("c")
1223-
if libc_name is None: # pragma: no cover
1224-
warnings.warn(
1225-
"libc not found. The ctypes module in Python"
1226-
f" {sys.version_info.major}.{sys.version_info.minor} is maybe"
1227-
" too old for this OS.",
1228-
RuntimeWarning,
1229-
)
1230-
return None
1231-
libc = ctypes.CDLL(libc_name, mode=_RTLD_NOLOAD)
1230+
# Remark: If libc is statically linked or if Python is linked against an
1231+
# alternative implementation of libc like musl, find_library will return
1232+
# None and CDLL will load the main program itself which should contain the
1233+
# libc symbols. We still name it libc for convenience.
1234+
# If the main program does not contain the libc symbols, it's ok because
1235+
# we check their presence later anyway.
1236+
libc = ctypes.CDLL(find_library("c"), mode=_RTLD_NOLOAD)
12321237
cls._system_libraries["libc"] = libc
12331238
return libc
12341239

0 commit comments

Comments
 (0)