compat.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. import sys
  9. # -------
  10. # urllib3
  11. # -------
  12. from pip._vendor.urllib3 import __version__ as urllib3_version
  13. # Detect which major version of urllib3 is being used.
  14. try:
  15. is_urllib3_1 = int(urllib3_version.split(".")[0]) == 1
  16. except (TypeError, AttributeError):
  17. # If we can't discern a version, prefer old functionality.
  18. is_urllib3_1 = True
  19. # -------------------
  20. # Character Detection
  21. # -------------------
  22. def _resolve_char_detection():
  23. """Find supported character detection libraries."""
  24. chardet = None
  25. return chardet
  26. chardet = _resolve_char_detection()
  27. # -------
  28. # Pythons
  29. # -------
  30. # Syntax sugar.
  31. _ver = sys.version_info
  32. #: Python 2.x?
  33. is_py2 = _ver[0] == 2
  34. #: Python 3.x?
  35. is_py3 = _ver[0] == 3
  36. # Note: We've patched out simplejson support in pip because it prevents
  37. # upgrading simplejson on Windows.
  38. import json
  39. from json import JSONDecodeError
  40. # Keep OrderedDict for backwards compatibility.
  41. from collections import OrderedDict
  42. from collections.abc import Callable, Mapping, MutableMapping
  43. from http import cookiejar as cookielib
  44. from http.cookies import Morsel
  45. from io import StringIO
  46. # --------------
  47. # Legacy Imports
  48. # --------------
  49. from urllib.parse import (
  50. quote,
  51. quote_plus,
  52. unquote,
  53. unquote_plus,
  54. urldefrag,
  55. urlencode,
  56. urljoin,
  57. urlparse,
  58. urlsplit,
  59. urlunparse,
  60. )
  61. from urllib.request import (
  62. getproxies,
  63. getproxies_environment,
  64. parse_http_list,
  65. proxy_bypass,
  66. proxy_bypass_environment,
  67. )
  68. builtin_str = str
  69. str = str
  70. bytes = bytes
  71. basestring = (str, bytes)
  72. numeric_types = (int, float)
  73. integer_types = (int,)