3.6 async hacking

This commit is contained in:
rocky 2022-05-07 06:06:38 -04:00
parent dae00e9b0b
commit 9b80663529
7 changed files with 22 additions and 8 deletions

Binary file not shown.

Binary file not shown.

View File

@ -1,4 +1,13 @@
# These are from 3.6 test_coroutines.py
async def run_gen(f):
return (10 async for i in f)
async def run_list(f):
return [i async for i in f()]
# async def run_dict():
# return {i + 1 async for i in [10, 20]}
async def iterate(gen):
res = []
async for i in gen:

View File

@ -192,6 +192,8 @@ class Python36Parser(Python35Parser):
compare_chained2 ::= expr COMPARE_OP come_froms JUMP_FORWARD
stmt ::= genexpr_func
genexpr_func ::= LOAD_ARG _come_froms FOR_ITER store comp_iter JUMP_BACK
"""
# Some of this is duplicated from parse37. Eventually we'll probably rebase from
@ -336,9 +338,9 @@ class Python36Parser(Python35Parser):
self.addRule(
"""
expr ::= generator_exp_async
generator_exp_async ::= load_genexpr LOAD_STR MAKE_FUNCTION_0 expr
GET_AITER CALL_FUNCTION_1
generator_exp_async ::= load_genexpr LOAD_STR MAKE_FUNCTION_0 expr
GET_AITER LOAD_CONST YIELD_FROM CALL_FUNCTION_1
stmt ::= genexpr_func_async
func_async_prefix ::= _come_froms
@ -349,7 +351,7 @@ class Python36Parser(Python35Parser):
END_FINALLY COME_FROM
genexpr_func_async ::= LOAD_ARG func_async_prefix
store func_async_middle comp_iter
JUMP_BACK COME_FROM
JUMP_BACK
POP_TOP POP_TOP POP_TOP POP_EXCEPT POP_TOP
expr ::= list_comp_async

View File

@ -182,7 +182,7 @@ class ComprehensionMixin:
self.write(" in ")
if node[2] == "expr":
iter_expr = node[2]
elif node[3] == "get_aiter":
elif node[3] in ("expr", "get_aiter"):
iter_expr = node[3]
else:
iter_expr = node[-3]

View File

@ -681,17 +681,20 @@ class NonterminalActions:
self.write("(")
iter_index = 3
if self.version > (3, 2):
code_index = -6
if self.version > (3, 6):
# Python 3.7+ adds optional "come_froms" at node[0]
if self.version >= (3, 6):
if node[0].kind in ("load_closure", "load_genexpr") and self.version >= (3, 8):
code_index = -6
is_lambda = self.is_lambda
if node[0].kind == "load_genexpr":
self.is_lambda = False
self.closure_walk(node, collection_index=4)
self.is_lambda = is_lambda
else:
code_index = -6
# Python 3.7+ adds optional "come_froms" at node[0] so count from the end
if node == "generator_exp_async" and self.version[:2] == (3, 6):
code_index = 0
else:
code_index = -6
iter_index = 4 if self.version < (3, 8) else 3
self.comprehension_walk(node, iter_index=iter_index, code_index=code_index)
pass