compat.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. import sys
  2. import time
  3. py3k = sys.version_info >= (3, 0)
  4. py33 = sys.version_info >= (3, 3)
  5. py2k = sys.version_info < (3,)
  6. py26 = sys.version_info >= (2, 6)
  7. py27 = sys.version_info >= (2, 7)
  8. jython = sys.platform.startswith('java')
  9. win32 = sys.platform.startswith('win')
  10. pypy = hasattr(sys, 'pypy_version_info')
  11. if py3k:
  12. # create a "getargspec" from getfullargspec(), which is not deprecated
  13. # in Py3K; getargspec() has started to emit warnings as of Py3.5.
  14. # As of Py3.4, now they are trying to move from getfullargspec()
  15. # to "signature()", but getfullargspec() is not deprecated, so stick
  16. # with that for now.
  17. import collections
  18. ArgSpec = collections.namedtuple(
  19. "ArgSpec",
  20. ["args", "varargs", "keywords", "defaults"])
  21. from inspect import getfullargspec as inspect_getfullargspec
  22. def inspect_getargspec(func):
  23. return ArgSpec(
  24. *inspect_getfullargspec(func)[0:4]
  25. )
  26. else:
  27. from inspect import getargspec as inspect_getargspec # noqa
  28. if py3k:
  29. from io import StringIO
  30. import builtins as compat_builtins
  31. from urllib.parse import quote_plus, unquote_plus
  32. from html.entities import codepoint2name, name2codepoint
  33. string_types = str,
  34. binary_type = bytes
  35. text_type = str
  36. from io import BytesIO as byte_buffer
  37. def u(s):
  38. return s
  39. def b(s):
  40. return s.encode("latin-1")
  41. def octal(lit):
  42. return eval("0o" + lit)
  43. else:
  44. import __builtin__ as compat_builtins # noqa
  45. try:
  46. from cStringIO import StringIO
  47. except:
  48. from StringIO import StringIO
  49. byte_buffer = StringIO
  50. from urllib import quote_plus, unquote_plus # noqa
  51. from htmlentitydefs import codepoint2name, name2codepoint # noqa
  52. string_types = basestring, # noqa
  53. binary_type = str
  54. text_type = unicode # noqa
  55. def u(s):
  56. return unicode(s, "utf-8") # noqa
  57. def b(s):
  58. return s
  59. def octal(lit):
  60. return eval("0" + lit)
  61. if py33:
  62. from importlib import machinery
  63. def load_module(module_id, path):
  64. return machinery.SourceFileLoader(module_id, path).load_module()
  65. else:
  66. import imp
  67. def load_module(module_id, path):
  68. fp = open(path, 'rb')
  69. try:
  70. return imp.load_source(module_id, path, fp)
  71. finally:
  72. fp.close()
  73. if py3k:
  74. def reraise(tp, value, tb=None, cause=None):
  75. if cause is not None:
  76. value.__cause__ = cause
  77. if value.__traceback__ is not tb:
  78. raise value.with_traceback(tb)
  79. raise value
  80. else:
  81. exec("def reraise(tp, value, tb=None, cause=None):\n"
  82. " raise tp, value, tb\n")
  83. def exception_as():
  84. return sys.exc_info()[1]
  85. try:
  86. import threading
  87. if py3k:
  88. import _thread as thread
  89. else:
  90. import thread
  91. except ImportError:
  92. import dummy_threading as threading # noqa
  93. if py3k:
  94. import _dummy_thread as thread
  95. else:
  96. import dummy_thread as thread # noqa
  97. if win32 or jython:
  98. time_func = time.clock
  99. else:
  100. time_func = time.time
  101. try:
  102. from functools import partial
  103. except:
  104. def partial(func, *args, **keywords):
  105. def newfunc(*fargs, **fkeywords):
  106. newkeywords = keywords.copy()
  107. newkeywords.update(fkeywords)
  108. return func(*(args + fargs), **newkeywords)
  109. return newfunc
  110. all = all
  111. import json # noqa
  112. def exception_name(exc):
  113. return exc.__class__.__name__
  114. try:
  115. from inspect import CO_VARKEYWORDS, CO_VARARGS
  116. def inspect_func_args(fn):
  117. if py3k:
  118. co = fn.__code__
  119. else:
  120. co = fn.func_code
  121. nargs = co.co_argcount
  122. names = co.co_varnames
  123. args = list(names[:nargs])
  124. varargs = None
  125. if co.co_flags & CO_VARARGS:
  126. varargs = co.co_varnames[nargs]
  127. nargs = nargs + 1
  128. varkw = None
  129. if co.co_flags & CO_VARKEYWORDS:
  130. varkw = co.co_varnames[nargs]
  131. if py3k:
  132. return args, varargs, varkw, fn.__defaults__
  133. else:
  134. return args, varargs, varkw, fn.func_defaults
  135. except ImportError:
  136. import inspect
  137. def inspect_func_args(fn):
  138. return inspect.getargspec(fn)
  139. if py3k:
  140. def callable(fn):
  141. return hasattr(fn, '__call__')
  142. else:
  143. callable = callable
  144. ################################################
  145. # cross-compatible metaclass implementation
  146. # Copyright (c) 2010-2012 Benjamin Peterson
  147. def with_metaclass(meta, base=object):
  148. """Create a base class with a metaclass."""
  149. return meta("%sBase" % meta.__name__, (base,), {})
  150. ################################################
  151. def arg_stringname(func_arg):
  152. """Gets the string name of a kwarg or vararg
  153. In Python3.4 a function's args are
  154. of _ast.arg type not _ast.name
  155. """
  156. if hasattr(func_arg, 'arg'):
  157. return func_arg.arg
  158. else:
  159. return str(func_arg)