compat.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # -*- coding: utf-8 -*-
  2. """
  3. pythoncompat
  4. """
  5. from .packages import chardet
  6. import sys
  7. # -------
  8. # Pythons
  9. # -------
  10. # Syntax sugar.
  11. _ver = sys.version_info
  12. #: Python 2.x?
  13. is_py2 = (_ver[0] == 2)
  14. #: Python 3.x?
  15. is_py3 = (_ver[0] == 3)
  16. try:
  17. import simplejson as json
  18. except (ImportError, SyntaxError):
  19. # simplejson does not support Python 3.2, it throws a SyntaxError
  20. # because of u'...' Unicode literals.
  21. import json
  22. # ---------
  23. # Specifics
  24. # ---------
  25. if is_py2:
  26. from urllib import quote, unquote, quote_plus, unquote_plus, urlencode, getproxies, proxy_bypass
  27. from urlparse import urlparse, urlunparse, urljoin, urlsplit, urldefrag
  28. from urllib2 import parse_http_list
  29. import cookielib
  30. from Cookie import Morsel
  31. from StringIO import StringIO
  32. from .packages.urllib3.packages.ordered_dict import OrderedDict
  33. builtin_str = str
  34. bytes = str
  35. str = unicode
  36. basestring = basestring
  37. numeric_types = (int, long, float)
  38. elif is_py3:
  39. from urllib.parse import urlparse, urlunparse, urljoin, urlsplit, urlencode, quote, unquote, quote_plus, unquote_plus, urldefrag
  40. from urllib.request import parse_http_list, getproxies, proxy_bypass
  41. from http import cookiejar as cookielib
  42. from http.cookies import Morsel
  43. from io import StringIO
  44. from collections import OrderedDict
  45. builtin_str = str
  46. str = str
  47. bytes = bytes
  48. basestring = (str, bytes)
  49. numeric_types = (int, float)