compat.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. """
  2. requests.compat
  3. ~~~~~~~~~~~~~~~
  4. This module previously handled import compatibility issues
  5. between Python 2 and Python 3. It remains for backwards
  6. compatibility until the next major version.
  7. """
  8. # pyright: reportUnusedImport=false
  9. from __future__ import annotations
  10. import importlib
  11. import sys
  12. from types import ModuleType
  13. # -------
  14. # urllib3
  15. # -------
  16. from urllib3 import (
  17. __version__ as urllib3_version, # type: ignore[reportPrivateImportUsage]
  18. )
  19. # Detect which major version of urllib3 is being used.
  20. try:
  21. is_urllib3_1 = int(urllib3_version.split(".")[0]) == 1
  22. except (TypeError, AttributeError):
  23. # If we can't discern a version, prefer old functionality.
  24. is_urllib3_1 = True
  25. # -------------------
  26. # Character Detection
  27. # -------------------
  28. def _resolve_char_detection() -> ModuleType | None:
  29. """Find supported character detection libraries."""
  30. chardet = None
  31. for lib in ("chardet", "charset_normalizer"):
  32. if chardet is None:
  33. try:
  34. chardet = importlib.import_module(lib)
  35. except ImportError:
  36. pass
  37. return chardet
  38. chardet = _resolve_char_detection()
  39. # -------
  40. # Pythons
  41. # -------
  42. # Syntax sugar.
  43. _ver = sys.version_info
  44. #: Python 2.x?
  45. is_py2 = _ver[0] == 2
  46. #: Python 3.x?
  47. is_py3 = _ver[0] == 3
  48. # json/simplejson module import resolution
  49. has_simplejson = False
  50. try:
  51. import simplejson as json # type: ignore[import-not-found]
  52. has_simplejson = True
  53. except ImportError:
  54. import json
  55. if has_simplejson:
  56. from simplejson import JSONDecodeError # type: ignore[import-not-found]
  57. else:
  58. from json import JSONDecodeError
  59. # Keep OrderedDict for backwards compatibility.
  60. from collections import OrderedDict
  61. from collections.abc import Callable, Mapping, MutableMapping
  62. from http import cookiejar as cookielib
  63. from http.cookies import Morsel
  64. from io import StringIO
  65. # --------------
  66. # Legacy Imports
  67. # --------------
  68. from urllib.parse import (
  69. quote,
  70. quote_plus,
  71. unquote,
  72. unquote_plus,
  73. urldefrag,
  74. urlencode,
  75. urljoin,
  76. urlparse,
  77. urlsplit,
  78. urlunparse,
  79. )
  80. from urllib.request import (
  81. getproxies,
  82. getproxies_environment,
  83. parse_http_list,
  84. proxy_bypass,
  85. proxy_bypass_environment, # type: ignore[attr-defined] # https://github.com/python/cpython/issues/145331
  86. )
  87. builtin_str = str
  88. str = str
  89. bytes = bytes
  90. basestring = (str, bytes)
  91. numeric_types = (int, float)
  92. integer_types = (int,)