compat.py 2.5 KB

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