compat.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 urllib3.packages.ordered_dict import 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. builtin_str = str
  50. str = str
  51. bytes = bytes
  52. basestring = (str, bytes)
  53. numeric_types = (int, float)
  54. integer_types = (int,)