PEP 0492 -- Coroutines with async and await syntax. Issue #24017.
diff --git a/Include/Python-ast.h b/Include/Python-ast.h
index a938aad..3bc015f 100644
--- a/Include/Python-ast.h
+++ b/Include/Python-ast.h
@@ -63,12 +63,13 @@
     } v;
 };
 
-enum _stmt_kind {FunctionDef_kind=1, ClassDef_kind=2, Return_kind=3,
-                  Delete_kind=4, Assign_kind=5, AugAssign_kind=6, For_kind=7,
-                  While_kind=8, If_kind=9, With_kind=10, Raise_kind=11,
-                  Try_kind=12, Assert_kind=13, Import_kind=14,
-                  ImportFrom_kind=15, Global_kind=16, Nonlocal_kind=17,
-                  Expr_kind=18, Pass_kind=19, Break_kind=20, Continue_kind=21};
+enum _stmt_kind {FunctionDef_kind=1, AsyncFunctionDef_kind=2, ClassDef_kind=3,
+                  Return_kind=4, Delete_kind=5, Assign_kind=6,
+                  AugAssign_kind=7, For_kind=8, AsyncFor_kind=9, While_kind=10,
+                  If_kind=11, With_kind=12, AsyncWith_kind=13, Raise_kind=14,
+                  Try_kind=15, Assert_kind=16, Import_kind=17,
+                  ImportFrom_kind=18, Global_kind=19, Nonlocal_kind=20,
+                  Expr_kind=21, Pass_kind=22, Break_kind=23, Continue_kind=24};
 struct _stmt {
     enum _stmt_kind kind;
     union {
@@ -82,6 +83,14 @@
         
         struct {
             identifier name;
+            arguments_ty args;
+            asdl_seq *body;
+            asdl_seq *decorator_list;
+            expr_ty returns;
+        } AsyncFunctionDef;
+        
+        struct {
+            identifier name;
             asdl_seq *bases;
             asdl_seq *keywords;
             asdl_seq *body;
@@ -115,6 +124,13 @@
         } For;
         
         struct {
+            expr_ty target;
+            expr_ty iter;
+            asdl_seq *body;
+            asdl_seq *orelse;
+        } AsyncFor;
+        
+        struct {
             expr_ty test;
             asdl_seq *body;
             asdl_seq *orelse;
@@ -132,6 +148,11 @@
         } With;
         
         struct {
+            asdl_seq *items;
+            asdl_seq *body;
+        } AsyncWith;
+        
+        struct {
             expr_ty exc;
             expr_ty cause;
         } Raise;
@@ -178,11 +199,11 @@
 enum _expr_kind {BoolOp_kind=1, BinOp_kind=2, UnaryOp_kind=3, Lambda_kind=4,
                   IfExp_kind=5, Dict_kind=6, Set_kind=7, ListComp_kind=8,
                   SetComp_kind=9, DictComp_kind=10, GeneratorExp_kind=11,
-                  Yield_kind=12, YieldFrom_kind=13, Compare_kind=14,
-                  Call_kind=15, Num_kind=16, Str_kind=17, Bytes_kind=18,
-                  NameConstant_kind=19, Ellipsis_kind=20, Attribute_kind=21,
-                  Subscript_kind=22, Starred_kind=23, Name_kind=24,
-                  List_kind=25, Tuple_kind=26};
+                  Await_kind=12, Yield_kind=13, YieldFrom_kind=14,
+                  Compare_kind=15, Call_kind=16, Num_kind=17, Str_kind=18,
+                  Bytes_kind=19, NameConstant_kind=20, Ellipsis_kind=21,
+                  Attribute_kind=22, Subscript_kind=23, Starred_kind=24,
+                  Name_kind=25, List_kind=26, Tuple_kind=27};
 struct _expr {
     enum _expr_kind kind;
     union {
@@ -245,6 +266,10 @@
         
         struct {
             expr_ty value;
+        } Await;
+        
+        struct {
+            expr_ty value;
         } Yield;
         
         struct {
@@ -402,6 +427,10 @@
 stmt_ty _Py_FunctionDef(identifier name, arguments_ty args, asdl_seq * body,
                         asdl_seq * decorator_list, expr_ty returns, int lineno,
                         int col_offset, PyArena *arena);
+#define AsyncFunctionDef(a0, a1, a2, a3, a4, a5, a6, a7) _Py_AsyncFunctionDef(a0, a1, a2, a3, a4, a5, a6, a7)
+stmt_ty _Py_AsyncFunctionDef(identifier name, arguments_ty args, asdl_seq *
+                             body, asdl_seq * decorator_list, expr_ty returns,
+                             int lineno, int col_offset, PyArena *arena);
 #define ClassDef(a0, a1, a2, a3, a4, a5, a6, a7) _Py_ClassDef(a0, a1, a2, a3, a4, a5, a6, a7)
 stmt_ty _Py_ClassDef(identifier name, asdl_seq * bases, asdl_seq * keywords,
                      asdl_seq * body, asdl_seq * decorator_list, int lineno,
@@ -420,6 +449,9 @@
 #define For(a0, a1, a2, a3, a4, a5, a6) _Py_For(a0, a1, a2, a3, a4, a5, a6)
 stmt_ty _Py_For(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq *
                 orelse, int lineno, int col_offset, PyArena *arena);
+#define AsyncFor(a0, a1, a2, a3, a4, a5, a6) _Py_AsyncFor(a0, a1, a2, a3, a4, a5, a6)
+stmt_ty _Py_AsyncFor(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq *
+                     orelse, int lineno, int col_offset, PyArena *arena);
 #define While(a0, a1, a2, a3, a4, a5) _Py_While(a0, a1, a2, a3, a4, a5)
 stmt_ty _Py_While(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno,
                   int col_offset, PyArena *arena);
@@ -429,6 +461,9 @@
 #define With(a0, a1, a2, a3, a4) _Py_With(a0, a1, a2, a3, a4)
 stmt_ty _Py_With(asdl_seq * items, asdl_seq * body, int lineno, int col_offset,
                  PyArena *arena);
+#define AsyncWith(a0, a1, a2, a3, a4) _Py_AsyncWith(a0, a1, a2, a3, a4)
+stmt_ty _Py_AsyncWith(asdl_seq * items, asdl_seq * body, int lineno, int
+                      col_offset, PyArena *arena);
 #define Raise(a0, a1, a2, a3, a4) _Py_Raise(a0, a1, a2, a3, a4)
 stmt_ty _Py_Raise(expr_ty exc, expr_ty cause, int lineno, int col_offset,
                   PyArena *arena);
@@ -491,6 +526,8 @@
 #define GeneratorExp(a0, a1, a2, a3, a4) _Py_GeneratorExp(a0, a1, a2, a3, a4)
 expr_ty _Py_GeneratorExp(expr_ty elt, asdl_seq * generators, int lineno, int
                          col_offset, PyArena *arena);
+#define Await(a0, a1, a2, a3) _Py_Await(a0, a1, a2, a3)
+expr_ty _Py_Await(expr_ty value, int lineno, int col_offset, PyArena *arena);
 #define Yield(a0, a1, a2, a3) _Py_Yield(a0, a1, a2, a3)
 expr_ty _Py_Yield(expr_ty value, int lineno, int col_offset, PyArena *arena);
 #define YieldFrom(a0, a1, a2, a3) _Py_YieldFrom(a0, a1, a2, a3)
diff --git a/Include/ceval.h b/Include/ceval.h
index 6811367..d3292b8 100644
--- a/Include/ceval.h
+++ b/Include/ceval.h
@@ -23,6 +23,8 @@
 #ifndef Py_LIMITED_API
 PyAPI_FUNC(void) PyEval_SetProfile(Py_tracefunc, PyObject *);
 PyAPI_FUNC(void) PyEval_SetTrace(Py_tracefunc, PyObject *);
+PyAPI_FUNC(void) PyEval_SetCoroutineWrapper(PyObject *wrapper);
+PyAPI_FUNC(PyObject *) PyEval_GetCoroutineWrapper();
 #endif
 
 struct _frame; /* Avoid including frameobject.h */
diff --git a/Include/code.h b/Include/code.h
index 131de50..56e6ec1 100644
--- a/Include/code.h
+++ b/Include/code.h
@@ -51,6 +51,11 @@
 */
 #define CO_NOFREE       0x0040
 
+/* The CO_COROUTINE flag is set for coroutine functions (defined with
+   ``async def`` keywords) */
+#define CO_COROUTINE            0x0080
+#define CO_ITERABLE_COROUTINE   0x0100
+
 /* These are no longer used. */
 #if 0
 #define CO_GENERATOR_ALLOWED    0x1000
diff --git a/Include/genobject.h b/Include/genobject.h
index 23571e6..ee0b130 100644
--- a/Include/genobject.h
+++ b/Include/genobject.h
@@ -38,6 +38,12 @@
 #define PyGen_Check(op) PyObject_TypeCheck(op, &PyGen_Type)
 #define PyGen_CheckExact(op) (Py_TYPE(op) == &PyGen_Type)
 
+#define PyGen_CheckCoroutineExact(op) (PyGen_CheckExact(op) && \
+                                       (((PyCodeObject*) \
+                                           ((PyGenObject*)op)->gi_code) \
+                                         ->co_flags & (CO_ITERABLE_COROUTINE | \
+                                                       CO_COROUTINE)))
+
 PyAPI_FUNC(PyObject *) PyGen_New(struct _frame *);
 PyAPI_FUNC(PyObject *) PyGen_NewWithQualName(struct _frame *,
     PyObject *name, PyObject *qualname);
@@ -46,6 +52,7 @@
 PyObject *_PyGen_Send(PyGenObject *, PyObject *);
 PyAPI_FUNC(void) _PyGen_Finalize(PyObject *self);
 
+PyObject *_PyGen_GetAwaitableIter(PyObject *o);
 
 #ifdef __cplusplus
 }
diff --git a/Include/graminit.h b/Include/graminit.h
index 3ec949a..d030bc3 100644
--- a/Include/graminit.h
+++ b/Include/graminit.h
@@ -6,79 +6,82 @@
 #define decorator 259
 #define decorators 260
 #define decorated 261
-#define funcdef 262
-#define parameters 263
-#define typedargslist 264
-#define tfpdef 265
-#define varargslist 266
-#define vfpdef 267
-#define stmt 268
-#define simple_stmt 269
-#define small_stmt 270
-#define expr_stmt 271
-#define testlist_star_expr 272
-#define augassign 273
-#define del_stmt 274
-#define pass_stmt 275
-#define flow_stmt 276
-#define break_stmt 277
-#define continue_stmt 278
-#define return_stmt 279
-#define yield_stmt 280
-#define raise_stmt 281
-#define import_stmt 282
-#define import_name 283
-#define import_from 284
-#define import_as_name 285
-#define dotted_as_name 286
-#define import_as_names 287
-#define dotted_as_names 288
-#define dotted_name 289
-#define global_stmt 290
-#define nonlocal_stmt 291
-#define assert_stmt 292
-#define compound_stmt 293
-#define if_stmt 294
-#define while_stmt 295
-#define for_stmt 296
-#define try_stmt 297
-#define with_stmt 298
-#define with_item 299
-#define except_clause 300
-#define suite 301
-#define test 302
-#define test_nocond 303
-#define lambdef 304
-#define lambdef_nocond 305
-#define or_test 306
-#define and_test 307
-#define not_test 308
-#define comparison 309
-#define comp_op 310
-#define star_expr 311
-#define expr 312
-#define xor_expr 313
-#define and_expr 314
-#define shift_expr 315
-#define arith_expr 316
-#define term 317
-#define factor 318
-#define power 319
-#define atom 320
-#define testlist_comp 321
-#define trailer 322
-#define subscriptlist 323
-#define subscript 324
-#define sliceop 325
-#define exprlist 326
-#define testlist 327
-#define dictorsetmaker 328
-#define classdef 329
-#define arglist 330
-#define argument 331
-#define comp_iter 332
-#define comp_for 333
-#define comp_if 334
-#define encoding_decl 335
-#define yield_expr 336
-#define yield_arg 337
+#define async_funcdef 262
+#define funcdef 263
+#define parameters 264
+#define typedargslist 265
+#define tfpdef 266
+#define varargslist 267
+#define vfpdef 268
+#define stmt 269
+#define simple_stmt 270
+#define small_stmt 271
+#define expr_stmt 272
+#define testlist_star_expr 273
+#define augassign 274
+#define del_stmt 275
+#define pass_stmt 276
+#define flow_stmt 277
+#define break_stmt 278
+#define continue_stmt 279
+#define return_stmt 280
+#define yield_stmt 281
+#define raise_stmt 282
+#define import_stmt 283
+#define import_name 284
+#define import_from 285
+#define import_as_name 286
+#define dotted_as_name 287
+#define import_as_names 288
+#define dotted_as_names 289
+#define dotted_name 290
+#define global_stmt 291
+#define nonlocal_stmt 292
+#define assert_stmt 293
+#define compound_stmt 294
+#define async_stmt 295
+#define if_stmt 296
+#define while_stmt 297
+#define for_stmt 298
+#define try_stmt 299
+#define with_stmt 300
+#define with_item 301
+#define except_clause 302
+#define suite 303
+#define test 304
+#define test_nocond 305
+#define lambdef 306
+#define lambdef_nocond 307
+#define or_test 308
+#define and_test 309
+#define not_test 310
+#define comparison 311
+#define comp_op 312
+#define star_expr 313
+#define expr 314
+#define xor_expr 315
+#define and_expr 316
+#define shift_expr 317
+#define arith_expr 318
+#define term 319
+#define factor 320
+#define power 321
+#define atom_expr 322
+#define atom 323
+#define testlist_comp 324
+#define trailer 325
+#define subscriptlist 326
+#define subscript 327
+#define sliceop 328
+#define exprlist 329
+#define testlist 330
+#define dictorsetmaker 331
+#define classdef 332
+#define arglist 333
+#define argument 334
+#define comp_iter 335
+#define comp_for 336
+#define comp_if 337
+#define encoding_decl 338
+#define yield_expr 339
+#define yield_arg 340
diff --git a/Include/object.h b/Include/object.h
index 5716f66..e173438 100644
--- a/Include/object.h
+++ b/Include/object.h
@@ -173,6 +173,9 @@
 typedef int(*ssizeobjargproc)(PyObject *, Py_ssize_t, PyObject *);
 typedef int(*ssizessizeobjargproc)(PyObject *, Py_ssize_t, Py_ssize_t, PyObject *);
 typedef int(*objobjargproc)(PyObject *, PyObject *, PyObject *);
+typedef PyObject *(*getawaitablefunc) (PyObject *);
+typedef PyObject *(*getaiterfunc) (PyObject *);
+typedef PyObject *(*aiternextfunc) (PyObject *);
 
 #ifndef Py_LIMITED_API
 /* buffer interface */
@@ -301,6 +304,11 @@
     objobjargproc mp_ass_subscript;
 } PyMappingMethods;
 
+typedef struct {
+    getawaitablefunc am_await;
+    getaiterfunc am_aiter;
+    aiternextfunc am_anext;
+} PyAsyncMethods;
 
 typedef struct {
      getbufferproc bf_getbuffer;
@@ -346,7 +354,7 @@
     printfunc tp_print;
     getattrfunc tp_getattr;
     setattrfunc tp_setattr;
-    void *tp_reserved; /* formerly known as tp_compare */
+    PyAsyncMethods *tp_as_async; /* formerly known as tp_compare or tp_reserved */
     reprfunc tp_repr;
 
     /* Method suites for standard classes */
@@ -453,6 +461,7 @@
     /* Note: there's a dependency on the order of these members
        in slotptr() in typeobject.c . */
     PyTypeObject ht_type;
+    PyAsyncMethods as_async;
     PyNumberMethods as_number;
     PyMappingMethods as_mapping;
     PySequenceMethods as_sequence; /* as_sequence comes after as_mapping,
diff --git a/Include/opcode.h b/Include/opcode.h
index 510dfe8..e2a3ba9 100644
--- a/Include/opcode.h
+++ b/Include/opcode.h
@@ -7,68 +7,73 @@
 
 
     /* Instruction opcodes for compiled code */
-#define POP_TOP             	1  
-#define ROT_TWO             	2  
-#define ROT_THREE           	3  
-#define DUP_TOP             	4  
-#define DUP_TOP_TWO         	5  
-#define NOP                 	9  
-#define UNARY_POSITIVE      	10 
-#define UNARY_NEGATIVE      	11 
-#define UNARY_NOT           	12 
-#define UNARY_INVERT        	15 
-#define BINARY_MATRIX_MULTIPLY	16 
-#define INPLACE_MATRIX_MULTIPLY	17 
-#define BINARY_POWER        	19 
-#define BINARY_MULTIPLY     	20 
-#define BINARY_MODULO       	22 
-#define BINARY_ADD          	23 
-#define BINARY_SUBTRACT     	24 
-#define BINARY_SUBSCR       	25 
-#define BINARY_FLOOR_DIVIDE 	26 
-#define BINARY_TRUE_DIVIDE  	27 
-#define INPLACE_FLOOR_DIVIDE	28 
-#define INPLACE_TRUE_DIVIDE 	29 
-#define STORE_MAP           	54 
-#define INPLACE_ADD         	55 
-#define INPLACE_SUBTRACT    	56 
-#define INPLACE_MULTIPLY    	57 
-#define INPLACE_MODULO      	59 
-#define STORE_SUBSCR        	60 
-#define DELETE_SUBSCR       	61 
-#define BINARY_LSHIFT       	62 
-#define BINARY_RSHIFT       	63 
-#define BINARY_AND          	64 
-#define BINARY_XOR          	65 
-#define BINARY_OR           	66 
-#define INPLACE_POWER       	67 
-#define GET_ITER            	68 
-#define PRINT_EXPR          	70 
-#define LOAD_BUILD_CLASS    	71 
-#define YIELD_FROM          	72 
-#define INPLACE_LSHIFT      	75 
-#define INPLACE_RSHIFT      	76 
-#define INPLACE_AND         	77 
-#define INPLACE_XOR         	78 
-#define INPLACE_OR          	79 
-#define BREAK_LOOP          	80 
-#define WITH_CLEANUP        	81 
-#define RETURN_VALUE        	83 
-#define IMPORT_STAR         	84 
-#define YIELD_VALUE         	86 
-#define POP_BLOCK           	87 
-#define END_FINALLY         	88 
-#define POP_EXCEPT          	89 
-#define HAVE_ARGUMENT       	90 
-#define STORE_NAME          	90 
-#define DELETE_NAME         	91 
-#define UNPACK_SEQUENCE     	92 
-#define FOR_ITER            	93 
-#define UNPACK_EX           	94 
-#define STORE_ATTR          	95 
-#define DELETE_ATTR         	96 
-#define STORE_GLOBAL        	97 
-#define DELETE_GLOBAL       	98 
+#define POP_TOP             	1
+#define ROT_TWO             	2
+#define ROT_THREE           	3
+#define DUP_TOP             	4
+#define DUP_TOP_TWO         	5
+#define NOP                 	9
+#define UNARY_POSITIVE      	10
+#define UNARY_NEGATIVE      	11
+#define UNARY_NOT           	12
+#define UNARY_INVERT        	15
+#define BINARY_MATRIX_MULTIPLY	16
+#define INPLACE_MATRIX_MULTIPLY	17
+#define BINARY_POWER        	19
+#define BINARY_MULTIPLY     	20
+#define BINARY_MODULO       	22
+#define BINARY_ADD          	23
+#define BINARY_SUBTRACT     	24
+#define BINARY_SUBSCR       	25
+#define BINARY_FLOOR_DIVIDE 	26
+#define BINARY_TRUE_DIVIDE  	27
+#define INPLACE_FLOOR_DIVIDE	28
+#define INPLACE_TRUE_DIVIDE 	29
+#define GET_AITER           	50
+#define GET_ANEXT           	51
+#define BEFORE_ASYNC_WITH   	52
+#define STORE_MAP           	54
+#define INPLACE_ADD         	55
+#define INPLACE_SUBTRACT    	56
+#define INPLACE_MULTIPLY    	57
+#define INPLACE_MODULO      	59
+#define STORE_SUBSCR        	60
+#define DELETE_SUBSCR       	61
+#define BINARY_LSHIFT       	62
+#define BINARY_RSHIFT       	63
+#define BINARY_AND          	64
+#define BINARY_XOR          	65
+#define BINARY_OR           	66
+#define INPLACE_POWER       	67
+#define GET_ITER            	68
+#define PRINT_EXPR          	70
+#define LOAD_BUILD_CLASS    	71
+#define YIELD_FROM          	72
+#define GET_AWAITABLE       	73
+#define INPLACE_LSHIFT      	75
+#define INPLACE_RSHIFT      	76
+#define INPLACE_AND         	77
+#define INPLACE_XOR         	78
+#define INPLACE_OR          	79
+#define BREAK_LOOP          	80
+#define WITH_CLEANUP_START  	81
+#define WITH_CLEANUP_FINISH   	82
+#define RETURN_VALUE        	83
+#define IMPORT_STAR         	84
+#define YIELD_VALUE         	86
+#define POP_BLOCK           	87
+#define END_FINALLY         	88
+#define POP_EXCEPT          	89
+#define HAVE_ARGUMENT       	90
+#define STORE_NAME          	90
+#define DELETE_NAME         	91
+#define UNPACK_SEQUENCE     	92
+#define FOR_ITER            	93
+#define UNPACK_EX           	94
+#define STORE_ATTR          	95
+#define DELETE_ATTR         	96
+#define STORE_GLOBAL        	97
+#define DELETE_GLOBAL       	98
 #define LOAD_CONST          	100
 #define LOAD_NAME           	101
 #define BUILD_TUPLE         	102
@@ -116,6 +121,7 @@
 #define BUILD_MAP_UNPACK_WITH_CALL	151
 #define BUILD_TUPLE_UNPACK  	152
 #define BUILD_SET_UNPACK    	153
+#define SETUP_ASYNC_WITH    	154
 
 /* EXCEPT_HANDLER is a special, implicit block type which is created when
    entering an except handler. It is not an opcode but we define it here
diff --git a/Include/pyerrors.h b/Include/pyerrors.h
index 8a7e4f4..a019865 100644
--- a/Include/pyerrors.h
+++ b/Include/pyerrors.h
@@ -147,6 +147,7 @@
 
 PyAPI_DATA(PyObject *) PyExc_BaseException;
 PyAPI_DATA(PyObject *) PyExc_Exception;
+PyAPI_DATA(PyObject *) PyExc_StopAsyncIteration;
 PyAPI_DATA(PyObject *) PyExc_StopIteration;
 PyAPI_DATA(PyObject *) PyExc_GeneratorExit;
 PyAPI_DATA(PyObject *) PyExc_ArithmeticError;
diff --git a/Include/pystate.h b/Include/pystate.h
index 8539b65..2ee81df 100644
--- a/Include/pystate.h
+++ b/Include/pystate.h
@@ -134,6 +134,8 @@
     void (*on_delete)(void *);
     void *on_delete_data;
 
+    PyObject *coroutine_wrapper;
+
     /* XXX signal handlers should also be here */
 
 } PyThreadState;
diff --git a/Include/token.h b/Include/token.h
index 2b213ee..595afa0 100644
--- a/Include/token.h
+++ b/Include/token.h
@@ -64,8 +64,10 @@
 #define ELLIPSIS        52
 /* Don't forget to update the table _PyParser_TokenNames in tokenizer.c! */
 #define OP		53
-#define ERRORTOKEN	54
-#define N_TOKENS	55
+#define AWAIT		54
+#define ASYNC		55
+#define ERRORTOKEN	56
+#define N_TOKENS	57
 
 /* Special definitions for cooperation with parser */
 
diff --git a/Include/typeslots.h b/Include/typeslots.h
index da2e87c..f56749d 100644
--- a/Include/typeslots.h
+++ b/Include/typeslots.h
@@ -76,3 +76,6 @@
 #define Py_tp_free 74
 #define Py_nb_matrix_multiply 75
 #define Py_nb_inplace_matrix_multiply 76
+#define Py_am_await 77
+#define Py_am_aiter 78
+#define Py_am_anext 79