-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
bpo-43683: Handle generator entry in bytecode #25138
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Add GEN_START opcode. Marks start of generator, including async, or coroutine and handles | ||
sending values to a newly created generator or coroutine. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1140,6 +1140,8 @@ stack_effect(int opcode, int oparg, int jump) | |
return 1; | ||
case LIST_TO_TUPLE: | ||
return 0; | ||
case GEN_START: | ||
return -1; | ||
case LIST_EXTEND: | ||
case SET_UPDATE: | ||
case DICT_MERGE: | ||
|
@@ -6169,7 +6171,11 @@ stackdepth(struct compiler *c) | |
} | ||
|
||
sp = stack; | ||
stackdepth_push(&sp, entryblock, 0); | ||
if (c->u->u_ste->ste_generator || c->u->u_ste->ste_coroutine) { | ||
stackdepth_push(&sp, entryblock, 1); | ||
} else { | ||
stackdepth_push(&sp, entryblock, 0); | ||
} | ||
while (sp != stack) { | ||
b = *--sp; | ||
int depth = b->b_startdepth; | ||
|
@@ -6648,6 +6654,41 @@ optimize_cfg(struct assembler *a, PyObject *consts); | |
static int | ||
ensure_exits_have_lineno(struct compiler *c); | ||
|
||
static int | ||
insert_generator_prefix(struct compiler *c, basicblock *entryblock) { | ||
|
||
int flags = compute_code_flags(c); | ||
if (flags < 0) { | ||
return -1; | ||
} | ||
int kind; | ||
if (flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) { | ||
if (flags & CO_COROUTINE) { | ||
kind = 1; | ||
} | ||
else if (flags & CO_ASYNC_GENERATOR) { | ||
kind = 2; | ||
} | ||
else { | ||
kind = 0; | ||
} | ||
} | ||
else { | ||
return 0; | ||
} | ||
if (compiler_next_instr(entryblock) < 0) { | ||
return -1; | ||
} | ||
for (int i = entryblock->b_iused-1; i > 0; i--) { | ||
entryblock->b_instr[i] = entryblock->b_instr[i-1]; | ||
} | ||
entryblock->b_instr[0].i_opcode = GEN_START; | ||
entryblock->b_instr[0].i_oparg = kind; | ||
entryblock->b_instr[0].i_lineno = -1; | ||
entryblock->b_instr[0].i_target = NULL; | ||
return 0; | ||
} | ||
|
||
static PyCodeObject * | ||
assemble(struct compiler *c, int addNone) | ||
{ | ||
|
@@ -6685,6 +6726,10 @@ assemble(struct compiler *c, int addNone) | |
entryblock = b; | ||
} | ||
|
||
if (insert_generator_prefix(c, entryblock)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we're compiling a generator (or coroutine etc.) the new function assumes (without asserting) that entryblock is not NULL. But evidently it could be, since below on line 6690 there's a check whether it could be. I don't know how it could be NULL - that would mean there's no code in the function at all, but it seems there's code above that always adds at least one instruction (RETURN_VALUE on line 6668). So maybe the check for entryblock == NULL below can be replaced by an assert? (I'd place that assert right underneath the for-loop above on lines 6683-6686. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. entryblock cannot be NULL. I'll add the assert. |
||
goto error; | ||
} | ||
|
||
/* Set firstlineno if it wasn't explicitly set. */ | ||
if (!c->u->u_firstlineno) { | ||
if (entryblock && entryblock->b_instr && entryblock->b_instr->i_lineno) | ||
|
Uh oh!
There was an error while loading. Please reload this page.