Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/pip/_vendor/requests/__init__.py: 60%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

68 statements  

1# __ 

2# /__) _ _ _ _ _/ _ 

3# / ( (- (/ (/ (- _) / _) 

4# / 

5 

6""" 

7Requests HTTP Library 

8~~~~~~~~~~~~~~~~~~~~~ 

9 

10Requests is an HTTP library, written in Python, for human beings. 

11Basic GET usage: 

12 

13 >>> import requests 

14 >>> r = requests.get('https://www.python.org') 

15 >>> r.status_code 

16 200 

17 >>> b'Python is a programming language' in r.content 

18 True 

19 

20... or POST: 

21 

22 >>> payload = dict(key1='value1', key2='value2') 

23 >>> r = requests.post('https://httpbin.org/post', data=payload) 

24 >>> print(r.text) 

25 { 

26 ... 

27 "form": { 

28 "key1": "value1", 

29 "key2": "value2" 

30 }, 

31 ... 

32 } 

33 

34The other HTTP methods are supported - see `requests.api`. Full documentation 

35is at <https://requests.readthedocs.io>. 

36 

37:copyright: (c) 2017 by Kenneth Reitz. 

38:license: Apache 2.0, see LICENSE for more details. 

39""" 

40 

41from __future__ import annotations 

42 

43import warnings 

44 

45from pip._vendor import urllib3 

46 

47from .exceptions import RequestsDependencyWarning 

48 

49charset_normalizer_version = None 

50chardet_version = None 

51 

52 

53def check_compatibility( 

54 urllib3_version: str, 

55 chardet_version: str | None, 

56 charset_normalizer_version: str | None, 

57) -> None: 

58 urllib3_version_list = urllib3_version.split(".")[:3] 

59 assert urllib3_version_list != ["dev"] # Verify urllib3 isn't installed from git. 

60 

61 # Sometimes, urllib3 only reports its version as 16.1. 

62 if len(urllib3_version_list) == 2: 

63 urllib3_version_list.append("0") 

64 

65 # Check urllib3 for compatibility. 

66 major, minor, patch = urllib3_version_list # noqa: F811 

67 major, minor, patch = int(major), int(minor), int(patch) 

68 # urllib3 >= 1.21.1 

69 assert major >= 1 

70 if major == 1: 

71 assert minor >= 21 

72 

73 # Check charset_normalizer for compatibility. 

74 if chardet_version: 

75 major, minor, patch = chardet_version.split(".")[:3] 

76 major, minor, patch = int(major), int(minor), int(patch) 

77 # chardet_version >= 3.0.2, < 8.0.0 

78 assert (3, 0, 2) <= (major, minor, patch) < (8, 0, 0) 

79 elif charset_normalizer_version: 

80 major, minor, patch = charset_normalizer_version.split(".")[:3] 

81 major, minor, patch = int(major), int(minor), int(patch) 

82 # charset_normalizer >= 2.0.0 < 4.0.0 

83 assert (2, 0, 0) <= (major, minor, patch) < (4, 0, 0) 

84 else: 

85 # pip does not need or use character detection 

86 pass 

87 

88 

89def _check_cryptography(cryptography_version: str) -> None: 

90 # cryptography < 1.3.4 

91 try: 

92 cryptography_version_list = list(map(int, cryptography_version.split("."))) 

93 except ValueError: 

94 return 

95 

96 if cryptography_version_list < [1, 3, 4]: 

97 warning = f"Old version of cryptography ({cryptography_version_list}) may cause slowdown." 

98 warnings.warn(warning, RequestsDependencyWarning) 

99 

100 

101# Check imported dependencies for compatibility. 

102try: 

103 check_compatibility( 

104 urllib3.__version__, # type: ignore[reportPrivateImportUsage] 

105 chardet_version, # type: ignore[reportUnknownArgumentType] 

106 charset_normalizer_version, 

107 ) 

108except (AssertionError, ValueError): 

109 warnings.warn( 

110 f"urllib3 ({urllib3.__version__}) or chardet " # type: ignore[reportPrivateImportUsage] 

111 f"({chardet_version})/charset_normalizer ({charset_normalizer_version}) " 

112 "doesn't match a supported version!", 

113 RequestsDependencyWarning, 

114 ) 

115 

116# Attempt to enable urllib3's fallback for SNI support 

117# if the standard library doesn't support SNI or the 

118# 'ssl' library isn't available. 

119try: 

120 # Note: This logic prevents upgrading cryptography on Windows, if imported 

121 # as part of pip. 

122 from pip._internal.utils.compat import WINDOWS 

123 if not WINDOWS: 

124 raise ImportError("pip internals: don't import cryptography on Windows") 

125 try: 

126 import ssl 

127 except ImportError: 

128 ssl = None 

129 

130 if not getattr(ssl, "HAS_SNI", False): 

131 from pip._vendor.urllib3.contrib import pyopenssl 

132 

133 pyopenssl.inject_into_urllib3() 

134 

135 # Check cryptography version 

136 from cryptography import ( # type: ignore[reportMissingImports] 

137 __version__ as cryptography_version, # type: ignore[reportUnknownVariableType] 

138 ) 

139 

140 _check_cryptography(cryptography_version) # type: ignore[reportUnknownArgumentType] 

141except ImportError: 

142 pass 

143 

144# urllib3's DependencyWarnings should be silenced. 

145from pip._vendor.urllib3.exceptions import DependencyWarning 

146 

147warnings.simplefilter("ignore", DependencyWarning) 

148 

149# Set default logging handler to avoid "No handler found" warnings. 

150import logging 

151from logging import NullHandler 

152 

153from . import packages, utils 

154from .__version__ import ( 

155 __author__, 

156 __author_email__, 

157 __build__, 

158 __cake__, 

159 __copyright__, 

160 __description__, 

161 __license__, 

162 __title__, 

163 __url__, 

164 __version__, 

165) 

166from .api import delete, get, head, options, patch, post, put, request 

167from .exceptions import ( 

168 ConnectionError, 

169 ConnectTimeout, 

170 FileModeWarning, 

171 HTTPError, 

172 JSONDecodeError, 

173 ReadTimeout, 

174 RequestException, 

175 Timeout, 

176 TooManyRedirects, 

177 URLRequired, 

178) 

179from .models import PreparedRequest, Request, Response 

180from .sessions import Session, session 

181from .status_codes import codes 

182 

183__all__ = ( 

184 "ConnectionError", 

185 "ConnectTimeout", 

186 "HTTPError", 

187 "JSONDecodeError", 

188 "PreparedRequest", 

189 "ReadTimeout", 

190 "Request", 

191 "RequestException", 

192 "Response", 

193 "Session", 

194 "Timeout", 

195 "TooManyRedirects", 

196 "URLRequired", 

197 "codes", 

198 "delete", 

199 "get", 

200 "head", 

201 "options", 

202 "packages", 

203 "patch", 

204 "post", 

205 "put", 

206 "request", 

207 "session", 

208 "utils", 

209) 

210 

211logging.getLogger(__name__).addHandler(NullHandler()) 

212 

213# FileModeWarnings go off per the default. 

214warnings.simplefilter("default", FileModeWarning, append=True)