compat.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. # Keep OrderedDict for backwards compatibility.
  36. from collections import Callable, Mapping, MutableMapping, OrderedDict
  37. builtin_str = str
  38. bytes = str
  39. str = unicode
  40. basestring = basestring
  41. numeric_types = (int, long, float)
  42. integer_types = (int, long)
  43. elif is_py3:
  44. from urllib.parse import urlparse, urlunparse, urljoin, urlsplit, urlencode, quote, unquote, quote_plus, unquote_plus, urldefrag
  45. from urllib.request import parse_http_list, getproxies, proxy_bypass, proxy_bypass_environment, getproxies_environment
  46. from http import cookiejar as cookielib
  47. from http.cookies import Morsel
  48. from io import StringIO
  49. # Keep OrderedDict for backwards compatibility.
  50. from collections import OrderedDict
  51. from collections.abc import Callable, Mapping, MutableMapping
  52. builtin_str = str
  53. str = str
  54. bytes = bytes
  55. basestring = (str, bytes)
  56. numeric_types = (int, float)
  57. integer_types = (int,)