compat.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. from urllib3.packages.ordered_dict import OrderedDict
  35. builtin_str = str
  36. bytes = str
  37. str = unicode
  38. basestring = basestring
  39. numeric_types = (int, long, float)
  40. integer_types = (int, long)
  41. elif is_py3:
  42. from urllib.parse import urlparse, urlunparse, urljoin, urlsplit, urlencode, quote, unquote, quote_plus, unquote_plus, urldefrag
  43. from urllib.request import parse_http_list, getproxies, proxy_bypass, proxy_bypass_environment, getproxies_environment
  44. from http import cookiejar as cookielib
  45. from http.cookies import Morsel
  46. from io import StringIO
  47. from collections import OrderedDict
  48. builtin_str = str
  49. str = str
  50. bytes = bytes
  51. basestring = (str, bytes)
  52. numeric_types = (int, float)
  53. integer_types = (int,)