compat.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import sys
  2. import time
  3. py3k = sys.version_info >= (3, 0)
  4. py33 = sys.version_info >= (3, 3)
  5. py26 = sys.version_info >= (2, 6)
  6. py25 = sys.version_info >= (2, 5)
  7. jython = sys.platform.startswith('java')
  8. win32 = sys.platform.startswith('win')
  9. pypy = hasattr(sys, 'pypy_version_info')
  10. if py3k:
  11. from io import StringIO
  12. import builtins as compat_builtins
  13. from urllib.parse import quote_plus, unquote_plus
  14. from html.entities import codepoint2name, name2codepoint
  15. string_types = str,
  16. binary_type = bytes
  17. text_type = str
  18. def u(s):
  19. return s
  20. def octal(lit):
  21. return eval("0o" + lit)
  22. else:
  23. import __builtin__ as compat_builtins
  24. try:
  25. from cStringIO import StringIO
  26. except:
  27. from StringIO import StringIO
  28. from urllib import quote_plus, unquote_plus
  29. from htmlentitydefs import codepoint2name, name2codepoint
  30. string_types = basestring,
  31. binary_type = str
  32. text_type = unicode
  33. def u(s):
  34. return unicode(s, "utf-8")
  35. def octal(lit):
  36. return eval("0" + lit)
  37. if py33:
  38. from importlib import machinery
  39. def load_module(module_id, path):
  40. return machinery.SourceFileLoader(module_id, path).load_module()
  41. else:
  42. import imp
  43. def load_module(module_id, path):
  44. fp = open(path, 'rb')
  45. try:
  46. return imp.load_source(module_id, path, fp)
  47. finally:
  48. fp.close()
  49. def exception_as():
  50. return sys.exc_info()[1]
  51. try:
  52. import threading
  53. if py3k:
  54. import _thread as thread
  55. else:
  56. import thread
  57. except ImportError:
  58. import dummy_threading as threading
  59. if py3k:
  60. import _dummy_thread as thread
  61. else:
  62. import dummy_thread as thread
  63. if win32 or jython:
  64. time_func = time.clock
  65. else:
  66. time_func = time.time
  67. try:
  68. from functools import partial
  69. except:
  70. def partial(func, *args, **keywords):
  71. def newfunc(*fargs, **fkeywords):
  72. newkeywords = keywords.copy()
  73. newkeywords.update(fkeywords)
  74. return func(*(args + fargs), **newkeywords)
  75. return newfunc
  76. if not py25:
  77. def all(iterable):
  78. for i in iterable:
  79. if not i:
  80. return False
  81. return True
  82. def exception_name(exc):
  83. try:
  84. return exc.__class__.__name__
  85. except AttributeError:
  86. return exc.__name__
  87. else:
  88. all = all
  89. def exception_name(exc):
  90. return exc.__class__.__name__
  91. try:
  92. from inspect import CO_VARKEYWORDS, CO_VARARGS
  93. def inspect_func_args(fn):
  94. if py3k:
  95. co = fn.__code__
  96. else:
  97. co = fn.func_code
  98. nargs = co.co_argcount
  99. names = co.co_varnames
  100. args = list(names[:nargs])
  101. varargs = None
  102. if co.co_flags & CO_VARARGS:
  103. varargs = co.co_varnames[nargs]
  104. nargs = nargs + 1
  105. varkw = None
  106. if co.co_flags & CO_VARKEYWORDS:
  107. varkw = co.co_varnames[nargs]
  108. if py3k:
  109. return args, varargs, varkw, fn.__defaults__
  110. else:
  111. return args, varargs, varkw, fn.func_defaults
  112. except ImportError:
  113. import inspect
  114. def inspect_func_args(fn):
  115. return inspect.getargspec(fn)
  116. if py3k:
  117. def callable(fn):
  118. return hasattr(fn, '__call__')
  119. else:
  120. callable = callable
  121. ################################################
  122. # cross-compatible metaclass implementation
  123. # Copyright (c) 2010-2012 Benjamin Peterson
  124. def with_metaclass(meta, base=object):
  125. """Create a base class with a metaclass."""
  126. return meta("%sBase" % meta.__name__, (base,), {})
  127. ################################################