| 1 | n/a | """ | 
|---|
| 2 | n/a | Define names for built-in types that aren't directly accessible as a builtin. | 
|---|
| 3 | n/a | """ | 
|---|
| 4 | n/a | import sys | 
|---|
| 5 | n/a |  | 
|---|
| 6 | n/a | # Iterators in Python aren't a matter of type but of protocol.  A large | 
|---|
| 7 | n/a | # and changing number of builtin types implement *some* flavor of | 
|---|
| 8 | n/a | # iterator.  Don't check the type!  Use hasattr to check for both | 
|---|
| 9 | n/a | # "__iter__" and "__next__" attributes instead. | 
|---|
| 10 | n/a |  | 
|---|
| 11 | n/a | def _f(): pass | 
|---|
| 12 | n/a | FunctionType = type(_f) | 
|---|
| 13 | n/a | LambdaType = type(lambda: None)         # Same as FunctionType | 
|---|
| 14 | n/a | CodeType = type(_f.__code__) | 
|---|
| 15 | n/a | MappingProxyType = type(type.__dict__) | 
|---|
| 16 | n/a | SimpleNamespace = type(sys.implementation) | 
|---|
| 17 | n/a |  | 
|---|
| 18 | n/a | def _g(): | 
|---|
| 19 | n/a | yield 1 | 
|---|
| 20 | n/a | GeneratorType = type(_g()) | 
|---|
| 21 | n/a |  | 
|---|
| 22 | n/a | async def _c(): pass | 
|---|
| 23 | n/a | _c = _c() | 
|---|
| 24 | n/a | CoroutineType = type(_c) | 
|---|
| 25 | n/a | _c.close()  # Prevent ResourceWarning | 
|---|
| 26 | n/a |  | 
|---|
| 27 | n/a | async def _ag(): | 
|---|
| 28 | n/a | yield | 
|---|
| 29 | n/a | _ag = _ag() | 
|---|
| 30 | n/a | AsyncGeneratorType = type(_ag) | 
|---|
| 31 | n/a |  | 
|---|
| 32 | n/a | class _C: | 
|---|
| 33 | n/a | def _m(self): pass | 
|---|
| 34 | n/a | MethodType = type(_C()._m) | 
|---|
| 35 | n/a |  | 
|---|
| 36 | n/a | BuiltinFunctionType = type(len) | 
|---|
| 37 | n/a | BuiltinMethodType = type([].append)     # Same as BuiltinFunctionType | 
|---|
| 38 | n/a |  | 
|---|
| 39 | n/a | SlotWrapperType = type(object.__init__) | 
|---|
| 40 | n/a | MethodWrapperType = type(object().__str__) | 
|---|
| 41 | n/a | MethodDescriptorType = type(str.join) | 
|---|
| 42 | n/a |  | 
|---|
| 43 | n/a | ModuleType = type(sys) | 
|---|
| 44 | n/a |  | 
|---|
| 45 | n/a | try: | 
|---|
| 46 | n/a | raise TypeError | 
|---|
| 47 | n/a | except TypeError: | 
|---|
| 48 | n/a | tb = sys.exc_info()[2] | 
|---|
| 49 | n/a | TracebackType = type(tb) | 
|---|
| 50 | n/a | FrameType = type(tb.tb_frame) | 
|---|
| 51 | n/a | tb = None; del tb | 
|---|
| 52 | n/a |  | 
|---|
| 53 | n/a | # For Jython, the following two types are identical | 
|---|
| 54 | n/a | GetSetDescriptorType = type(FunctionType.__code__) | 
|---|
| 55 | n/a | MemberDescriptorType = type(FunctionType.__globals__) | 
|---|
| 56 | n/a |  | 
|---|
| 57 | n/a | del sys, _f, _g, _C, _c,                           # Not for export | 
|---|
| 58 | n/a |  | 
|---|
| 59 | n/a |  | 
|---|
| 60 | n/a | # Provide a PEP 3115 compliant mechanism for class creation | 
|---|
| 61 | n/a | def new_class(name, bases=(), kwds=None, exec_body=None): | 
|---|
| 62 | n/a | """Create a class object dynamically using the appropriate metaclass.""" | 
|---|
| 63 | n/a | meta, ns, kwds = prepare_class(name, bases, kwds) | 
|---|
| 64 | n/a | if exec_body is not None: | 
|---|
| 65 | n/a | exec_body(ns) | 
|---|
| 66 | n/a | return meta(name, bases, ns, **kwds) | 
|---|
| 67 | n/a |  | 
|---|
| 68 | n/a | def prepare_class(name, bases=(), kwds=None): | 
|---|
| 69 | n/a | """Call the __prepare__ method of the appropriate metaclass. | 
|---|
| 70 | n/a |  | 
|---|
| 71 | n/a | Returns (metaclass, namespace, kwds) as a 3-tuple | 
|---|
| 72 | n/a |  | 
|---|
| 73 | n/a | *metaclass* is the appropriate metaclass | 
|---|
| 74 | n/a | *namespace* is the prepared class namespace | 
|---|
| 75 | n/a | *kwds* is an updated copy of the passed in kwds argument with any | 
|---|
| 76 | n/a | 'metaclass' entry removed. If no kwds argument is passed in, this will | 
|---|
| 77 | n/a | be an empty dict. | 
|---|
| 78 | n/a | """ | 
|---|
| 79 | n/a | if kwds is None: | 
|---|
| 80 | n/a | kwds = {} | 
|---|
| 81 | n/a | else: | 
|---|
| 82 | n/a | kwds = dict(kwds) # Don't alter the provided mapping | 
|---|
| 83 | n/a | if 'metaclass' in kwds: | 
|---|
| 84 | n/a | meta = kwds.pop('metaclass') | 
|---|
| 85 | n/a | else: | 
|---|
| 86 | n/a | if bases: | 
|---|
| 87 | n/a | meta = type(bases[0]) | 
|---|
| 88 | n/a | else: | 
|---|
| 89 | n/a | meta = type | 
|---|
| 90 | n/a | if isinstance(meta, type): | 
|---|
| 91 | n/a | # when meta is a type, we first determine the most-derived metaclass | 
|---|
| 92 | n/a | # instead of invoking the initial candidate directly | 
|---|
| 93 | n/a | meta = _calculate_meta(meta, bases) | 
|---|
| 94 | n/a | if hasattr(meta, '__prepare__'): | 
|---|
| 95 | n/a | ns = meta.__prepare__(name, bases, **kwds) | 
|---|
| 96 | n/a | else: | 
|---|
| 97 | n/a | ns = {} | 
|---|
| 98 | n/a | return meta, ns, kwds | 
|---|
| 99 | n/a |  | 
|---|
| 100 | n/a | def _calculate_meta(meta, bases): | 
|---|
| 101 | n/a | """Calculate the most derived metaclass.""" | 
|---|
| 102 | n/a | winner = meta | 
|---|
| 103 | n/a | for base in bases: | 
|---|
| 104 | n/a | base_meta = type(base) | 
|---|
| 105 | n/a | if issubclass(winner, base_meta): | 
|---|
| 106 | n/a | continue | 
|---|
| 107 | n/a | if issubclass(base_meta, winner): | 
|---|
| 108 | n/a | winner = base_meta | 
|---|
| 109 | n/a | continue | 
|---|
| 110 | n/a | # else: | 
|---|
| 111 | n/a | raise TypeError("metaclass conflict: " | 
|---|
| 112 | n/a | "the metaclass of a derived class " | 
|---|
| 113 | n/a | "must be a (non-strict) subclass " | 
|---|
| 114 | n/a | "of the metaclasses of all its bases") | 
|---|
| 115 | n/a | return winner | 
|---|
| 116 | n/a |  | 
|---|
| 117 | n/a | class DynamicClassAttribute: | 
|---|
| 118 | n/a | """Route attribute access on a class to __getattr__. | 
|---|
| 119 | n/a |  | 
|---|
| 120 | n/a | This is a descriptor, used to define attributes that act differently when | 
|---|
| 121 | n/a | accessed through an instance and through a class.  Instance access remains | 
|---|
| 122 | n/a | normal, but access to an attribute through a class will be routed to the | 
|---|
| 123 | n/a | class's __getattr__ method; this is done by raising AttributeError. | 
|---|
| 124 | n/a |  | 
|---|
| 125 | n/a | This allows one to have properties active on an instance, and have virtual | 
|---|
| 126 | n/a | attributes on the class with the same name (see Enum for an example). | 
|---|
| 127 | n/a |  | 
|---|
| 128 | n/a | """ | 
|---|
| 129 | n/a | def __init__(self, fget=None, fset=None, fdel=None, doc=None): | 
|---|
| 130 | n/a | self.fget = fget | 
|---|
| 131 | n/a | self.fset = fset | 
|---|
| 132 | n/a | self.fdel = fdel | 
|---|
| 133 | n/a | # next two lines make DynamicClassAttribute act the same as property | 
|---|
| 134 | n/a | self.__doc__ = doc or fget.__doc__ | 
|---|
| 135 | n/a | self.overwrite_doc = doc is None | 
|---|
| 136 | n/a | # support for abstract methods | 
|---|
| 137 | n/a | self.__isabstractmethod__ = bool(getattr(fget, '__isabstractmethod__', False)) | 
|---|
| 138 | n/a |  | 
|---|
| 139 | n/a | def __get__(self, instance, ownerclass=None): | 
|---|
| 140 | n/a | if instance is None: | 
|---|
| 141 | n/a | if self.__isabstractmethod__: | 
|---|
| 142 | n/a | return self | 
|---|
| 143 | n/a | raise AttributeError() | 
|---|
| 144 | n/a | elif self.fget is None: | 
|---|
| 145 | n/a | raise AttributeError("unreadable attribute") | 
|---|
| 146 | n/a | return self.fget(instance) | 
|---|
| 147 | n/a |  | 
|---|
| 148 | n/a | def __set__(self, instance, value): | 
|---|
| 149 | n/a | if self.fset is None: | 
|---|
| 150 | n/a | raise AttributeError("can't set attribute") | 
|---|
| 151 | n/a | self.fset(instance, value) | 
|---|
| 152 | n/a |  | 
|---|
| 153 | n/a | def __delete__(self, instance): | 
|---|
| 154 | n/a | if self.fdel is None: | 
|---|
| 155 | n/a | raise AttributeError("can't delete attribute") | 
|---|
| 156 | n/a | self.fdel(instance) | 
|---|
| 157 | n/a |  | 
|---|
| 158 | n/a | def getter(self, fget): | 
|---|
| 159 | n/a | fdoc = fget.__doc__ if self.overwrite_doc else None | 
|---|
| 160 | n/a | result = type(self)(fget, self.fset, self.fdel, fdoc or self.__doc__) | 
|---|
| 161 | n/a | result.overwrite_doc = self.overwrite_doc | 
|---|
| 162 | n/a | return result | 
|---|
| 163 | n/a |  | 
|---|
| 164 | n/a | def setter(self, fset): | 
|---|
| 165 | n/a | result = type(self)(self.fget, fset, self.fdel, self.__doc__) | 
|---|
| 166 | n/a | result.overwrite_doc = self.overwrite_doc | 
|---|
| 167 | n/a | return result | 
|---|
| 168 | n/a |  | 
|---|
| 169 | n/a | def deleter(self, fdel): | 
|---|
| 170 | n/a | result = type(self)(self.fget, self.fset, fdel, self.__doc__) | 
|---|
| 171 | n/a | result.overwrite_doc = self.overwrite_doc | 
|---|
| 172 | n/a | return result | 
|---|
| 173 | n/a |  | 
|---|
| 174 | n/a |  | 
|---|
| 175 | n/a | import functools as _functools | 
|---|
| 176 | n/a | import collections.abc as _collections_abc | 
|---|
| 177 | n/a |  | 
|---|
| 178 | n/a | class _GeneratorWrapper: | 
|---|
| 179 | n/a | # TODO: Implement this in C. | 
|---|
| 180 | n/a | def __init__(self, gen): | 
|---|
| 181 | n/a | self.__wrapped = gen | 
|---|
| 182 | n/a | self.__isgen = gen.__class__ is GeneratorType | 
|---|
| 183 | n/a | self.__name__ = getattr(gen, '__name__', None) | 
|---|
| 184 | n/a | self.__qualname__ = getattr(gen, '__qualname__', None) | 
|---|
| 185 | n/a | def send(self, val): | 
|---|
| 186 | n/a | return self.__wrapped.send(val) | 
|---|
| 187 | n/a | def throw(self, tp, *rest): | 
|---|
| 188 | n/a | return self.__wrapped.throw(tp, *rest) | 
|---|
| 189 | n/a | def close(self): | 
|---|
| 190 | n/a | return self.__wrapped.close() | 
|---|
| 191 | n/a | @property | 
|---|
| 192 | n/a | def gi_code(self): | 
|---|
| 193 | n/a | return self.__wrapped.gi_code | 
|---|
| 194 | n/a | @property | 
|---|
| 195 | n/a | def gi_frame(self): | 
|---|
| 196 | n/a | return self.__wrapped.gi_frame | 
|---|
| 197 | n/a | @property | 
|---|
| 198 | n/a | def gi_running(self): | 
|---|
| 199 | n/a | return self.__wrapped.gi_running | 
|---|
| 200 | n/a | @property | 
|---|
| 201 | n/a | def gi_yieldfrom(self): | 
|---|
| 202 | n/a | return self.__wrapped.gi_yieldfrom | 
|---|
| 203 | n/a | cr_code = gi_code | 
|---|
| 204 | n/a | cr_frame = gi_frame | 
|---|
| 205 | n/a | cr_running = gi_running | 
|---|
| 206 | n/a | cr_await = gi_yieldfrom | 
|---|
| 207 | n/a | def __next__(self): | 
|---|
| 208 | n/a | return next(self.__wrapped) | 
|---|
| 209 | n/a | def __iter__(self): | 
|---|
| 210 | n/a | if self.__isgen: | 
|---|
| 211 | n/a | return self.__wrapped | 
|---|
| 212 | n/a | return self | 
|---|
| 213 | n/a | __await__ = __iter__ | 
|---|
| 214 | n/a |  | 
|---|
| 215 | n/a | def coroutine(func): | 
|---|
| 216 | n/a | """Convert regular generator function to a coroutine.""" | 
|---|
| 217 | n/a |  | 
|---|
| 218 | n/a | if not callable(func): | 
|---|
| 219 | n/a | raise TypeError('types.coroutine() expects a callable') | 
|---|
| 220 | n/a |  | 
|---|
| 221 | n/a | if (func.__class__ is FunctionType and | 
|---|
| 222 | n/a | getattr(func, '__code__', None).__class__ is CodeType): | 
|---|
| 223 | n/a |  | 
|---|
| 224 | n/a | co_flags = func.__code__.co_flags | 
|---|
| 225 | n/a |  | 
|---|
| 226 | n/a | # Check if 'func' is a coroutine function. | 
|---|
| 227 | n/a | # (0x180 == CO_COROUTINE | CO_ITERABLE_COROUTINE) | 
|---|
| 228 | n/a | if co_flags & 0x180: | 
|---|
| 229 | n/a | return func | 
|---|
| 230 | n/a |  | 
|---|
| 231 | n/a | # Check if 'func' is a generator function. | 
|---|
| 232 | n/a | # (0x20 == CO_GENERATOR) | 
|---|
| 233 | n/a | if co_flags & 0x20: | 
|---|
| 234 | n/a | # TODO: Implement this in C. | 
|---|
| 235 | n/a | co = func.__code__ | 
|---|
| 236 | n/a | func.__code__ = CodeType( | 
|---|
| 237 | n/a | co.co_argcount, co.co_kwonlyargcount, co.co_nlocals, | 
|---|
| 238 | n/a | co.co_stacksize, | 
|---|
| 239 | n/a | co.co_flags | 0x100,  # 0x100 == CO_ITERABLE_COROUTINE | 
|---|
| 240 | n/a | co.co_code, | 
|---|
| 241 | n/a | co.co_consts, co.co_names, co.co_varnames, co.co_filename, | 
|---|
| 242 | n/a | co.co_name, co.co_firstlineno, co.co_lnotab, co.co_freevars, | 
|---|
| 243 | n/a | co.co_cellvars) | 
|---|
| 244 | n/a | return func | 
|---|
| 245 | n/a |  | 
|---|
| 246 | n/a | # The following code is primarily to support functions that | 
|---|
| 247 | n/a | # return generator-like objects (for instance generators | 
|---|
| 248 | n/a | # compiled with Cython). | 
|---|
| 249 | n/a |  | 
|---|
| 250 | n/a | @_functools.wraps(func) | 
|---|
| 251 | n/a | def wrapped(*args, **kwargs): | 
|---|
| 252 | n/a | coro = func(*args, **kwargs) | 
|---|
| 253 | n/a | if (coro.__class__ is CoroutineType or | 
|---|
| 254 | n/a | coro.__class__ is GeneratorType and coro.gi_code.co_flags & 0x100): | 
|---|
| 255 | n/a | # 'coro' is a native coroutine object or an iterable coroutine | 
|---|
| 256 | n/a | return coro | 
|---|
| 257 | n/a | if (isinstance(coro, _collections_abc.Generator) and | 
|---|
| 258 | n/a | not isinstance(coro, _collections_abc.Coroutine)): | 
|---|
| 259 | n/a | # 'coro' is either a pure Python generator iterator, or it | 
|---|
| 260 | n/a | # implements collections.abc.Generator (and does not implement | 
|---|
| 261 | n/a | # collections.abc.Coroutine). | 
|---|
| 262 | n/a | return _GeneratorWrapper(coro) | 
|---|
| 263 | n/a | # 'coro' is either an instance of collections.abc.Coroutine or | 
|---|
| 264 | n/a | # some other object -- pass it through. | 
|---|
| 265 | n/a | return coro | 
|---|
| 266 | n/a |  | 
|---|
| 267 | n/a | return wrapped | 
|---|
| 268 | n/a |  | 
|---|
| 269 | n/a |  | 
|---|
| 270 | n/a | __all__ = [n for n in globals() if n[:1] != '_'] | 
|---|