Merge pull request #6702 from nateprewitt/no_char_detection
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Allow optional char detection dependencies in post-packaging
authored by Ian Stapleton Cordasco · committed by GitHub · parents d6dded3, 555b870 · Browse files · 5 files·+60 added−23 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+60 added−23 removed
- other:+60 added−23 removed
Provenance
- Commit
0c030f7- Tree
bcab4af- Author
Ian Stapleton Cordasco <graffatcolmingov@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
d6dded3- Parent 2
555b870
5 files changed+60 added−23 removed
modified:.github/workflows/run-tests.yml+20 added
35 unmodified lines36373839404142434445464748495051525354555657585935 unmodified lines make - name: Run tests run: | make ci
no_chardet: name: "No Character Detection" runs-on: ubuntu-latest strategy: fail-fast: true
steps: - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - name: 'Set up Python 3.8' uses: actions/setup-python@82c7e631bb3cdc910f68e0081d67478d79c6982d with: python-version: '3.8' - name: Install dependencies run: | make python -m pip uninstall -y "charset_normalizer" "chardet" - name: Run tests run: | make cimodified:src/requests/__init__.py+5 added-1 removed
81 unmodified lines82 major, minor, patch = int(major), int(minor), int(patch)83 # charset_normalizer >= 2.0.0 < 4.0.084 assert (2, 0, 0) <= (major, minor, patch) < (4, 0, 0)85 else:86- raise Exception("You need either charset_normalizer or chardet installed")86+ warnings.warn(87+ "Unable to find acceptable character detection dependency "88+ "(chardet or charset_normalizer).",89+ RequestsDependencyWarning,90+ )91 92 93 def _check_cryptography(cryptography_version):94 # cryptography < 1.3.490 unmodified linesmodified:src/requests/compat.py+20 added-5 removed
5 unmodified lines6 between Python 2 and Python 3. It remains for backwards7 compatibility until the next major version.8 """9 10-try:11- import chardet12-except ImportError:13- import charset_normalizer as chardet14-10+import importlib11 import sys12 13+# -------------------14+# Character Detection15+# -------------------16+17+18+def _resolve_char_detection():19+ """Find supported character detection libraries."""20+ chardet = None21+ for lib in ("chardet", "charset_normalizer"):22+ if chardet is None:23+ try:24+ chardet = importlib.import_module(lib)25+ except ImportError:26+ pass27+ return chardet28+29+30+chardet = _resolve_char_detection()31+32 # -------33 # Pythons34 # -------35 59 unmodified linesmodified:src/requests/models.py+6 added-1 removed
787 unmodified lines788 789 @property790 def apparent_encoding(self):791 """The apparent encoding, provided by the charset_normalizer or chardet libraries."""792+ if chardet is not None:792- return chardet.detect(self.content)["encoding"]793+ return chardet.detect(self.content)["encoding"]794+ else:795+ # If no character detection library is available, we'll fall back796+ # to a standard Python utf-8 str.797+ return "utf-8"798 799 def iter_content(self, chunk_size=1, decode_unicode=False):800 """Iterates over the response data. When stream=True is set on the801 request, this avoids reading the content at once into memory for236 unmodified linesmodified:src/requests/packages.py+9 added-16 removed
1 import sys2 3-try:4- import chardet3+from .compat import chardet5-except ImportError:6- import warnings4 8- import charset_normalizer as chardet9-10- warnings.filterwarnings("ignore", "Trying to detect", module="charset_normalizer")11-5 # This code exists for backwards compatibility reasons.6 # I don't like it either. Just look the other way. :)7 8 for package in ("urllib3", "idna"):3 unmodified lines12 for mod in list(sys.modules):13 if mod == package or mod.startswith(f"{package}."):14 sys.modules[f"requests.packages.{mod}"] = sys.modules[mod]15 23-target = chardet.__name__24-for mod in list(sys.modules):25- if mod == target or mod.startswith(f"{target}."):26- imported_mod = sys.modules[mod]27- sys.modules[f"requests.packages.{mod}"] = imported_mod28- mod = mod.replace(target, "chardet")29- sys.modules[f"requests.packages.{mod}"] = imported_mod30-# Kinda cool, though, right?16+if chardet is not None:17+ target = chardet.__name__18+ for mod in list(sys.modules):19+ if mod == target or mod.startswith(f"{target}."):20+ imported_mod = sys.modules[mod]21+ sys.modules[f"requests.packages.{mod}"] = imported_mod22+ mod = mod.replace(target, "chardet")23+ sys.modules[f"requests.packages.{mod}"] = imported_mod