1
from __future__ import absolute_import
9
def glibc_version_string():
10
"Returns glibc version string, or None if not using glibc."
12
# ctypes.CDLL(None) internally calls dlopen(NULL), and as the dlopen
13
# manpage says, "If filename is NULL, then the returned handle is for the
14
# main program". This way we can let the linker do the work to figure out
15
# which libc our process is actually using.
16
process_namespace = ctypes.CDLL(None)
18
gnu_get_libc_version = process_namespace.gnu_get_libc_version
19
except AttributeError:
20
# Symbol doesn't exist -> therefore, we are not linked to
24
# Call gnu_get_libc_version, which returns a string like "2.5"
25
gnu_get_libc_version.restype = ctypes.c_char_p
26
version_str = gnu_get_libc_version()
27
# py2 / py3 compatibility:
28
if not isinstance(version_str, str):
29
version_str = version_str.decode("ascii")
34
# Separated out from have_compatible_glibc for easier unit testing
35
def check_glibc_version(version_str, required_major, minimum_minor):
36
# Parse string and check against requested version.
38
# We use a regexp instead of str.split because we want to discard any
39
# random junk that might come after the minor version -- this might happen
40
# in patched/forked versions of glibc (e.g. Linaro's version of glibc
41
# uses version strings like "2.20-2014.11"). See gh-3588.
42
m = re.match(r"(?P<major>[0-9]+)\.(?P<minor>[0-9]+)", version_str)
44
warnings.warn("Expected glibc version with 2 components major.minor,"
45
" got: %s" % version_str, RuntimeWarning)
47
return (int(m.group("major")) == required_major and
48
int(m.group("minor")) >= minimum_minor)
51
def have_compatible_glibc(required_major, minimum_minor):
52
version_str = glibc_version_string()
53
if version_str is None:
55
return check_glibc_version(version_str, required_major, minimum_minor)
58
# platform.libc_ver regularly returns completely nonsensical glibc
59
# versions. E.g. on my computer, platform says:
61
# ~$ python2.7 -c 'import platform; print(platform.libc_ver())'
63
# ~$ python3.5 -c 'import platform; print(platform.libc_ver())'
69
# ldd (Debian GLIBC 2.22-11) 2.22
71
# This is unfortunate, because it means that the linehaul data on libc
72
# versions that was generated by pip 8.1.2 and earlier is useless and
73
# misleading. Solution: instead of using platform, use our code that actually
76
glibc_version = glibc_version_string()
77
if glibc_version is None:
78
# For non-glibc platforms, fall back on platform.libc_ver
79
return platform.libc_ver()
81
return ("glibc", glibc_version)