-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add cchardet support and use chardet as fallback #4115
Changes from all commits
689b692
2335dae
fbe5a7a
b231590
2f88d52
0e3bb27
03851c5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,15 +39,18 @@ | |
:copyright: (c) 2017 by Kenneth Reitz. | ||
:license: Apache 2.0, see LICENSE for more details. | ||
""" | ||
def get_version(package): | ||
version = package.__version__.split('.')[:3] | ||
if len(version) == 2: | ||
version.append('0') | ||
major, minor, patch = version | ||
major, minor, patch = int(major), int(minor), int(patch) | ||
return major, minor, patch | ||
|
||
# Check urllib3 for compatibility. | ||
import urllib3 | ||
urllib3_version = urllib3.__version__.split('.') | ||
# Sometimes, urllib3 only reports its version as 16.1. | ||
if len(urllib3_version) == 2: | ||
urllib3_version.append('0') | ||
major, minor, patch = urllib3_version | ||
major, minor, patch = int(major), int(minor), int(patch) | ||
|
||
major, minor, patch = get_version(urllib3) | ||
# urllib3 >= 1.21.1, < 1.22 | ||
try: | ||
assert major == 1 | ||
|
@@ -56,18 +59,33 @@ | |
except AssertionError: | ||
raise RuntimeError('Requests dependency \'urllib3\' must be version >= 1.21.1, < 1.22!') | ||
|
||
import warnings | ||
|
||
# Check chardet for compatibility. | ||
import chardet | ||
major, minor, patch = chardet.__version__.split('.')[:3] | ||
major, minor, patch = int(major), int(minor), int(patch) | ||
# chardet >= 3.0.2, < 3.1.0 | ||
try: | ||
assert major == 3 | ||
assert minor < 1 | ||
assert patch >= 2 | ||
except AssertionError: | ||
raise RuntimeError('Requests dependency \'chardet\' must be version >= 3.0.2, < 3.1.0!') | ||
# Check cchardet for compatibility. | ||
import cchardet as chardet | ||
major, minor, patch = get_version(chardet) | ||
# cchardet >= 2.1.0 | ||
try: | ||
assert major == 2 | ||
assert minor >= 1 | ||
assert minor < 2 | ||
assert patch >= 0 | ||
except AssertionError: | ||
warnings.warn('Requests dependency \'cchardet\' must be version >= 2.1.0, < 2.2.0! Falling back to chardet') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not a dependency of Requests, it's an optional speed-up. Please revise |
||
raise | ||
|
||
except (ImportError, AssertionError): | ||
# Check chardet for compatibility. | ||
import chardet | ||
major, minor, patch = get_version(chardet) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
# chardet >= 3.0.2, < 3.1.0 | ||
try: | ||
assert major == 3 | ||
assert minor < 1 | ||
assert patch >= 2 | ||
except AssertionError: | ||
raise RuntimeError('Requests dependency \'chardet\' must be version >= 3.0.2, < 3.1.0!') | ||
|
||
# Attempt to enable urllib3's SNI support, if possible | ||
try: | ||
|
@@ -76,8 +94,6 @@ | |
except ImportError: | ||
pass | ||
|
||
import warnings | ||
|
||
# urllib3's DependencyWarnings should be silenced. | ||
from urllib3.exceptions import DependencyWarning | ||
warnings.simplefilter('ignore', DependencyWarning) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,8 +3,16 @@ | |
# This code exists for backwards compatibility reasons. | ||
# I don't like it either. Just look the other way. :) | ||
|
||
for package in ('urllib3', 'idna', 'chardet'): | ||
locals()[package] = __import__(package) | ||
for package in ('urllib3', 'idna', ('chardet', 'cchardet')): | ||
if isinstance(package, tuple): | ||
package, alt_package = package | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we need to be extremely careful about touching this code, at all. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree. This looks safe, but it'd be so much better if this were testable. |
||
try: | ||
locals()[package] = __import__(alt_package) | ||
except ImportError: | ||
locals()[package] = __import__(package) | ||
else: | ||
locals()[package] = __import__(package) | ||
|
||
# This traversal is apparently necessary such that the identities are | ||
# preserved (requests.packages.urllib3.* is urllib3.*) | ||
for mod in list(sys.modules): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -93,6 +93,7 @@ def run_tests(self): | |
cmdclass={'test': PyTest}, | ||
tests_require=test_requirements, | ||
extras_require={ | ||
'cchardet:(python_version == "2.7" or python_version >= "3.4")': ['cchardet>=2.1.0'], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we're enforcing (in |
||
'security': ['pyOpenSSL>=0.14', 'cryptography>=1.3.4', 'idna>=2.0.0'], | ||
'socks': ['PySocks>=1.5.6, !=1.5.7'], | ||
'socks:sys_platform == "win32" and (python_version == "2.7" or python_version == "2.6")': ['win_inet_pton'], | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not: