Skip to content
Myr

Allow character detection dependencies to be optional in post-packaging steps

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Nate Prewitt · committed by Nate Prewitt · parent d6dded3 · 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

5 files+60 added−23 removed

modified:.github/workflows/run-tests.yml+20 added
modified:src/requests/__init__.py+5 added-1 removed
81 unmodified lines
82 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 )919293def _check_cryptography(cryptography_version):94 # cryptography < 1.3.4
90 unmodified lines
modified:src/requests/compat.py+20 added-5 removed
5 unmodified lines
6between Python 2 and Python 3. It remains for backwards7compatibility until the next major version.8"""910try:11 import chardet12except ImportError:13 import charset_normalizer as chardet1410import importlib11import sys1213# -------------------14# Character Detection15# -------------------161718def _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 chardet282930chardet = _resolve_char_detection()3132# -------33# Pythons34# -------35
59 unmodified lines
modified:src/requests/models.py+6 added-1 removed
787 unmodified lines
788789 @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"798799 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 for
236 unmodified lines
modified:src/requests/packages.py+9 added-16 removed
1import sys23try:4 import chardet3from .compat import chardet5except ImportError:6 import warnings48 import charset_normalizer as chardet910 warnings.filterwarnings("ignore", "Trying to detect", module="charset_normalizer")115# This code exists for backwards compatibility reasons.6# I don't like it either. Just look the other way. :)78for package in ("urllib3", "idna"):
3 unmodified lines
12 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]1523target = chardet.__name__24for 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?16if 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