Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/packaging/_manylinux.py: 24%

123 statements  

« prev     ^ index     » next       coverage.py v7.2.2, created at 2023-03-26 06:25 +0000

1import collections 

2import contextlib 

3import functools 

4import os 

5import re 

6import sys 

7import warnings 

8from typing import Dict, Generator, Iterator, NamedTuple, Optional, Tuple 

9 

10from ._elffile import EIClass, EIData, ELFFile, EMachine 

11 

12EF_ARM_ABIMASK = 0xFF000000 

13EF_ARM_ABI_VER5 = 0x05000000 

14EF_ARM_ABI_FLOAT_HARD = 0x00000400 

15 

16 

17# `os.PathLike` not a generic type until Python 3.9, so sticking with `str` 

18# as the type for `path` until then. 

19@contextlib.contextmanager 

20def _parse_elf(path: str) -> Generator[Optional[ELFFile], None, None]: 

21 try: 

22 with open(path, "rb") as f: 

23 yield ELFFile(f) 

24 except (OSError, TypeError, ValueError): 

25 yield None 

26 

27 

28def _is_linux_armhf(executable: str) -> bool: 

29 # hard-float ABI can be detected from the ELF header of the running 

30 # process 

31 # https://static.docs.arm.com/ihi0044/g/aaelf32.pdf 

32 with _parse_elf(executable) as f: 

33 return ( 

34 f is not None 

35 and f.capacity == EIClass.C32 

36 and f.encoding == EIData.Lsb 

37 and f.machine == EMachine.Arm 

38 and f.flags & EF_ARM_ABIMASK == EF_ARM_ABI_VER5 

39 and f.flags & EF_ARM_ABI_FLOAT_HARD == EF_ARM_ABI_FLOAT_HARD 

40 ) 

41 

42 

43def _is_linux_i686(executable: str) -> bool: 

44 with _parse_elf(executable) as f: 

45 return ( 

46 f is not None 

47 and f.capacity == EIClass.C32 

48 and f.encoding == EIData.Lsb 

49 and f.machine == EMachine.I386 

50 ) 

51 

52 

53def _have_compatible_abi(executable: str, arch: str) -> bool: 

54 if arch == "armv7l": 

55 return _is_linux_armhf(executable) 

56 if arch == "i686": 

57 return _is_linux_i686(executable) 

58 return arch in {"x86_64", "aarch64", "ppc64", "ppc64le", "s390x"} 

59 

60 

61# If glibc ever changes its major version, we need to know what the last 

62# minor version was, so we can build the complete list of all versions. 

63# For now, guess what the highest minor version might be, assume it will 

64# be 50 for testing. Once this actually happens, update the dictionary 

65# with the actual value. 

66_LAST_GLIBC_MINOR: Dict[int, int] = collections.defaultdict(lambda: 50) 

67 

68 

69class _GLibCVersion(NamedTuple): 

70 major: int 

71 minor: int 

72 

73 

74def _glibc_version_string_confstr() -> Optional[str]: 

75 """ 

76 Primary implementation of glibc_version_string using os.confstr. 

77 """ 

78 # os.confstr is quite a bit faster than ctypes.DLL. It's also less likely 

79 # to be broken or missing. This strategy is used in the standard library 

80 # platform module. 

81 # https://github.com/python/cpython/blob/fcf1d003bf4f0100c/Lib/platform.py#L175-L183 

82 try: 

83 # Should be a string like "glibc 2.17". 

84 version_string: str = getattr(os, "confstr")("CS_GNU_LIBC_VERSION") 

85 assert version_string is not None 

86 _, version = version_string.rsplit() 

87 except (AssertionError, AttributeError, OSError, ValueError): 

88 # os.confstr() or CS_GNU_LIBC_VERSION not available (or a bad value)... 

89 return None 

90 return version 

91 

92 

93def _glibc_version_string_ctypes() -> Optional[str]: 

94 """ 

95 Fallback implementation of glibc_version_string using ctypes. 

96 """ 

97 try: 

98 import ctypes 

99 except ImportError: 

100 return None 

101 

102 # ctypes.CDLL(None) internally calls dlopen(NULL), and as the dlopen 

103 # manpage says, "If filename is NULL, then the returned handle is for the 

104 # main program". This way we can let the linker do the work to figure out 

105 # which libc our process is actually using. 

106 # 

107 # We must also handle the special case where the executable is not a 

108 # dynamically linked executable. This can occur when using musl libc, 

109 # for example. In this situation, dlopen() will error, leading to an 

110 # OSError. Interestingly, at least in the case of musl, there is no 

111 # errno set on the OSError. The single string argument used to construct 

112 # OSError comes from libc itself and is therefore not portable to 

113 # hard code here. In any case, failure to call dlopen() means we 

114 # can proceed, so we bail on our attempt. 

115 try: 

116 process_namespace = ctypes.CDLL(None) 

117 except OSError: 

118 return None 

119 

120 try: 

121 gnu_get_libc_version = process_namespace.gnu_get_libc_version 

122 except AttributeError: 

123 # Symbol doesn't exist -> therefore, we are not linked to 

124 # glibc. 

125 return None 

126 

127 # Call gnu_get_libc_version, which returns a string like "2.5" 

128 gnu_get_libc_version.restype = ctypes.c_char_p 

129 version_str: str = gnu_get_libc_version() 

130 # py2 / py3 compatibility: 

131 if not isinstance(version_str, str): 

132 version_str = version_str.decode("ascii") 

133 

134 return version_str 

135 

136 

137def _glibc_version_string() -> Optional[str]: 

138 """Returns glibc version string, or None if not using glibc.""" 

139 return _glibc_version_string_confstr() or _glibc_version_string_ctypes() 

140 

141 

142def _parse_glibc_version(version_str: str) -> Tuple[int, int]: 

143 """Parse glibc version. 

144 

145 We use a regexp instead of str.split because we want to discard any 

146 random junk that might come after the minor version -- this might happen 

147 in patched/forked versions of glibc (e.g. Linaro's version of glibc 

148 uses version strings like "2.20-2014.11"). See gh-3588. 

149 """ 

150 m = re.match(r"(?P<major>[0-9]+)\.(?P<minor>[0-9]+)", version_str) 

151 if not m: 

152 warnings.warn( 

153 f"Expected glibc version with 2 components major.minor," 

154 f" got: {version_str}", 

155 RuntimeWarning, 

156 ) 

157 return -1, -1 

158 return int(m.group("major")), int(m.group("minor")) 

159 

160 

161@functools.lru_cache() 

162def _get_glibc_version() -> Tuple[int, int]: 

163 version_str = _glibc_version_string() 

164 if version_str is None: 

165 return (-1, -1) 

166 return _parse_glibc_version(version_str) 

167 

168 

169# From PEP 513, PEP 600 

170def _is_compatible(name: str, arch: str, version: _GLibCVersion) -> bool: 

171 sys_glibc = _get_glibc_version() 

172 if sys_glibc < version: 

173 return False 

174 # Check for presence of _manylinux module. 

175 try: 

176 import _manylinux # noqa 

177 except ImportError: 

178 return True 

179 if hasattr(_manylinux, "manylinux_compatible"): 

180 result = _manylinux.manylinux_compatible(version[0], version[1], arch) 

181 if result is not None: 

182 return bool(result) 

183 return True 

184 if version == _GLibCVersion(2, 5): 

185 if hasattr(_manylinux, "manylinux1_compatible"): 

186 return bool(_manylinux.manylinux1_compatible) 

187 if version == _GLibCVersion(2, 12): 

188 if hasattr(_manylinux, "manylinux2010_compatible"): 

189 return bool(_manylinux.manylinux2010_compatible) 

190 if version == _GLibCVersion(2, 17): 

191 if hasattr(_manylinux, "manylinux2014_compatible"): 

192 return bool(_manylinux.manylinux2014_compatible) 

193 return True 

194 

195 

196_LEGACY_MANYLINUX_MAP = { 

197 # CentOS 7 w/ glibc 2.17 (PEP 599) 

198 (2, 17): "manylinux2014", 

199 # CentOS 6 w/ glibc 2.12 (PEP 571) 

200 (2, 12): "manylinux2010", 

201 # CentOS 5 w/ glibc 2.5 (PEP 513) 

202 (2, 5): "manylinux1", 

203} 

204 

205 

206def platform_tags(linux: str, arch: str) -> Iterator[str]: 

207 if not _have_compatible_abi(sys.executable, arch): 

208 return 

209 # Oldest glibc to be supported regardless of architecture is (2, 17). 

210 too_old_glibc2 = _GLibCVersion(2, 16) 

211 if arch in {"x86_64", "i686"}: 

212 # On x86/i686 also oldest glibc to be supported is (2, 5). 

213 too_old_glibc2 = _GLibCVersion(2, 4) 

214 current_glibc = _GLibCVersion(*_get_glibc_version()) 

215 glibc_max_list = [current_glibc] 

216 # We can assume compatibility across glibc major versions. 

217 # https://sourceware.org/bugzilla/show_bug.cgi?id=24636 

218 # 

219 # Build a list of maximum glibc versions so that we can 

220 # output the canonical list of all glibc from current_glibc 

221 # down to too_old_glibc2, including all intermediary versions. 

222 for glibc_major in range(current_glibc.major - 1, 1, -1): 

223 glibc_minor = _LAST_GLIBC_MINOR[glibc_major] 

224 glibc_max_list.append(_GLibCVersion(glibc_major, glibc_minor)) 

225 for glibc_max in glibc_max_list: 

226 if glibc_max.major == too_old_glibc2.major: 

227 min_minor = too_old_glibc2.minor 

228 else: 

229 # For other glibc major versions oldest supported is (x, 0). 

230 min_minor = -1 

231 for glibc_minor in range(glibc_max.minor, min_minor, -1): 

232 glibc_version = _GLibCVersion(glibc_max.major, glibc_minor) 

233 tag = "manylinux_{}_{}".format(*glibc_version) 

234 if _is_compatible(tag, arch, glibc_version): 

235 yield linux.replace("linux", tag) 

236 # Handle the legacy manylinux1, manylinux2010, manylinux2014 tags. 

237 if glibc_version in _LEGACY_MANYLINUX_MAP: 

238 legacy_tag = _LEGACY_MANYLINUX_MAP[glibc_version] 

239 if _is_compatible(legacy_tag, arch, glibc_version): 

240 yield linux.replace("linux", legacy_tag)