Skip to content

Commit ea671c4

Browse files
committed
fixup! Switch LGPL'd chardet for MIT licensed charset_normalizer
1 parent 65fe1ce commit ea671c4

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

HISTORY.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ dev
88

99
**Dependencies**
1010

11-
- Switch chardet for the MIT-licensed charset_normalizer to remove license
12-
ambiguity for projects bundling requests.
11+
- Switch chardet for the MIT-licensed charset_normalizer for Python3 to remove license
12+
ambiguity for projects bundling requests. Python2 still depends upon the
13+
LGPL'd module.
1314

1415
2.25.1 (2020-12-16)
1516
-------------------

requests/__init__.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,20 @@
4141
"""
4242

4343
import urllib3
44-
import charset_normalizer
4544
import warnings
4645
from .exceptions import RequestsDependencyWarning
4746

47+
try:
48+
from charset_normalizer import __version__ as charset_normalizer_version
49+
except ImportError:
50+
charset_normalizer_version = None
51+
52+
try:
53+
from chardet import __version__ as chardet_version
54+
except ImportError:
55+
chardet_version = None
4856

49-
def check_compatibility(urllib3_version, charset_normalizer_version):
57+
def check_compatibility(urllib3_version, chardet_version, charset_normalizer_version):
5058
urllib3_version = urllib3_version.split('.')
5159
assert urllib3_version != ['dev'] # Verify urllib3 isn't installed from git.
5260

@@ -63,10 +71,16 @@ def check_compatibility(urllib3_version, charset_normalizer_version):
6371
assert minor <= 26
6472

6573
# Check charset_normalizer for compatibility.
66-
major, minor, patch = charset_normalizer_version.split('.')[:3]
67-
major, minor, patch = int(major), int(minor), int(patch)
68-
# charset_normalizer >= 3.0.2, < 5.0.0
69-
assert (1, 3, 5) <= (major, minor, patch) < (2, 0, 0)
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, < 5.0.0
78+
assert (3, 0, 2) <= (major, minor, patch) < (5, 0, 0)
79+
else:
80+
major, minor, patch = charset_normalizer_version.split('.')[:3]
81+
major, minor, patch = int(major), int(minor), int(patch)
82+
# charset_normalizer >= 1.3.5, < 2.0.0
83+
assert (1, 3, 5) <= (major, minor, patch) < (2, 0, 0)
7084

7185

7286
def _check_cryptography(cryptography_version):
@@ -82,10 +96,10 @@ def _check_cryptography(cryptography_version):
8296

8397
# Check imported dependencies for compatibility.
8498
try:
85-
check_compatibility(urllib3.__version__, charset_normalizer.__version__)
99+
check_compatibility(urllib3.__version__, chardet_version, charset_normalizer_version)
86100
except (AssertionError, ValueError):
87-
warnings.warn("urllib3 ({}) or charset_normalizer ({}) doesn't match a supported "
88-
"version!".format(urllib3.__version__, charset_normalizer.__version__),
101+
warnings.warn("urllib3 ({}) or chardet ({})/charset_normalizer ({}) doesn't match a supported "
102+
"version!".format(urllib3.__version__, chardet_version, charset_normalizer_version),
89103
RequestsDependencyWarning)
90104

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

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ def run_tests(self):
4141
packages = ['requests']
4242

4343
requires = [
44-
'charset_normalizer>=1.3.5,<2',
44+
'charset_normalizer>=1.3.5,<2; python_version >= "3"',
45+
'chardet>=3.0.2,<5; python_version < "3"',
4546
'idna>=2.5,<3',
4647
'urllib3>=1.21.1,<1.27',
4748
'certifi>=2017.4.17'

0 commit comments

Comments
 (0)