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