compat.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. from .packages 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
  36. from .packages.urllib3.packages.ordered_dict import 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. 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,)