1from __future__ import absolute_import 2import sys 3 4# True if we are running on Python 2. 5PY2 = sys.version_info[0] == 2 6PYVER = sys.version_info[:2] 7JYTHON = sys.platform.startswith('java') 8 9if PY2 and not JYTHON: # pragma: no cover 10 import cPickle as pickle 11else: # pragma: no cover 12 import pickle 13 14 15if not PY2: # pragma: no cover 16 xrange_ = range 17 NoneType = type(None) 18 19 string_type = str 20 unicode_text = str 21 byte_string = bytes 22 23 from urllib.parse import urlencode as url_encode 24 from urllib.parse import quote as url_quote 25 from urllib.parse import unquote as url_unquote 26 from urllib.parse import urlparse as url_parse 27 from urllib.request import url2pathname 28 import http.cookies as http_cookies 29 from base64 import b64decode as _b64decode, b64encode as _b64encode 30 31 try: 32 import dbm as anydbm 33 except: 34 import dumbdbm as anydbm 35 36 def b64decode(b): 37 return _b64decode(b.encode('ascii')) 38 39 def b64encode(s): 40 return _b64encode(s).decode('ascii') 41 42 def u_(s): 43 return str(s) 44 45 def bytes_(s): 46 if isinstance(s, byte_string): 47 return s 48 return str(s).encode('ascii', 'strict') 49 50 def dictkeyslist(d): 51 return list(d.keys()) 52 53else: 54 xrange_ = xrange 55 from types import NoneType 56 57 string_type = basestring 58 unicode_text = unicode 59 byte_string = str 60 61 from urllib import urlencode as url_encode 62 from urllib import quote as url_quote 63 from urllib import unquote as url_unquote 64 from urlparse import urlparse as url_parse 65 from urllib import url2pathname 66 import Cookie as http_cookies 67 from base64 import b64decode, b64encode 68 import anydbm 69 70 def u_(s): 71 if isinstance(s, unicode_text): 72 return s 73 74 if not isinstance(s, byte_string): 75 s = str(s) 76 return unicode(s, 'utf-8') 77 78 def bytes_(s): 79 if isinstance(s, byte_string): 80 return s 81 return str(s) 82 83 def dictkeyslist(d): 84 return d.keys() 85 86 87def im_func(f): 88 if not PY2: # pragma: no cover 89 return getattr(f, '__func__', None) 90 else: 91 return getattr(f, 'im_func', None) 92 93 94def default_im_func(f): 95 if not PY2: # pragma: no cover 96 return getattr(f, '__func__', f) 97 else: 98 return getattr(f, 'im_func', f) 99 100 101def im_self(f): 102 if not PY2: # pragma: no cover 103 return getattr(f, '__self__', None) 104 else: 105 return getattr(f, 'im_self', None) 106 107 108def im_class(f): 109 if not PY2: # pragma: no cover 110 self = im_self(f) 111 if self is not None: 112 return self.__class__ 113 else: 114 return None 115 else: 116 return getattr(f, 'im_class', None) 117 118 119def add_metaclass(metaclass): 120 """Class decorator for creating a class with a metaclass.""" 121 def wrapper(cls): 122 orig_vars = cls.__dict__.copy() 123 slots = orig_vars.get('__slots__') 124 if slots is not None: 125 if isinstance(slots, str): 126 slots = [slots] 127 for slots_var in slots: 128 orig_vars.pop(slots_var) 129 orig_vars.pop('__dict__', None) 130 orig_vars.pop('__weakref__', None) 131 return metaclass(cls.__name__, cls.__bases__, orig_vars) 132 return wrapper 133 134 135if not PY2: # pragma: no cover 136 import builtins 137 exec_ = getattr(builtins, "exec") 138 139 def reraise(tp, value, tb=None): 140 if value.__traceback__ is not tb: 141 raise value.with_traceback(tb) 142 raise value 143else: # pragma: no cover 144 def exec_(code, globs=None, locs=None): 145 """Execute code in a namespace.""" 146 if globs is None: 147 frame = sys._getframe(1) 148 globs = frame.f_globals 149 if locs is None: 150 locs = frame.f_locals 151 del frame 152 elif locs is None: 153 locs = globs 154 exec("""exec code in globs, locs""") 155 156 exec_("""def reraise(tp, value, tb=None): 157 raise tp, value, tb 158""") 159 160 161try: 162 from inspect import signature as func_signature 163except ImportError: 164 from funcsigs import signature as func_signature 165 166 167def bindfuncargs(arginfo, args, kwargs): 168 boundargs = arginfo.bind(*args, **kwargs) 169 return boundargs.args, boundargs.kwargs 170