summaryrefslogtreecommitdiffstats
path: root/Lib/types.py
diff options
context:
space:
mode:
authorYury Selivanov <yselivanov@sprymix.com>2015-05-12 02:57:16 (GMT)
committerYury Selivanov <yselivanov@sprymix.com>2015-05-12 02:57:16 (GMT)
commit7544508f0245173bff5866aa1598c8f6cce1fc5f (patch)
treebf80850d9cd46fc811f04b8c2484fb50775c697d /Lib/types.py
parent4e6bf4b3da03b132b0698f30ee931a350585b117 (diff)
downloadcpython-7544508f0245173bff5866aa1598c8f6cce1fc5f.zip
cpython-7544508f0245173bff5866aa1598c8f6cce1fc5f.tar.gz
cpython-7544508f0245173bff5866aa1598c8f6cce1fc5f.tar.bz2
PEP 0492 -- Coroutines with async and await syntax. Issue #24017.
Diffstat (limited to 'Lib/types.py')
-rw-r--r--Lib/types.py24
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."""