8000 GH-96793: Implement PEP 479 in bytecode. by markshannon · Pull Request #99006 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

GH-96793: Implement PEP 479 in bytecode. #99006

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Wrap async generators and generator expressions in handler for Stop[A…
…sync]Iteration.
  • Loading branch information
markshannon committed Nov 2, 2022
commit 9cda4fcd71cb5967a34886c997a78072016611a5
16 changes: 15 additions & 1 deletion Lib/test/test_dis.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,10 +545,12 @@ async def _asyncwith(c):
RERAISE 1
>> LOAD_ERROR 1
CHECK_EXC_MATCH
POP_JUMP_IF_FALSE 8 (to 140)
POP_JUMP_IF_FALSE 10 (to 144)
PUSH_EXC_INFO
LOAD_ERROR 2
LOAD_CONST 3 ('coroutine raised StopIteration')
CALL 0
SWAP 2
RAISE_VARARGS 2
>> RERAISE 1
ExceptionTable:
Expand Down Expand Up @@ -710,6 +712,18 @@ def foo(x):
JUMP_BACKWARD 9 (to 8)
>> END_FOR
RETURN_VALUE
>> LOAD_ERROR 1
CHECK_EXC_MATCH
POP_JUMP_IF_FALSE 10 (to 56)
PUSH_EXC_INFO
LOAD_ERROR 2
LOAD_CONST 1 ('generator raised StopIteration')
CALL 0
SWAP 2
RAISE_VARARGS 2
>> RERAISE 1
ExceptionTable:
1 row
""" % (dis_nested_1,
__file__,
_h.__code__.co_firstlineno + 3,
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_sys.py
Original file line number Diff line number Diff line change
Expand Up @@ -1439,7 +1439,7 @@ def bar(cls):
check(bar, size('PP'))
# generator
def get_gen(): yield 1
check(get_gen(), size('P2P4P4c7P2ic??4P'))
check(get_gen(), size('P2P4P4c7P2ic??5P'))
# iterator
check(iter('abc'), size('lP'))
# callable-iterator
Expand Down
22 changes: 3 additions & 19 deletions Objects/genobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -247,25 +247,9 @@ gen_send_ex2(PyGenObject *gen, PyObject *arg, PyObject **presult,
}
}
else {
if (PyErr_ExceptionMatches(PyExc_StopIteration)) {
const char *msg = "generator raised StopIteration";
if (PyCoro_CheckExact(gen)) {
msg = "coroutine raised StopIteration";
}
else if (PyAsyncGen_CheckExact(gen)) {
msg = "async generator raised StopIteration";
}
_PyErr_FormatFromCause(PyExc_RuntimeError, "%s", msg);
}
else if (PyAsyncGen_CheckExact(gen) &&
PyErr_ExceptionMatches(PyExc_StopAsyncIteration))
{
/* code in `gen` raised a StopAsyncIteration error:
raise a RuntimeError.
*/
const char *msg = "async generator raised StopAsyncIteration";
_PyErr_FormatFromCause(PyExc_RuntimeError, "%s", msg);
}
assert(!PyErr_ExceptionMatches(PyExc_StopIteration));
assert(!PyAsyncGen_CheckExact(gen) ||
!PyErr_ExceptionMatches(PyExc_StopAsyncIteration));
}

/* generator can't be rerun, so release the frame */
Expand Down
3 changes: 3 additions & 0 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -2187,6 +2187,9 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
case 2:
value = PyExc_RuntimeError;
break;
case 3:
value = PyExc_StopAsyncIteration;
break;
default:
Py_UNREACHABLE();
}
Expand Down
94 changes: 58 additions & 36 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -2568,22 +2568,42 @@ compiler_check_debug_args(struct compiler *c, arguments_ty args)
return 1;
}

static inline int
insert_instruction(basicblock *block, int pos, struct instr *instr) {
if (basicblock_next_instr(block) < 0) {
return -1;
}
for (int i = block->b_iused - 1; i > pos; i--) {
block->b_instr[i] = block->b_instr[i-1];
}
block->b_instr[pos] = *instr;
return 0;
}

static int
coroutine_stopiteration_handler(struct compiler *c, jump_target_label *handler)
wrap_in_stopiteration_handler(struct compiler *c)
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this is not wrapping too much, like the instructions that deal with the function's arguments and annotations?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Annotations are executed in the enclosing scope, as are defaults. So there is no problem there.
MAKE_CELL and RETURN_GENERATOR instructions are inserted in the back-end so happen after this.

ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None);
ADDOP(c, NO_LOCATION, RETURN_VALUE);
if (cfg_builder_use_label(CFG_BUILDER(c), *handler) < 0) {
return 0;
}
jump_target_label other = cfg_new_label(CFG_BUILDER(c));
if (!IS_LABEL(other)) {
NEW_JUMP_TARGET_LABEL(c, handler);
NEW_JUMP_TARGET_LABEL(c, next);

/* Insert SETUP_CLEANUP at start */
struct instr setup = {
.i_opcode = SETUP_CLEANUP,
.i_oparg = handler.id,
.i_loc = NO_LOCATION,
.i_target = NULL,
};
if (insert_instruction(c->u->u_cfg_builder.g_entryblock, 0, &setup)) {
return 0;
}
USE_LABEL(c, *handler);

ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None);
ADDOP(c, NO_LOCATION, RETURN_VALUE);
USE_LABEL(c, handler);
ADDOP_I(c, NO_LOCATION, LOAD_ERROR, 1); // StopIteration
ADDOP(c, NO_LOCATION, CHECK_EXC_MATCH);
ADDOP_JUMP(c, NO_LOCATION, POP_JUMP_IF_FALSE, other);
ADDOP_JUMP(c, NO_LOCATION, POP_JUMP_IF_FALSE, next);
ADDOP(c, NO_LOCATION, PUSH_EXC_INFO);
ADDOP_I(c, NO_LOCATION, LOAD_ERROR, 2); // RuntimeError
const char *msg = c->u->u_ste->ste_coroutine ?
(c->u->u_ste->ste_generator ?
Expand All @@ -2597,9 +2617,31 @@ coroutine_stopiteration_handler(struct compiler *c, jump_target_label *handler)
}
ADDOP_LOAD_CONST_NEW(c, NO_LOCATION, message);
ADDOP_I(c, NO_LOCATION, CALL, 0);
ADDOP_I(c, NO_LOCATION, SWAP, 2);
ADDOP_I(c, NO_LOCATION, RAISE_VARARGS, 2);

USE_LABEL(c, other);
USE_LABEL(c, next);
if (c->u->u_ste->ste_coroutine &&
c->u->u_ste->ste_generator)
{
NEW_JUMP_TARGET_LABEL(c, next);
ADDOP_I(c, NO_LOCATION, LOAD_ERROR, 3); // StopAsyncIteration
ADDOP(c, NO_LOCATION, CHECK_EXC_MATCH);
ADDOP_JUMP(c, NO_LOCATION, POP_JUMP_IF_FALSE, next);
ADDOP(c, NO_LOCATION, PUSH_EXC_INFO);
ADDOP_I(c, NO_LOCATION, LOAD_ERROR, 2); // RuntimeError
const char *msg = "async generator raised StopAsyncIteration";
PyObject *message = _PyUnicode_FromASCII(msg, strlen(msg));
if (message == NULL) {
return 0;
}
ADDOP_LOAD_CONST_NEW(c, NO_LOCATION, message);
ADDOP_I(c, NO_LOCATION, CALL, 0);
ADDOP_I(c, NO_LOCATION, SWAP, 2);
ADDOP_I(c, NO_LOCATION, RAISE_VARARGS, 2);

USE_LABEL(c, next);
}
ADDOP_I(c, NO_LOCATION, RERAISE, 1);
1E0A return 1;
}
Expand Down Expand Up @@ -2669,17 +2711,6 @@ compiler_function(struct compiler *c, stmt_ty s, int is_async)
return 0;
}

bool is_coro = (c->u->u_ste->ste_coroutine || c->u->u_ste->ste_generator);
jump_target_label handler;
if (is_coro) {
handler = cfg_new_label(CFG_BUILDER(c));
if (!IS_LABEL(handler)) {
compiler_exit_scope(c);
return 0;
}
ADDOP_JUMP(c, NO_LOCATION, SETUP_CLEANUP, handler);
}

/* if not -OO mode, add docstring */
if (c->c_optimize < 2) {
docstring = _PyAST_GetDocString(body);
Expand All @@ -2695,8 +2726,8 @@ compiler_function(struct compiler *c, stmt_ty s, int is_async)
for (i = docstring ? 1 : 0; i < asdl_seq_LEN(body); i++) {
VISIT_IN_SCOPE(c, stmt, (stmt_ty)asdl_seq_GET(body, i));
}
if (is_coro) {
if (!coroutine_stopiteration_handler(c, &handler)) {
if (c->u->u_ste->ste_coroutine || c->u->u_ste->ste_generator) {
if (!wrap_in_stopiteration_handler(c)) {
compiler_exit_scope(c);
return 0;
}
Expand Down Expand Up @@ -5542,6 +5573,9 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type,
if (type != COMP_GENEXP) {
ADDOP(c, LOC(e), RETURN_VALUE);
}
if (!wrap_in_stopiteration_handler(c)) {
goto error_in_scope;
}

co = assemble(c, 1);
qualname = c->u->u_qualname;
Expand Down Expand Up @@ -8606,18 +8640,6 @@ build_cellfixedoffsets(struct compiler *c)
return fixed;
}

static inline int
insert_instruction(basicblock *block, int pos, struct instr *instr) {
if (basicblock_next_instr(block) < 0) {
return -1;
}
for (int i = block->b_iused - 1; i > pos; i--) {
block->b_instr[i] = block->b_instr[i-1];
}
block->b_instr[pos] = *instr;
return 0;
}

static int
insert_prefix_instructions(struct compiler *c, basicblock *entryblock,
int *fixed, int nfreevars, int code_flags)
Expand Down
0