mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
PEP 0492 -- Coroutines with async and await syntax. Issue #24017.
This commit is contained in:
parent
4e6bf4b3da
commit
7544508f02
72 changed files with 9261 additions and 5739 deletions
24
Lib/types.py
24
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."""
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue