compat.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. #: Python 3.0.x
  17. is_py30 = (is_py3 and _ver[1] == 0)
  18. #: Python 3.1.x
  19. is_py31 = (is_py3 and _ver[1] == 1)
  20. #: Python 3.2.x
  21. is_py32 = (is_py3 and _ver[1] == 2)
  22. #: Python 3.3.x
  23. is_py33 = (is_py3 and _ver[1] == 3)
  24. #: Python 3.4.x
  25. is_py34 = (is_py3 and _ver[1] == 4)
  26. #: Python 2.7.x
  27. is_py27 = (is_py2 and _ver[1] == 7)
  28. #: Python 2.6.x
  29. is_py26 = (is_py2 and _ver[1] == 6)
  30. #: Python 2.5.x
  31. is_py25 = (is_py2 and _ver[1] == 5)
  32. #: Python 2.4.x
  33. is_py24 = (is_py2 and _ver[1] == 4) # I'm assuming this is not by choice.
  34. # ---------
  35. # Platforms
  36. # ---------
  37. # Syntax sugar.
  38. _ver = sys.version.lower()
  39. is_pypy = ('pypy' in _ver)
  40. is_jython = ('jython' in _ver)
  41. is_ironpython = ('iron' in _ver)
  42. # Assume CPython, if nothing else.
  43. is_cpython = not any((is_pypy, is_jython, is_ironpython))
  44. # Windows-based system.
  45. is_windows = 'win32' in str(sys.platform).lower()
  46. # Standard Linux 2+ system.
  47. is_linux = ('linux' in str(sys.platform).lower())
  48. is_osx = ('darwin' in str(sys.platform).lower())
  49. is_hpux = ('hpux' in str(sys.platform).lower()) # Complete guess.
  50. is_solaris = ('solar==' in str(sys.platform).lower()) # Complete guess.
  51. try:
  52. import simplejson as json
  53. except (ImportError, SyntaxError):
  54. # simplejson does not support Python 3.2, it throws a SyntaxError
  55. # because of u'...' Unicode literals.
  56. import json
  57. # ---------
  58. # Specifics
  59. # ---------
  60. if is_py2:
  61. from urllib import quote, unquote, quote_plus, unquote_plus, urlencode, getproxies, proxy_bypass
  62. from urlparse import urlparse, urlunparse, urljoin, urlsplit, urldefrag
  63. from urllib2 import parse_http_list
  64. import cookielib
  65. from Cookie import Morsel
  66. from StringIO import StringIO
  67. from .packages.urllib3.packages.ordered_dict import OrderedDict
  68. builtin_str = str
  69. bytes = str
  70. str = unicode
  71. basestring = basestring
  72. numeric_types = (int, long, float)
  73. elif is_py3:
  74. from urllib.parse import urlparse, urlunparse, urljoin, urlsplit, urlencode, quote, unquote, quote_plus, unquote_plus, urldefrag
  75. from urllib.request import parse_http_list, getproxies, proxy_bypass
  76. from http import cookiejar as cookielib
  77. from http.cookies import Morsel
  78. from io import StringIO
  79. from collections import OrderedDict
  80. builtin_str = str
  81. str = str
  82. bytes = bytes
  83. basestring = (str, bytes)
  84. numeric_types = (int, float)