compat.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 sys
  9. # -------
  10. # Pythons
  11. # -------
  12. # Syntax sugar.
  13. _ver = sys.version_info
  14. #: Python 2.x?
  15. is_py2 = (_ver[0] == 2)
  16. #: Python 3.x?
  17. is_py3 = (_ver[0] == 3)
  18. try:
  19. import simplejson as json
  20. except ImportError:
  21. import json
  22. # ---------
  23. # Specifics
  24. # ---------
  25. if is_py2:
  26. from urllib import (
  27. quote, unquote, quote_plus, unquote_plus, urlencode, getproxies,
  28. proxy_bypass, proxy_bypass_environment, getproxies_environment)
  29. from urlparse import urlparse, urlunparse, urljoin, urlsplit, urldefrag
  30. from urllib2 import parse_http_list
  31. import cookielib
  32. from Cookie import Morsel
  33. from StringIO import StringIO
  34. # Keep OrderedDict for backwards compatibility.
  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. # Keep OrderedDict for backwards compatibility.
  49. from collections import OrderedDict
  50. from collections.abc import Callable, Mapping, MutableMapping
  51. builtin_str = str
  52. str = str
  53. bytes = bytes
  54. basestring = (str, bytes)
  55. numeric_types = (int, float)
  56. integer_types = (int,)