compat.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. # Note: We've patched out simplejson support in pip because it prevents
  20. # upgrading simplejson on Windows.
  21. # try:
  22. # import simplejson as json
  23. # except (ImportError, SyntaxError):
  24. # # simplejson does not support Python 3.2, it throws a SyntaxError
  25. # # because of u'...' Unicode literals.
  26. import json
  27. # ---------
  28. # Specifics
  29. # ---------
  30. if is_py2:
  31. from urllib import quote, unquote, quote_plus, unquote_plus, urlencode, getproxies, proxy_bypass
  32. from urlparse import urlparse, urlunparse, urljoin, urlsplit, urldefrag
  33. from urllib2 import parse_http_list
  34. import cookielib
  35. from Cookie import Morsel
  36. from StringIO import StringIO
  37. from .packages.urllib3.packages.ordered_dict import OrderedDict
  38. builtin_str = str
  39. bytes = str
  40. str = unicode
  41. basestring = basestring
  42. numeric_types = (int, long, float)
  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
  46. from http import cookiejar as cookielib
  47. from http.cookies import Morsel
  48. from io import StringIO
  49. from collections import OrderedDict
  50. builtin_str = str
  51. str = str
  52. bytes = bytes
  53. basestring = (str, bytes)
  54. numeric_types = (int, float)