diff options
Diffstat (limited to 'Lib/types.py')
-rw-r--r-- | Lib/types.py | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/Lib/types.py b/Lib/types.py index 4fb2def..49e4d04 100644 --- a/Lib/types.py +++ b/Lib/types.py @@ -43,6 +43,30 @@ MemberDescriptorType = type(FunctionType.__globals__) del sys, _f, _g, _C, # Not for export +_CO_GENERATOR = 0x20 +_CO_ITERABLE_COROUTINE = 0x100 + +def coroutine(func): + """Convert regular generator function to a coroutine.""" + + # TODO: Implement this in C. + + if (not isinstance(func, (FunctionType, MethodType)) or + not isinstance(getattr(func, '__code__', None), CodeType) or + not (func.__code__.co_flags & _CO_GENERATOR)): + raise TypeError('coroutine() expects a generator function') + + co = func.__code__ + func.__code__ = CodeType( + co.co_argcount, co.co_kwonlyargcount, co.co_nlocals, co.co_stacksize, + co.co_flags | _CO_ITERABLE_COROUTINE, + co.co_code, + co.co_consts, co.co_names, co.co_varnames, co.co_filename, co.co_name, + co.co_firstlineno, co.co_lnotab, co.co_freevars, co.co_cellvars) + + return func + + # Provide a PEP 3115 compliant mechanism for class creation def new_class(name, bases=(), kwds=None, exec_body=None): """Create a class object dynamically using the appropriate metaclass.""" |