comparison lib/python3.8/site-packages/pip/_internal/utils/glibc.py @ 1:64071f2a4cf0 draft default tip

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