Skip to content
This repository was archived by the owner on Feb 13, 2025. It is now read-only.

Commit ac32e1e

Browse files
author
Anselm Kruis
committed
Merge with default (PEP 492)
Unfortunately the outcome of this merge is not yet functional.
2 parents 5d88da3 + 7544508 commit ac32e1e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+9150
-5622
lines changed

Doc/includes/typestruct.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ typedef struct _typeobject {
99
printfunc tp_print;
1010
getattrfunc tp_getattr;
1111
setattrfunc tp_setattr;
12-
void *tp_reserved;
12+
PyAsyncMethods *tp_as_async;
1313
reprfunc tp_repr;
1414

1515
/* Method suites for standard classes */

Doc/library/dis.rst

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,38 @@ the original TOS1.
508508
Implements ``del TOS1[TOS]``.
509509

510510

511+
**Coroutines opcodes**
512+
513+
.. opcode:: GET_AWAITABLE
514+
515+
Implements ``TOS = get_awaitable(TOS)``; where ``get_awaitable(o)``
516+
returns ``o`` if ``o`` is a coroutine object; or resolved ``o.__await__``.
517+
518+
519+
.. opcode:: GET_AITER
520+
521+
Implements ``TOS = get_awaitable(TOS.__aiter__())``. See ``GET_AWAITABLE``
522+
for details about ``get_awaitable``
523+
524+
525+
.. opcode:: GET_ANEXT
526+
527+
Implements ``PUSH(get_awaitable(TOS.__anext__()))``. See ``GET_AWAITABLE``
528+
for details about ``get_awaitable``
529+
530+
531+
.. opcode:: BEFORE_ASYNC_WITH
532+
533+
Resolves ``__aenter__`` and ``__aexit__`` from the object on top of the
534+
stack. Pushes ``__aexit__`` and result of ``__aenter__()`` to the stack.
535+
536+
537+
.. opcode:: SETUP_ASYNC_WITH
538+
539+
Creates a new frame object.
540+
541+
542+
511543
**Miscellaneous opcodes**
512544

513545
.. opcode:: PRINT_EXPR
@@ -612,7 +644,7 @@ iterations of the loop.
612644
:opcode:`UNPACK_SEQUENCE`).
613645

614646

615-
.. opcode:: WITH_CLEANUP
647+
.. opcode:: WITH_CLEANUP_START
616648

617649
Cleans up the stack when a :keyword:`with` statement block exits. TOS is the
618650
context manager's :meth:`__exit__` bound method. Below TOS are 1--3 values
@@ -624,7 +656,13 @@ iterations of the loop.
624656
* (SECOND, THIRD, FOURTH) = exc_info()
625657

626658
In the last case, ``TOS(SECOND, THIRD, FOURTH)`` is called, otherwise
627-
``TOS(None, None, None)``. In addition, TOS is removed from the stack.
659+
``TOS(None, None, None)``. Pushes SECOND and result of the call
660+
to the stack.
661+
662+
663+
.. opcode:: WITH_CLEANUP_FINISH
664+
665+
Pops exception type and result of 'exit' function call from the stack.
628666

629667
If the stack represents an exception, *and* the function call returns a
630668
'true' value, this information is "zapped" and replaced with a single

Grammar/Grammar

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@ eval_input: testlist NEWLINE* ENDMARKER
2121

2222
decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
2323
decorators: decorator+
24-
decorated: decorators (classdef | funcdef)
24+
decorated: decorators (classdef | funcdef | async_funcdef)
25+
26+
async_funcdef: ASYNC funcdef
2527
funcdef: 'def' NAME parameters ['->' test] ':' suite
28+
2629
parameters: '(' [typedargslist] ')'
2730
typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [','
2831
['*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] | '**' tfpdef]]
@@ -65,7 +68,8 @@ global_stmt: 'global' NAME (',' NAME)*
6568
nonlocal_stmt: 'nonlocal' NAME (',' NAME)*
6669
assert_stmt: 'assert' test [',' test]
6770

68-
compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated
71+
compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated | async_stmt
72+
async_stmt: ASYNC (funcdef | with_stmt | for_stmt)
6973
if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
7074
while_stmt: 'while' test ':' suite ['else' ':' suite]
7175
for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]
@@ -99,7 +103,8 @@ shift_expr: arith_expr (('<<'|'>>') arith_expr)*
99103
arith_expr: term (('+'|'-') term)*
100104
term: factor (('*'|'@'|'/'|'%'|'//') factor)*
101105
factor: ('+'|'-'|'~') factor | power
102-
power: atom trailer* ['**' factor]
106+
power: atom_expr ['**' factor]
107+
atom_expr: [AWAIT] atom trailer*
103108
atom: ('(' [yield_expr|testlist_comp] ')' |
104109
'[' [testlist_comp] ']' |
105110
'{' [dictorsetmaker] '}' |

Include/Python-ast.h

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,13 @@ struct _mod {
6363
} v;
6464
};
6565

66-
enum _stmt_kind {FunctionDef_kind=1, ClassDef_kind=2, Return_kind=3,
67-
Delete_kind=4, Assign_kind=5, AugAssign_kind=6, For_kind=7,
68-
While_kind=8, If_kind=9, With_kind=10, Raise_kind=11,
69-
Try_kind=12, Assert_kind=13, Import_kind=14,
70-
ImportFrom_kind=15, Global_kind=16, Nonlocal_kind=17,
71-
Expr_kind=18, Pass_kind=19, Break_kind=20, Continue_kind=21};
66+
enum _stmt_kind {FunctionDef_kind=1, AsyncFunctionDef_kind=2, ClassDef_kind=3,
67+
Return_kind=4, Delete_kind=5, Assign_kind=6,
68+
AugAssign_kind=7, For_kind=8, AsyncFor_kind=9, While_kind=10,
69+
If_kind=11, With_kind=12, AsyncWith_kind=13, Raise_kind=14,
70+
Try_kind=15, Assert_kind=16, Import_kind=17,
71+
ImportFrom_kind=18, Global_kind=19, Nonlocal_kind=20,
72+
Expr_kind=21, Pass_kind=22, Break_kind=23, Continue_kind=24};
7273
struct _stmt {
7374
enum _stmt_kind kind;
7475
union {
@@ -80,6 +81,14 @@ struct _stmt {
8081
expr_ty returns;
8182
} FunctionDef;
8283

84+
struct {
85+
identifier name;
86+
arguments_ty args;
87+
asdl_seq *body;
88+
asdl_seq *decorator_list;
89+
expr_ty returns;
90+
} AsyncFunctionDef;
91+
8392
struct {
8493
identifier name;
8594
asdl_seq *bases;
@@ -114,6 +123,13 @@ struct _stmt {
114123
asdl_seq *orelse;
115124
} For;
116125

126+
struct {
127+
expr_ty target;
128+
expr_ty iter;
129+
asdl_seq *body;
130+
asdl_seq *orelse;
131+
} AsyncFor;
132+
117133
struct {
118134
expr_ty test;
119135
asdl_seq *body;
@@ -131,6 +147,11 @@ struct _stmt {
131147
asdl_seq *body;
132148
} With;
133149

150+
struct {
151+
asdl_seq *items;
152+
asdl_seq *body;
153+
} AsyncWith;
154+
134155
struct {
135156
expr_ty exc;
136157
expr_ty cause;
@@ -178,11 +199,11 @@ struct _stmt {
178199
enum _expr_kind {BoolOp_kind=1, BinOp_kind=2, UnaryOp_kind=3, Lambda_kind=4,
179200
IfExp_kind=5, Dict_kind=6, Set_kind=7, ListComp_kind=8,
180201
SetComp_kind=9, DictComp_kind=10, GeneratorExp_kind=11,
181-
Yield_kind=12, YieldFrom_kind=13, Compare_kind=14,
182-
Call_kind=15, Num_kind=16, Str_kind=17, Bytes_kind=18,
183-
NameConstant_kind=19, Ellipsis_kind=20, Attribute_kind=21,
184-
Subscript_kind=22, Starred_kind=23, Name_kind=24,
185-
List_kind=25, Tuple_kind=26};
202+
Await_kind=12, Yield_kind=13, YieldFrom_kind=14,
203+
Compare_kind=15, Call_kind=16, Num_kind=17, Str_kind=18,
204+
Bytes_kind=19, NameConstant_kind=20, Ellipsis_kind=21,
205+
Attribute_kind=22, Subscript_kind=23, Starred_kind=24,
206+
Name_kind=25, List_kind=26, Tuple_kind=27};
186207
struct _expr {
187208
enum _expr_kind kind;
188209
union {
@@ -243,6 +264,10 @@ struct _expr {
243264
asdl_seq *generators;
244265
} GeneratorExp;
245266

267+
struct {
268+
expr_ty value;
269+
} Await;
270+
246271
struct {
247272
expr_ty value;
248273
} Yield;
@@ -402,6 +427,10 @@ mod_ty _Py_Suite(asdl_seq * body, PyArena *arena);
402427
stmt_ty _Py_FunctionDef(identifier name, arguments_ty args, asdl_seq * body,
403428
asdl_seq * decorator_list, expr_ty returns, int lineno,
404429
int col_offset, PyArena *arena);
430+
#define AsyncFunctionDef(a0, a1, a2, a3, a4, a5, a6, a7) _Py_AsyncFunctionDef(a0, a1, a2, a3, a4, a5, a6, a7)
431+
stmt_ty _Py_AsyncFunctionDef(identifier name, arguments_ty args, asdl_seq *
432+
body, asdl_seq * decorator_list, expr_ty returns,
433+
int lineno, int col_offset, PyArena *arena);
405434
#define ClassDef(a0, a1, a2, a3, a4, a5, a6, a7) _Py_ClassDef(a0, a1, a2, a3, a4, a5, a6, a7)
406435
stmt_ty _Py_ClassDef(identifier name, asdl_seq * bases, asdl_seq * keywords,
407436
asdl_seq * body, asdl_seq * decorator_list, int lineno,
@@ -420,6 +449,9 @@ stmt_ty _Py_AugAssign(expr_ty target, operator_ty op, expr_ty value, int
420449
#define For(a0, a1, a2, a3, a4, a5, a6) _Py_For(a0, a1, a2, a3, a4, a5, a6)
421450
stmt_ty _Py_For(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq *
422451
orelse, int lineno, int col_offset, PyArena *arena);
452+
#define AsyncFor(a0, a1, a2, a3, a4, a5, a6) _Py_AsyncFor(a0, a1, a2, a3, a4, a5, a6)
453+
stmt_ty _Py_AsyncFor(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq *
454+
orelse, int lineno, int col_offset, PyArena *arena);
423455
#define While(a0, a1, a2, a3, a4, a5) _Py_While(a0, a1, a2, a3, a4, a5)
424456
stmt_ty _Py_While(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno,
425457
int col_offset, PyArena *arena);
@@ -429,6 +461,9 @@ stmt_ty _Py_If(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno,
429461
#define With(a0, a1, a2, a3, a4) _Py_With(a0, a1, a2, a3, a4)
430462
stmt_ty _Py_With(asdl_seq * items, asdl_seq * body, int lineno, int col_offset,
431463
PyArena *arena);
464+
#define AsyncWith(a0, a1, a2, a3, a4) _Py_AsyncWith(a0, a1, a2, a3, a4)
465+
stmt_ty _Py_AsyncWith(asdl_seq * items, asdl_seq * body, int lineno, int
466+
col_offset, PyArena *arena);
432467
#define Raise(a0, a1, a2, a3, a4) _Py_Raise(a0, a1, a2, a3, a4)
433468
stmt_ty _Py_Raise(expr_ty exc, expr_ty cause, int lineno, int col_offset,
434469
PyArena *arena);
@@ -491,6 +526,8 @@ expr_ty _Py_DictComp(expr_ty key, expr_ty value, asdl_seq * generators, int
491526
#define GeneratorExp(a0, a1, a2, a3, a4) _Py_GeneratorExp(a0, a1, a2, a3, a4)
492527
expr_ty _Py_GeneratorExp(expr_ty elt, asdl_seq * generators, int lineno, int
493528
col_offset, PyArena *arena);
529+
#define Await(a0, a1, a2, a3) _Py_Await(a0, a1, a2, a3)
530+
expr_ty _Py_Await(expr_ty value, int lineno, int col_offset, PyArena *arena);
494531
#define Yield(a0, a1, a2, a3) _Py_Yield(a0, a1, a2, a3)
495532
expr_ty _Py_Yield(expr_ty value, int lineno, int col_offset, PyArena *arena);
496533
#define YieldFrom(a0, a1, a2, a3) _Py_YieldFrom(a0, a1, a2, a3)

Include/ceval.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ PyAPI_FUNC(PyObject *) PyEval_CallMethod(PyObject *obj,
2323
#ifndef Py_LIMITED_API
2424
PyAPI_FUNC(void) PyEval_SetProfile(Py_tracefunc, PyObject *);
2525
PyAPI_FUNC(void) PyEval_SetTrace(Py_tracefunc, PyObject *);
26+
PyAPI_FUNC(void) PyEval_SetCoroutineWrapper(PyObject *wrapper);
27+
PyAPI_FUNC(PyObject *) PyEval_GetCoroutineWrapper();
2628
#endif
2729

2830
struct _frame; /* Avoid including frameobject.h */

Include/code.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ typedef struct {
5151
*/
5252
#define CO_NOFREE 0x0040
5353

54+
/* The CO_COROUTINE flag is set for coroutine functions (defined with
55+
``async def`` keywords) */
56+
#define CO_COROUTINE 0x0080
57+
#define CO_ITERABLE_COROUTINE 0x0100
58+
5459
/* These are no longer used. */
5560
#if 0
5661
#define CO_GENERATOR_ALLOWED 0x1000

Include/genobject.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ PyAPI_DATA(PyTypeObject) PyGen_Type;
3838
#define PyGen_Check(op) PyObject_TypeCheck(op, &PyGen_Type)
3939
#define PyGen_CheckExact(op) (Py_TYPE(op) == &PyGen_Type)
4040

41+
#define PyGen_CheckCoroutineExact(op) (PyGen_CheckExact(op) && \
42+
(((PyCodeObject*) \
43+
((PyGenObject*)op)->gi_code) \
44+
->co_flags & (CO_ITERABLE_COROUTINE | \
45+
CO_COROUTINE)))
46+
4147
PyAPI_FUNC(PyObject *) PyGen_New(struct _frame *);
4248
PyAPI_FUNC(PyObject *) PyGen_NewWithQualName(struct _frame *,
4349
PyObject *name, PyObject *qualname);
@@ -46,6 +52,7 @@ PyAPI_FUNC(int) _PyGen_FetchStopIterationValue(PyObject **);
4652
PyObject *_PyGen_Send(PyGenObject *, PyObject *);
4753
PyAPI_FUNC(void) _PyGen_Finalize(PyObject *self);
4854

55+
PyObject *_PyGen_GetAwaitableIter(PyObject *o);
4956

5057
#ifdef __cplusplus
5158
}

0 commit comments

Comments
 (0)