Georg Brandl | e5a32dc | 2007-12-21 08:16:54 +0000 | [diff] [blame] | 1 | """ |
| 2 | Define names for built-in types that aren't directly accessible as a builtin. |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 3 | """ |
Guido van Rossum | 85d8945 | 1994-06-23 11:53:27 +0000 | [diff] [blame] | 4 | import sys |
| 5 | |
Tim Peters | 26991a7 | 2001-09-25 22:02:03 +0000 | [diff] [blame] | 6 | # Iterators in Python aren't a matter of type but of protocol. A large |
| 7 | # and changing number of builtin types implement *some* flavor of |
| 8 | # iterator. Don't check the type! Use hasattr to check for both |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 9 | # "__iter__" and "__next__" attributes instead. |
Tim Peters | 26991a7 | 2001-09-25 22:02:03 +0000 | [diff] [blame] | 10 | |
Guido van Rossum | adc940e | 1994-09-29 10:04:43 +0000 | [diff] [blame] | 11 | def _f(): pass |
| 12 | FunctionType = type(_f) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 13 | LambdaType = type(lambda: None) # Same as FunctionType |
Guido van Rossum | a8add0e | 2007-05-14 22:03:55 +0000 | [diff] [blame] | 14 | CodeType = type(_f.__code__) |
Victor Stinner | 0db176f | 2012-04-16 00:16:30 +0200 | [diff] [blame] | 15 | MappingProxyType = type(type.__dict__) |
Barry Warsaw | 409da15 | 2012-06-03 16:18:47 -0400 | [diff] [blame] | 16 | SimpleNamespace = type(sys.implementation) |
Guido van Rossum | 85d8945 | 1994-06-23 11:53:27 +0000 | [diff] [blame] | 17 | |
Tim Peters | 264c659 | 2004-07-18 00:08:11 +0000 | [diff] [blame] | 18 | def _g(): |
Tim Peters | 3e7b1a0 | 2001-06-25 19:46:25 +0000 | [diff] [blame] | 19 | yield 1 |
Tim Peters | 264c659 | 2004-07-18 00:08:11 +0000 | [diff] [blame] | 20 | GeneratorType = type(_g()) |
Tim Peters | 3e7b1a0 | 2001-06-25 19:46:25 +0000 | [diff] [blame] | 21 | |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 22 | async def _c(): pass |
| 23 | _c = _c() |
| 24 | CoroutineType = type(_c) |
| 25 | _c.close() # Prevent ResourceWarning |
| 26 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 27 | async def _ag(): |
| 28 | yield |
| 29 | _ag = _ag() |
| 30 | AsyncGeneratorType = type(_ag) |
| 31 | |
Guido van Rossum | adc940e | 1994-09-29 10:04:43 +0000 | [diff] [blame] | 32 | class _C: |
Guido van Rossum | 898c915 | 1997-09-04 22:12:34 +0000 | [diff] [blame] | 33 | def _m(self): pass |
Guido van Rossum | 65810fe | 2006-05-26 19:12:38 +0000 | [diff] [blame] | 34 | MethodType = type(_C()._m) |
Guido van Rossum | 85d8945 | 1994-06-23 11:53:27 +0000 | [diff] [blame] | 35 | |
Guido van Rossum | adc940e | 1994-09-29 10:04:43 +0000 | [diff] [blame] | 36 | BuiltinFunctionType = type(len) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 37 | BuiltinMethodType = type([].append) # Same as BuiltinFunctionType |
Guido van Rossum | 85d8945 | 1994-06-23 11:53:27 +0000 | [diff] [blame] | 38 | |
Jim Fasarakis-Hilliard | 08c1601 | 2017-04-25 21:26:36 +0300 | [diff] [blame] | 39 | WrapperDescriptorType = type(object.__init__) |
Guido van Rossum | 934aba6 | 2017-02-01 10:55:58 -0800 | [diff] [blame] | 40 | MethodWrapperType = type(object().__str__) |
| 41 | MethodDescriptorType = type(str.join) |
Serhiy Storchaka | 3327a2d | 2017-12-15 14:13:41 +0200 | [diff] [blame] | 42 | ClassMethodDescriptorType = type(dict.__dict__['fromkeys']) |
Guido van Rossum | 934aba6 | 2017-02-01 10:55:58 -0800 | [diff] [blame] | 43 | |
Guido van Rossum | 85d8945 | 1994-06-23 11:53:27 +0000 | [diff] [blame] | 44 | ModuleType = type(sys) |
Guido van Rossum | 85d8945 | 1994-06-23 11:53:27 +0000 | [diff] [blame] | 45 | |
| 46 | try: |
Guido van Rossum | 898c915 | 1997-09-04 22:12:34 +0000 | [diff] [blame] | 47 | raise TypeError |
Guido van Rossum | 85d8945 | 1994-06-23 11:53:27 +0000 | [diff] [blame] | 48 | except TypeError: |
Guido van Rossum | a8add0e | 2007-05-14 22:03:55 +0000 | [diff] [blame] | 49 | tb = sys.exc_info()[2] |
| 50 | TracebackType = type(tb) |
| 51 | FrameType = type(tb.tb_frame) |
Guido van Rossum | f15d159 | 1997-09-29 23:22:12 +0000 | [diff] [blame] | 52 | tb = None; del tb |
Guido van Rossum | 85d8945 | 1994-06-23 11:53:27 +0000 | [diff] [blame] | 53 | |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 54 | # For Jython, the following two types are identical |
| 55 | GetSetDescriptorType = type(FunctionType.__code__) |
| 56 | MemberDescriptorType = type(FunctionType.__globals__) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 57 | |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 58 | del sys, _f, _g, _C, _c, # Not for export |
Nick Coghlan | 7fc570a | 2012-05-20 02:34:13 +1000 | [diff] [blame] | 59 | |
| 60 | |
| 61 | # Provide a PEP 3115 compliant mechanism for class creation |
| 62 | def new_class(name, bases=(), kwds=None, exec_body=None): |
| 63 | """Create a class object dynamically using the appropriate metaclass.""" |
Ivan Levkivskyi | 2b5fd1e | 2017-12-14 23:32:56 +0100 | [diff] [blame] | 64 | resolved_bases = resolve_bases(bases) |
| 65 | meta, ns, kwds = prepare_class(name, resolved_bases, kwds) |
Nick Coghlan | 7fc570a | 2012-05-20 02:34:13 +1000 | [diff] [blame] | 66 | if exec_body is not None: |
| 67 | exec_body(ns) |
Ivan Levkivskyi | 2b5fd1e | 2017-12-14 23:32:56 +0100 | [diff] [blame] | 68 | if resolved_bases is not bases: |
| 69 | ns['__orig_bases__'] = bases |
| 70 | return meta(name, resolved_bases, ns, **kwds) |
| 71 | |
| 72 | def resolve_bases(bases): |
| 73 | """Resolve MRO entries dynamically as specified by PEP 560.""" |
| 74 | new_bases = list(bases) |
| 75 | updated = False |
| 76 | shift = 0 |
| 77 | for i, base in enumerate(bases): |
| 78 | if isinstance(base, type): |
| 79 | continue |
| 80 | if not hasattr(base, "__mro_entries__"): |
| 81 | continue |
| 82 | new_base = base.__mro_entries__(bases) |
| 83 | updated = True |
| 84 | if not isinstance(new_base, tuple): |
| 85 | raise TypeError("__mro_entries__ must return a tuple") |
| 86 | else: |
| 87 | new_bases[i+shift:i+shift+1] = new_base |
| 88 | shift += len(new_base) - 1 |
| 89 | if not updated: |
| 90 | return bases |
| 91 | return tuple(new_bases) |
Nick Coghlan | 7fc570a | 2012-05-20 02:34:13 +1000 | [diff] [blame] | 92 | |
| 93 | def prepare_class(name, bases=(), kwds=None): |
| 94 | """Call the __prepare__ method of the appropriate metaclass. |
| 95 | |
| 96 | Returns (metaclass, namespace, kwds) as a 3-tuple |
| 97 | |
| 98 | *metaclass* is the appropriate metaclass |
| 99 | *namespace* is the prepared class namespace |
| 100 | *kwds* is an updated copy of the passed in kwds argument with any |
| 101 | 'metaclass' entry removed. If no kwds argument is passed in, this will |
| 102 | be an empty dict. |
| 103 | """ |
| 104 | if kwds is None: |
| 105 | kwds = {} |
| 106 | else: |
| 107 | kwds = dict(kwds) # Don't alter the provided mapping |
| 108 | if 'metaclass' in kwds: |
| 109 | meta = kwds.pop('metaclass') |
| 110 | else: |
| 111 | if bases: |
| 112 | meta = type(bases[0]) |
| 113 | else: |
| 114 | meta = type |
| 115 | if isinstance(meta, type): |
| 116 | # when meta is a type, we first determine the most-derived metaclass |
| 117 | # instead of invoking the initial candidate directly |
| 118 | meta = _calculate_meta(meta, bases) |
| 119 | if hasattr(meta, '__prepare__'): |
| 120 | ns = meta.__prepare__(name, bases, **kwds) |
| 121 | else: |
Eric Snow | 4f29e75 | 2016-09-08 15:11:11 -0700 | [diff] [blame] | 122 | ns = {} |
Nick Coghlan | 7fc570a | 2012-05-20 02:34:13 +1000 | [diff] [blame] | 123 | return meta, ns, kwds |
| 124 | |
| 125 | def _calculate_meta(meta, bases): |
| 126 | """Calculate the most derived metaclass.""" |
| 127 | winner = meta |
| 128 | for base in bases: |
| 129 | base_meta = type(base) |
| 130 | if issubclass(winner, base_meta): |
| 131 | continue |
| 132 | if issubclass(base_meta, winner): |
| 133 | winner = base_meta |
| 134 | continue |
| 135 | # else: |
| 136 | raise TypeError("metaclass conflict: " |
| 137 | "the metaclass of a derived class " |
| 138 | "must be a (non-strict) subclass " |
| 139 | "of the metaclasses of all its bases") |
| 140 | return winner |
Ethan Furman | e03ea37 | 2013-09-25 07:14:41 -0700 | [diff] [blame] | 141 | |
| 142 | class DynamicClassAttribute: |
| 143 | """Route attribute access on a class to __getattr__. |
| 144 | |
| 145 | This is a descriptor, used to define attributes that act differently when |
| 146 | accessed through an instance and through a class. Instance access remains |
| 147 | normal, but access to an attribute through a class will be routed to the |
| 148 | class's __getattr__ method; this is done by raising AttributeError. |
| 149 | |
| 150 | This allows one to have properties active on an instance, and have virtual |
| 151 | attributes on the class with the same name (see Enum for an example). |
| 152 | |
| 153 | """ |
| 154 | def __init__(self, fget=None, fset=None, fdel=None, doc=None): |
| 155 | self.fget = fget |
| 156 | self.fset = fset |
| 157 | self.fdel = fdel |
| 158 | # next two lines make DynamicClassAttribute act the same as property |
Ethan Furman | 63c141c | 2013-10-18 00:27:39 -0700 | [diff] [blame] | 159 | self.__doc__ = doc or fget.__doc__ |
Ethan Furman | e03ea37 | 2013-09-25 07:14:41 -0700 | [diff] [blame] | 160 | self.overwrite_doc = doc is None |
| 161 | # support for abstract methods |
| 162 | self.__isabstractmethod__ = bool(getattr(fget, '__isabstractmethod__', False)) |
| 163 | |
| 164 | def __get__(self, instance, ownerclass=None): |
| 165 | if instance is None: |
| 166 | if self.__isabstractmethod__: |
| 167 | return self |
| 168 | raise AttributeError() |
| 169 | elif self.fget is None: |
| 170 | raise AttributeError("unreadable attribute") |
| 171 | return self.fget(instance) |
| 172 | |
| 173 | def __set__(self, instance, value): |
| 174 | if self.fset is None: |
| 175 | raise AttributeError("can't set attribute") |
| 176 | self.fset(instance, value) |
| 177 | |
| 178 | def __delete__(self, instance): |
| 179 | if self.fdel is None: |
| 180 | raise AttributeError("can't delete attribute") |
| 181 | self.fdel(instance) |
| 182 | |
| 183 | def getter(self, fget): |
| 184 | fdoc = fget.__doc__ if self.overwrite_doc else None |
| 185 | result = type(self)(fget, self.fset, self.fdel, fdoc or self.__doc__) |
| 186 | result.overwrite_doc = self.overwrite_doc |
| 187 | return result |
| 188 | |
| 189 | def setter(self, fset): |
| 190 | result = type(self)(self.fget, fset, self.fdel, self.__doc__) |
| 191 | result.overwrite_doc = self.overwrite_doc |
| 192 | return result |
| 193 | |
| 194 | def deleter(self, fdel): |
| 195 | result = type(self)(self.fget, self.fset, fdel, self.__doc__) |
| 196 | result.overwrite_doc = self.overwrite_doc |
| 197 | return result |
Serhiy Storchaka | 7beb4f9 | 2015-03-04 09:43:27 +0200 | [diff] [blame] | 198 | |
| 199 | |
Yury Selivanov | 00e3372 | 2015-06-24 11:44:51 -0400 | [diff] [blame] | 200 | class _GeneratorWrapper: |
| 201 | # TODO: Implement this in C. |
| 202 | def __init__(self, gen): |
Yury Selivanov | f847f1f | 2015-06-24 12:49:28 -0400 | [diff] [blame] | 203 | self.__wrapped = gen |
| 204 | self.__isgen = gen.__class__ is GeneratorType |
Yury Selivanov | 00e3372 | 2015-06-24 11:44:51 -0400 | [diff] [blame] | 205 | self.__name__ = getattr(gen, '__name__', None) |
| 206 | self.__qualname__ = getattr(gen, '__qualname__', None) |
| 207 | def send(self, val): |
Yury Selivanov | f847f1f | 2015-06-24 12:49:28 -0400 | [diff] [blame] | 208 | return self.__wrapped.send(val) |
Yury Selivanov | 00e3372 | 2015-06-24 11:44:51 -0400 | [diff] [blame] | 209 | def throw(self, tp, *rest): |
Yury Selivanov | f847f1f | 2015-06-24 12:49:28 -0400 | [diff] [blame] | 210 | return self.__wrapped.throw(tp, *rest) |
Yury Selivanov | 00e3372 | 2015-06-24 11:44:51 -0400 | [diff] [blame] | 211 | def close(self): |
Yury Selivanov | f847f1f | 2015-06-24 12:49:28 -0400 | [diff] [blame] | 212 | return self.__wrapped.close() |
Yury Selivanov | 00e3372 | 2015-06-24 11:44:51 -0400 | [diff] [blame] | 213 | @property |
| 214 | def gi_code(self): |
Yury Selivanov | f847f1f | 2015-06-24 12:49:28 -0400 | [diff] [blame] | 215 | return self.__wrapped.gi_code |
Yury Selivanov | 00e3372 | 2015-06-24 11:44:51 -0400 | [diff] [blame] | 216 | @property |
| 217 | def gi_frame(self): |
Yury Selivanov | f847f1f | 2015-06-24 12:49:28 -0400 | [diff] [blame] | 218 | return self.__wrapped.gi_frame |
Yury Selivanov | 00e3372 | 2015-06-24 11:44:51 -0400 | [diff] [blame] | 219 | @property |
| 220 | def gi_running(self): |
Yury Selivanov | f847f1f | 2015-06-24 12:49:28 -0400 | [diff] [blame] | 221 | return self.__wrapped.gi_running |
Yury Selivanov | 53e6230 | 2015-07-03 00:35:02 -0400 | [diff] [blame] | 222 | @property |
| 223 | def gi_yieldfrom(self): |
| 224 | return self.__wrapped.gi_yieldfrom |
Yury Selivanov | 00e3372 | 2015-06-24 11:44:51 -0400 | [diff] [blame] | 225 | cr_code = gi_code |
| 226 | cr_frame = gi_frame |
| 227 | cr_running = gi_running |
Yury Selivanov | 53e6230 | 2015-07-03 00:35:02 -0400 | [diff] [blame] | 228 | cr_await = gi_yieldfrom |
Yury Selivanov | 00e3372 | 2015-06-24 11:44:51 -0400 | [diff] [blame] | 229 | def __next__(self): |
Yury Selivanov | f847f1f | 2015-06-24 12:49:28 -0400 | [diff] [blame] | 230 | return next(self.__wrapped) |
Yury Selivanov | 00e3372 | 2015-06-24 11:44:51 -0400 | [diff] [blame] | 231 | def __iter__(self): |
Yury Selivanov | f847f1f | 2015-06-24 12:49:28 -0400 | [diff] [blame] | 232 | if self.__isgen: |
| 233 | return self.__wrapped |
Yury Selivanov | 00e3372 | 2015-06-24 11:44:51 -0400 | [diff] [blame] | 234 | return self |
| 235 | __await__ = __iter__ |
| 236 | |
Yury Selivanov | c565cd5 | 2015-05-29 09:06:05 -0400 | [diff] [blame] | 237 | def coroutine(func): |
| 238 | """Convert regular generator function to a coroutine.""" |
| 239 | |
Yury Selivanov | c565cd5 | 2015-05-29 09:06:05 -0400 | [diff] [blame] | 240 | if not callable(func): |
| 241 | raise TypeError('types.coroutine() expects a callable') |
| 242 | |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 243 | if (func.__class__ is FunctionType and |
| 244 | getattr(func, '__code__', None).__class__ is CodeType): |
Yury Selivanov | c565cd5 | 2015-05-29 09:06:05 -0400 | [diff] [blame] | 245 | |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 246 | co_flags = func.__code__.co_flags |
| 247 | |
| 248 | # Check if 'func' is a coroutine function. |
| 249 | # (0x180 == CO_COROUTINE | CO_ITERABLE_COROUTINE) |
| 250 | if co_flags & 0x180: |
| 251 | return func |
| 252 | |
| 253 | # Check if 'func' is a generator function. |
| 254 | # (0x20 == CO_GENERATOR) |
| 255 | if co_flags & 0x20: |
| 256 | # TODO: Implement this in C. |
| 257 | co = func.__code__ |
| 258 | func.__code__ = CodeType( |
| 259 | co.co_argcount, co.co_kwonlyargcount, co.co_nlocals, |
| 260 | co.co_stacksize, |
| 261 | co.co_flags | 0x100, # 0x100 == CO_ITERABLE_COROUTINE |
| 262 | co.co_code, |
| 263 | co.co_consts, co.co_names, co.co_varnames, co.co_filename, |
| 264 | co.co_name, co.co_firstlineno, co.co_lnotab, co.co_freevars, |
| 265 | co.co_cellvars) |
| 266 | return func |
Yury Selivanov | c565cd5 | 2015-05-29 09:06:05 -0400 | [diff] [blame] | 267 | |
Yury Selivanov | 13f7723 | 2015-05-29 16:19:18 -0400 | [diff] [blame] | 268 | # The following code is primarily to support functions that |
| 269 | # return generator-like objects (for instance generators |
| 270 | # compiled with Cython). |
| 271 | |
Serhiy Storchaka | 8110837 | 2017-09-26 00:55:55 +0300 | [diff] [blame] | 272 | # Delay functools and _collections_abc import for speeding up types import. |
| 273 | import functools |
| 274 | import _collections_abc |
| 275 | @functools.wraps(func) |
Yury Selivanov | c565cd5 | 2015-05-29 09:06:05 -0400 | [diff] [blame] | 276 | def wrapped(*args, **kwargs): |
| 277 | coro = func(*args, **kwargs) |
Yury Selivanov | fdbeb2b | 2015-07-03 13:11:35 -0400 | [diff] [blame] | 278 | if (coro.__class__ is CoroutineType or |
| 279 | coro.__class__ is GeneratorType and coro.gi_code.co_flags & 0x100): |
| 280 | # 'coro' is a native coroutine object or an iterable coroutine |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 281 | return coro |
Yury Selivanov | fdbeb2b | 2015-07-03 13:11:35 -0400 | [diff] [blame] | 282 | if (isinstance(coro, _collections_abc.Generator) and |
| 283 | not isinstance(coro, _collections_abc.Coroutine)): |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 284 | # 'coro' is either a pure Python generator iterator, or it |
| 285 | # implements collections.abc.Generator (and does not implement |
| 286 | # collections.abc.Coroutine). |
Yury Selivanov | 00e3372 | 2015-06-24 11:44:51 -0400 | [diff] [blame] | 287 | return _GeneratorWrapper(coro) |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 288 | # 'coro' is either an instance of collections.abc.Coroutine or |
| 289 | # some other object -- pass it through. |
Yury Selivanov | c565cd5 | 2015-05-29 09:06:05 -0400 | [diff] [blame] | 290 | return coro |
| 291 | |
| 292 | return wrapped |
| 293 | |
| 294 | |
Serhiy Storchaka | 7beb4f9 | 2015-03-04 09:43:27 +0200 | [diff] [blame] | 295 | __all__ = [n for n in globals() if n[:1] != '_'] |