Skip to content

bpo-39934: Account for control blocks in 'except' in compiler. #22395

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 2 commits into from
Sep 25, 2020
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
53 changes: 53 additions & 0 deletions Lib/test/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,59 @@
>>> import ä £
Traceback (most recent call last):
SyntaxError: invalid character '£' (U+00A3)

Too many implicit blocks in named exception handlers

>>> try:
... raise Exception
... except Exception as e:
... try:
... raise Exception
... except Exception as e:
... try:
... raise Exception
... except Exception as e:
... try:
... raise Exception
... except Exception as e:
... try:
... raise Exception
... except Exception as e:
... try:
... raise Exception
... except Exception as e:
... try:
... raise Exception
... except Exception as e:
... try:
... raise Exception
... except Exception as e:
... try:
... raise Exception
... except Exception as e:
... try:
... raise Exception
... except Exception as e:
... try:
... raise Exception
... except Exception as e:
... try:
... raise Exception
... except Exception as e:
... try:
... raise Exception
... except Exception as e:
... try:
... raise Exception
... except Exception as e:
... try:
... raise Exception
... except Exception as e:
... pass
...
Traceback (most recent call last):
SyntaxError: too many statically nested blocks

"""

import re
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Correctly count control blocks in 'except' in compiler. Ensures that a
syntax error, rather a fatal error, occurs for deeply nested, named
exception handlers.
15 changes: 10 additions & 5 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ It's called a frame block to distinguish it from a basic block in the
compiler IR.
*/

enum fblocktype { WHILE_LOOP, FOR_LOOP, EXCEPT, FINALLY_TRY, FINALLY_END,
WITH, ASYNC_WITH, HANDLER_CLEANUP, POP_VALUE };
enum fblocktype { WHILE_LOOP, FOR_LOOP, TRY_EXCEPT, FINALLY_TRY, FINALLY_END,
WITH, ASYNC_WITH, HANDLER_CLEANUP, POP_VALUE, EXCEPTION_HANDLER };

struct fblockinfo {
enum fblocktype fb_type;
Expand Down Expand Up @@ -1665,6 +1665,7 @@ compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info,
{
switch (info->fb_type) {
case WHILE_LOOP:
case EXCEPTION_HANDLER:
return 1;

case FOR_LOOP:
Expand All @@ -1675,7 +1676,7 @@ compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info,
ADDOP(c, POP_TOP);
return 1;

case EXCEPT:
case TRY_EXCEPT:
ADDOP(c, POP_BLOCK);
return 1;

Expand Down Expand Up @@ -3060,14 +3061,17 @@ compiler_try_except(struct compiler *c, stmt_ty s)
return 0;
ADDOP_JUMP(c, SETUP_FINALLY, except);
compiler_use_next_block(c, body);
if (!compiler_push_fblock(c, EXCEPT, body, NULL, NULL))
if (!compiler_push_fblock(c, TRY_EXCEPT, body, NULL, NULL))
return 0;
VISIT_SEQ(c, stmt, s->v.Try.body);
ADDOP(c, POP_BLOCK);
compiler_pop_fblock(c, EXCEPT, body);
compiler_pop_fblock(c, TRY_EXCEPT, body);
ADDOP_JUMP(c, JUMP_FORWARD, orelse);
n = asdl_seq_LEN(s->v.Try.handlers);
compiler_use_next_block(c, except);
/* Runtime will push a block here, so we need to account for that */
if (!compiler_push_fblock(c, EXCEPTION_HANDLER, NULL, NULL, NULL))
return 0;
for (i = 0; i < n; i++) {
excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
s->v.Try.handlers, i);
Expand Down Expand Up @@ -3152,6 +3156,7 @@ compiler_try_except(struct compiler *c, stmt_ty s)
}
compiler_use_next_block(c, except);
}
compiler_pop_fblock(c, EXCEPTION_HANDLER, NULL);
ADDOP(c, RERAISE);
compiler_use_next_block(c, orelse);
VISIT_SEQ(c, stmt, s->v.Try.orelse);
Expand Down