1
- # -*- coding: utf-8 -*-
2
-
3
1
# __
4
2
# /__) _ _ _ _ _/ _
5
3
# / ( (- (/ (/ (- _) / _)
40
38
:license: Apache 2.0, see LICENSE for more details.
41
39
"""
42
40
43
- from pip ._vendor import urllib3
44
41
import warnings
42
+
43
+ from pip ._vendor import urllib3
44
+
45
45
from .exceptions import RequestsDependencyWarning
46
46
47
- try :
48
- from charset_normalizer import __version__ as charset_normalizer_version
49
- except ImportError :
50
- charset_normalizer_version = None
47
+ charset_normalizer_version = None
51
48
52
49
try :
53
50
from pip ._vendor .chardet import __version__ as chardet_version
54
51
except ImportError :
55
52
chardet_version = None
56
53
54
+
57
55
def check_compatibility (urllib3_version , chardet_version , charset_normalizer_version ):
58
- urllib3_version = urllib3_version .split ('.' )
59
- assert urllib3_version != [' dev' ] # Verify urllib3 isn't installed from git.
56
+ urllib3_version = urllib3_version .split ("." )
57
+ assert urllib3_version != [" dev" ] # Verify urllib3 isn't installed from git.
60
58
61
59
# Sometimes, urllib3 only reports its version as 16.1.
62
60
if len (urllib3_version ) == 2 :
63
- urllib3_version .append ('0' )
61
+ urllib3_version .append ("0" )
64
62
65
63
# Check urllib3 for compatibility.
66
64
major , minor , patch = urllib3_version # noqa: F811
@@ -72,81 +70,113 @@ def check_compatibility(urllib3_version, chardet_version, charset_normalizer_ver
72
70
73
71
# Check charset_normalizer for compatibility.
74
72
if chardet_version :
75
- major , minor , patch = chardet_version .split ('.' )[:3 ]
73
+ major , minor , patch = chardet_version .split ("." )[:3 ]
76
74
major , minor , patch = int (major ), int (minor ), int (patch )
77
- # chardet_version >= 3.0.2, < 5 .0.0
78
- assert (3 , 0 , 2 ) <= (major , minor , patch ) < (5 , 0 , 0 )
75
+ # chardet_version >= 3.0.2, < 6 .0.0
76
+ assert (3 , 0 , 2 ) <= (major , minor , patch ) < (6 , 0 , 0 )
79
77
elif charset_normalizer_version :
80
- major , minor , patch = charset_normalizer_version .split ('.' )[:3 ]
78
+ major , minor , patch = charset_normalizer_version .split ("." )[:3 ]
81
79
major , minor , patch = int (major ), int (minor ), int (patch )
82
80
# charset_normalizer >= 2.0.0 < 3.0.0
83
81
assert (2 , 0 , 0 ) <= (major , minor , patch ) < (3 , 0 , 0 )
84
82
else :
85
83
raise Exception ("You need either charset_normalizer or chardet installed" )
86
84
85
+
87
86
def _check_cryptography (cryptography_version ):
88
87
# cryptography < 1.3.4
89
88
try :
90
- cryptography_version = list (map (int , cryptography_version .split ('.' )))
89
+ cryptography_version = list (map (int , cryptography_version .split ("." )))
91
90
except ValueError :
92
91
return
93
92
94
93
if cryptography_version < [1 , 3 , 4 ]:
95
- warning = 'Old version of cryptography ({}) may cause slowdown.' .format (cryptography_version )
94
+ warning = "Old version of cryptography ({}) may cause slowdown." .format (
95
+ cryptography_version
96
+ )
96
97
warnings .warn (warning , RequestsDependencyWarning )
97
98
99
+
98
100
# Check imported dependencies for compatibility.
99
101
try :
100
- check_compatibility (urllib3 .__version__ , chardet_version , charset_normalizer_version )
102
+ check_compatibility (
103
+ urllib3 .__version__ , chardet_version , charset_normalizer_version
104
+ )
101
105
except (AssertionError , ValueError ):
102
- warnings .warn ("urllib3 ({}) or chardet ({})/charset_normalizer ({}) doesn't match a supported "
103
- "version!" .format (urllib3 .__version__ , chardet_version , charset_normalizer_version ),
104
- RequestsDependencyWarning )
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
+ )
105
113
106
114
# Attempt to enable urllib3's fallback for SNI support
107
115
# if the standard library doesn't support SNI or the
108
116
# 'ssl' library isn't available.
109
117
try :
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" )
110
123
try :
111
124
import ssl
112
125
except ImportError :
113
126
ssl = None
114
127
115
128
if not getattr (ssl , "HAS_SNI" , False ):
116
129
from pip ._vendor .urllib3 .contrib import pyopenssl
130
+
117
131
pyopenssl .inject_into_urllib3 ()
118
132
119
133
# Check cryptography version
120
134
from cryptography import __version__ as cryptography_version
135
+
121
136
_check_cryptography (cryptography_version )
122
137
except ImportError :
123
138
pass
124
139
125
140
# urllib3's DependencyWarnings should be silenced.
126
141
from pip ._vendor .urllib3 .exceptions import DependencyWarning
127
- warnings .simplefilter ('ignore' , DependencyWarning )
128
-
129
- from .__version__ import __title__ , __description__ , __url__ , __version__
130
- from .__version__ import __build__ , __author__ , __author_email__ , __license__
131
- from .__version__ import __copyright__ , __cake__
132
142
133
- from . import utils
134
- from . import packages
135
- from .models import Request , Response , PreparedRequest
136
- from .api import request , get , head , post , patch , put , delete , options
137
- from .sessions import session , Session
138
- from .status_codes import codes
139
- from .exceptions import (
140
- RequestException , Timeout , URLRequired ,
141
- TooManyRedirects , HTTPError , ConnectionError ,
142
- FileModeWarning , ConnectTimeout , ReadTimeout , JSONDecodeError
143
- )
143
+ warnings .simplefilter ("ignore" , DependencyWarning )
144
144
145
145
# Set default logging handler to avoid "No handler found" warnings.
146
146
import logging
147
147
from logging import NullHandler
148
148
149
+ from . import packages , utils
150
+ from .__version__ import (
151
+ __author__ ,
152
+ __author_email__ ,
153
+ __build__ ,
154
+ __cake__ ,
155
+ __copyright__ ,
156
+ __description__ ,
157
+ __license__ ,
158
+ __title__ ,
159
+ __url__ ,
160
+ __version__ ,
161
+ )
162
+ from .api import delete , get , head , options , patch , post , put , request
163
+ from .exceptions import (
164
+ ConnectionError ,
165
+ ConnectTimeout ,
166
+ FileModeWarning ,
167
+ HTTPError ,
168
+ JSONDecodeError ,
169
+ ReadTimeout ,
170
+ RequestException ,
171
+ Timeout ,
172
+ TooManyRedirects ,
173
+ URLRequired ,
174
+ )
175
+ from .models import PreparedRequest , Request , Response
176
+ from .sessions import Session , session
177
+ from .status_codes import codes
178
+
149
179
logging .getLogger (__name__ ).addHandler (NullHandler ())
150
180
151
181
# FileModeWarnings go off per the default.
152
- warnings .simplefilter (' default' , FileModeWarning , append = True )
182
+ warnings .simplefilter (" default" , FileModeWarning , append = True )
0 commit comments