Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 95c80f8

Browse files
committed
Disallow 'yield' in a 'try' block when there's a 'finally' clause.
Derived from Thomas Wouters's patch on the Iterators list, but doesn't try to read c->c_block[c->c_nblocks].
1 parent 1bf198e commit 95c80f8

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

Python/compile.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2659,10 +2659,20 @@ com_return_stmt(struct compiling *c, node *n)
26592659
static void
26602660
com_yield_stmt(struct compiling *c, node *n)
26612661
{
2662+
int i;
26622663
REQ(n, yield_stmt); /* 'yield' testlist */
26632664
if (!c->c_infunction) {
26642665
com_error(c, PyExc_SyntaxError, "'yield' outside function");
26652666
}
2667+
2668+
for (i = 0; i < c->c_nblocks; ++i) {
2669+
if (c->c_block[i] == SETUP_FINALLY) {
2670+
com_error(c, PyExc_SyntaxError,
2671+
"'yield' not allowed in a 'try' block "
2672+
"with a 'finally' clause");
2673+
return;
2674+
}
2675+
}
26662676
com_node(c, CHILD(n, 1));
26672677
com_addbyte(c, YIELD_VALUE);
26682678
com_pop(c, 1);

0 commit comments

Comments
 (0)