Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/pip/_internal/utils/glibc.py: 45%
33 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:48 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:48 +0000
1# The following comment should be removed at some point in the future.
2# mypy: strict-optional=False
4import os
5import sys
6from typing import Optional, Tuple
9def glibc_version_string() -> Optional[str]:
10 "Returns glibc version string, or None if not using glibc."
11 return glibc_version_string_confstr() or glibc_version_string_ctypes()
14def glibc_version_string_confstr() -> Optional[str]:
15 "Primary implementation of glibc_version_string using os.confstr."
16 # os.confstr is quite a bit faster than ctypes.DLL. It's also less likely
17 # to be broken or missing. This strategy is used in the standard library
18 # platform module:
19 # https://github.com/python/cpython/blob/fcf1d003bf4f0100c9d0921ff3d70e1127ca1b71/Lib/platform.py#L175-L183
20 if sys.platform == "win32":
21 return None
22 try:
23 # os.confstr("CS_GNU_LIBC_VERSION") returns a string like "glibc 2.17":
24 _, version = os.confstr("CS_GNU_LIBC_VERSION").split()
25 except (AttributeError, OSError, ValueError):
26 # os.confstr() or CS_GNU_LIBC_VERSION not available (or a bad value)...
27 return None
28 return version
31def glibc_version_string_ctypes() -> Optional[str]:
32 "Fallback implementation of glibc_version_string using ctypes."
34 try:
35 import ctypes
36 except ImportError:
37 return None
39 # ctypes.CDLL(None) internally calls dlopen(NULL), and as the dlopen
40 # manpage says, "If filename is NULL, then the returned handle is for the
41 # main program". This way we can let the linker do the work to figure out
42 # which libc our process is actually using.
43 process_namespace = ctypes.CDLL(None)
44 try:
45 gnu_get_libc_version = process_namespace.gnu_get_libc_version
46 except AttributeError:
47 # Symbol doesn't exist -> therefore, we are not linked to
48 # glibc.
49 return None
51 # Call gnu_get_libc_version, which returns a string like "2.5"
52 gnu_get_libc_version.restype = ctypes.c_char_p
53 version_str = gnu_get_libc_version()
54 # py2 / py3 compatibility:
55 if not isinstance(version_str, str):
56 version_str = version_str.decode("ascii")
58 return version_str
61# platform.libc_ver regularly returns completely nonsensical glibc
62# versions. E.g. on my computer, platform says:
63#
64# ~$ python2.7 -c 'import platform; print(platform.libc_ver())'
65# ('glibc', '2.7')
66# ~$ python3.5 -c 'import platform; print(platform.libc_ver())'
67# ('glibc', '2.9')
68#
69# But the truth is:
70#
71# ~$ ldd --version
72# ldd (Debian GLIBC 2.22-11) 2.22
73#
74# This is unfortunate, because it means that the linehaul data on libc
75# versions that was generated by pip 8.1.2 and earlier is useless and
76# misleading. Solution: instead of using platform, use our code that actually
77# works.
78def libc_ver() -> Tuple[str, str]:
79 """Try to determine the glibc version
81 Returns a tuple of strings (lib, version) which default to empty strings
82 in case the lookup fails.
83 """
84 glibc_version = glibc_version_string()
85 if glibc_version is None:
86 return ("", "")
87 else:
88 return ("glibc", glibc_version)