You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
1.6 KiB

4 years ago
  1. # -*- coding: utf-8 -*-
  2. """
  3. requests.compat
  4. ~~~~~~~~~~~~~~~
  5. This module handles import compatibility issues between Python 2 and
  6. Python 3.
  7. """
  8. import chardet
  9. import sys
  10. # -------
  11. # Pythons
  12. # -------
  13. # Syntax sugar.
  14. _ver = sys.version_info
  15. #: Python 2.x?
  16. is_py2 = (_ver[0] == 2)
  17. #: Python 3.x?
  18. is_py3 = (_ver[0] == 3)
  19. try:
  20. import simplejson as json
  21. except ImportError:
  22. import json
  23. # ---------
  24. # Specifics
  25. # ---------
  26. if is_py2:
  27. from urllib import (
  28. quote, unquote, quote_plus, unquote_plus, urlencode, getproxies,
  29. proxy_bypass, proxy_bypass_environment, getproxies_environment)
  30. from urlparse import urlparse, urlunparse, urljoin, urlsplit, urldefrag
  31. from urllib2 import parse_http_list
  32. import cookielib
  33. from Cookie import Morsel
  34. from StringIO import StringIO
  35. from collections import Callable, Mapping, MutableMapping, OrderedDict
  36. builtin_str = str
  37. bytes = str
  38. str = unicode
  39. basestring = basestring
  40. numeric_types = (int, long, float)
  41. integer_types = (int, long)
  42. elif is_py3:
  43. from urllib.parse import urlparse, urlunparse, urljoin, urlsplit, urlencode, quote, unquote, quote_plus, unquote_plus, urldefrag
  44. from urllib.request import parse_http_list, getproxies, proxy_bypass, proxy_bypass_environment, getproxies_environment
  45. from http import cookiejar as cookielib
  46. from http.cookies import Morsel
  47. from io import StringIO
  48. from collections import OrderedDict
  49. from collections.abc import Callable, Mapping, MutableMapping
  50. builtin_str = str
  51. str = str
  52. bytes = bytes
  53. basestring = (str, bytes)
  54. numeric_types = (int, float)
  55. integer_types = (int,)