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