Skip to content

Commit 4d81d0d

Browse files
Fix stack effect computation.
1 parent 4afb870 commit 4d81d0d

File tree

3 files changed

+2829
-2818
lines changed

3 files changed

+2829
-2818
lines changed

Python/compile.c

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -958,8 +958,7 @@ PyCompile_OpcodeStackEffect(int opcode, int oparg)
958958
case YIELD_FROM:
959959
return -1;
960960
case POP_BLOCK:
961-
/* Adjust 6 entries reserved by SETUP_FINALLY */
962-
return -6;
961+
return 0;
963962
case POP_EXCEPT:
964963
return -3;
965964
case END_FINALLY:
@@ -1036,10 +1035,11 @@ PyCompile_OpcodeStackEffect(int opcode, int oparg)
10361035
* the handler if an exception be raised. */
10371036
return 6;
10381037
case BEGIN_FINALLY:
1039-
/* The stack has been reserved by SETUP_FINALLY */
1038+
/* Actually pushes 1 value, but count 6 for balancing with
1039+
* END_FINALLY and POP_FINALLY. */
10401040
return 6;
10411041
case CALL_FINALLY:
1042-
return 0;
1042+
return 1;
10431043

10441044
case LOAD_FAST:
10451045
return 1;
@@ -1509,7 +1509,15 @@ compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info,
15091509
ADDOP(c, POP_BLOCK);
15101510
if (preserve_tos)
15111511
ADDOP(c, ROT_TWO);
1512-
ADDOP_JREL(c, CALL_FINALLY, info->fb_exit);
1512+
ADDOP(c, BEGIN_FINALLY);
1513+
ADDOP(c, WITH_CLEANUP_START);
1514+
if (info->fb_type == ASYNC_WITH) {
1515+
ADDOP(c, GET_AWAITABLE);
1516+
ADDOP_O(c, LOAD_CONST, Py_None, consts);
1517+
ADDOP(c, YIELD_FROM);
1518+
}
1519+
ADDOP(c, WITH_CLEANUP_FINISH);
1520+
ADDOP_I(c, POP_FINALLY, 0);
15131521
return 1;
15141522

15151523
case HANDLER_CLEANUP:
@@ -5003,7 +5011,7 @@ stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
50035011
{
50045012
int i, target_depth, effect;
50055013
struct instr *instr;
5006-
// assert(!b->b_seen || b->b_startdepth == depth);
5014+
assert(!b->b_seen || b->b_startdepth == depth);
50075015
if (b->b_seen || b->b_startdepth >= depth)
50085016
return maxdepth;
50095017
/* Guard against infinite recursion */
@@ -5020,13 +5028,12 @@ stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
50205028

50215029
if (depth > maxdepth)
50225030
maxdepth = depth;
5023-
if (depth < 0)
5024-
return maxdepth;
50255031
assert(depth >= 0); /* invalid code or bug in stackdepth() */
50265032
if (instr->i_jrel || instr->i_jabs) {
50275033
/* Recursively inspect jump target */
50285034
target_depth = depth;
50295035
if (instr->i_opcode == CALL_FINALLY) {
5036+
depth = depth - 1;
50305037
continue;
50315038
}
50325039
if (instr->i_opcode == FOR_ITER) {

0 commit comments

Comments
 (0)