PEP 0492 -- Coroutines with async and await syntax. Issue #24017.

This commit is contained in:
Yury Selivanov 2015-05-11 22:57:16 -04:00
parent 4e6bf4b3da
commit 7544508f02
72 changed files with 9261 additions and 5739 deletions

View file

@ -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."""