1import sys
2
3from .compat import chardet
4
5# This code exists for backwards compatibility reasons.
6# I don't like it either. Just look the other way. :)
7
8for package in ("urllib3", "idna"):
9 locals()[package] = __import__(package)
10 # This traversal is apparently necessary such that the identities are
11 # preserved (requests.packages.urllib3.* is urllib3.*)
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]
15
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_mod
22 mod = mod.replace(target, "chardet")
23 sys.modules[f"requests.packages.{mod}"] = imported_mod