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

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

66 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 

41import warnings 

42 

43from pip._vendor import urllib3 

44 

45from .exceptions import RequestsDependencyWarning 

46 

47charset_normalizer_version = None 

48chardet_version = None 

49 

50 

51def check_compatibility(urllib3_version, chardet_version, charset_normalizer_version): 

52 urllib3_version = urllib3_version.split(".") 

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

54 

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

56 if len(urllib3_version) == 2: 

57 urllib3_version.append("0") 

58 

59 # Check urllib3 for compatibility. 

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

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

62 # urllib3 >= 1.21.1 

63 assert major >= 1 

64 if major == 1: 

65 assert minor >= 21 

66 

67 # Check charset_normalizer for compatibility. 

68 if chardet_version: 

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

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

71 # chardet_version >= 3.0.2, < 6.0.0 

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

73 elif charset_normalizer_version: 

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

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

76 # charset_normalizer >= 2.0.0 < 4.0.0 

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

78 else: 

79 # pip does not need or use character detection 

80 pass 

81 

82 

83def _check_cryptography(cryptography_version): 

84 # cryptography < 1.3.4 

85 try: 

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

87 except ValueError: 

88 return 

89 

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

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

92 cryptography_version 

93 ) 

94 warnings.warn(warning, RequestsDependencyWarning) 

95 

96 

97# Check imported dependencies for compatibility. 

98try: 

99 check_compatibility( 

100 urllib3.__version__, chardet_version, charset_normalizer_version 

101 ) 

102except (AssertionError, ValueError): 

103 warnings.warn( 

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

105 "version!".format( 

106 urllib3.__version__, chardet_version, charset_normalizer_version 

107 ), 

108 RequestsDependencyWarning, 

109 ) 

110 

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

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

113# 'ssl' library isn't available. 

114try: 

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

116 # as part of pip. 

117 from pip._internal.utils.compat import WINDOWS 

118 if not WINDOWS: 

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

120 try: 

121 import ssl 

122 except ImportError: 

123 ssl = None 

124 

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

126 from pip._vendor.urllib3.contrib import pyopenssl 

127 

128 pyopenssl.inject_into_urllib3() 

129 

130 # Check cryptography version 

131 from cryptography import __version__ as cryptography_version 

132 

133 _check_cryptography(cryptography_version) 

134except ImportError: 

135 pass 

136 

137# urllib3's DependencyWarnings should be silenced. 

138from pip._vendor.urllib3.exceptions import DependencyWarning 

139 

140warnings.simplefilter("ignore", DependencyWarning) 

141 

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

143import logging 

144from logging import NullHandler 

145 

146from . import packages, utils 

147from .__version__ import ( 

148 __author__, 

149 __author_email__, 

150 __build__, 

151 __cake__, 

152 __copyright__, 

153 __description__, 

154 __license__, 

155 __title__, 

156 __url__, 

157 __version__, 

158) 

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

160from .exceptions import ( 

161 ConnectionError, 

162 ConnectTimeout, 

163 FileModeWarning, 

164 HTTPError, 

165 JSONDecodeError, 

166 ReadTimeout, 

167 RequestException, 

168 Timeout, 

169 TooManyRedirects, 

170 URLRequired, 

171) 

172from .models import PreparedRequest, Request, Response 

173from .sessions import Session, session 

174from .status_codes import codes 

175 

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

177 

178# FileModeWarnings go off per the default. 

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