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

68 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-07 06:48 +0000

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 

41import warnings 

42 

43from pip._vendor import urllib3 

44 

45from .exceptions import RequestsDependencyWarning 

46 

47charset_normalizer_version = None 

48 

49try: 

50 from pip._vendor.chardet import __version__ as chardet_version 

51except ImportError: 

52 chardet_version = None 

53 

54 

55def check_compatibility(urllib3_version, chardet_version, charset_normalizer_version): 

56 urllib3_version = urllib3_version.split(".") 

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

58 

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

60 if len(urllib3_version) == 2: 

61 urllib3_version.append("0") 

62 

63 # Check urllib3 for compatibility. 

64 major, minor, patch = urllib3_version # noqa: F811 

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

66 # urllib3 >= 1.21.1 

67 assert major >= 1 

68 if major == 1: 

69 assert minor >= 21 

70 

71 # Check charset_normalizer for compatibility. 

72 if chardet_version: 

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

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

75 # chardet_version >= 3.0.2, < 6.0.0 

76 assert (3, 0, 2) <= (major, minor, patch) < (6, 0, 0) 

77 elif charset_normalizer_version: 

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

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

80 # charset_normalizer >= 2.0.0 < 4.0.0 

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

82 else: 

83 raise Exception("You need either charset_normalizer or chardet installed") 

84 

85 

86def _check_cryptography(cryptography_version): 

87 # cryptography < 1.3.4 

88 try: 

89 cryptography_version = list(map(int, cryptography_version.split("."))) 

90 except ValueError: 

91 return 

92 

93 if cryptography_version < [1, 3, 4]: 

94 warning = "Old version of cryptography ({}) may cause slowdown.".format( 

95 cryptography_version 

96 ) 

97 warnings.warn(warning, RequestsDependencyWarning) 

98 

99 

100# Check imported dependencies for compatibility. 

101try: 

102 check_compatibility( 

103 urllib3.__version__, chardet_version, charset_normalizer_version 

104 ) 

105except (AssertionError, ValueError): 

106 warnings.warn( 

107 "urllib3 ({}) or chardet ({})/charset_normalizer ({}) doesn't match a supported " 

108 "version!".format( 

109 urllib3.__version__, chardet_version, charset_normalizer_version 

110 ), 

111 RequestsDependencyWarning, 

112 ) 

113 

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

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

116# 'ssl' library isn't available. 

117try: 

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

119 # as part of pip. 

120 from pip._internal.utils.compat import WINDOWS 

121 if not WINDOWS: 

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

123 try: 

124 import ssl 

125 except ImportError: 

126 ssl = None 

127 

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

129 from pip._vendor.urllib3.contrib import pyopenssl 

130 

131 pyopenssl.inject_into_urllib3() 

132 

133 # Check cryptography version 

134 from cryptography import __version__ as cryptography_version 

135 

136 _check_cryptography(cryptography_version) 

137except ImportError: 

138 pass 

139 

140# urllib3's DependencyWarnings should be silenced. 

141from pip._vendor.urllib3.exceptions import DependencyWarning 

142 

143warnings.simplefilter("ignore", DependencyWarning) 

144 

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

146import logging 

147from logging import NullHandler 

148 

149from . import packages, utils 

150from .__version__ import ( 

151 __author__, 

152 __author_email__, 

153 __build__, 

154 __cake__, 

155 __copyright__, 

156 __description__, 

157 __license__, 

158 __title__, 

159 __url__, 

160 __version__, 

161) 

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

163from .exceptions import ( 

164 ConnectionError, 

165 ConnectTimeout, 

166 FileModeWarning, 

167 HTTPError, 

168 JSONDecodeError, 

169 ReadTimeout, 

170 RequestException, 

171 Timeout, 

172 TooManyRedirects, 

173 URLRequired, 

174) 

175from .models import PreparedRequest, Request, Response 

176from .sessions import Session, session 

177from .status_codes import codes 

178 

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

180 

181# FileModeWarnings go off per the default. 

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