Skip to content

Commit 36a5650

Browse files
committed
Always initialize exception handler for new instruction
1 parent be56464 commit 36a5650

File tree

2 files changed

+47
-42
lines changed

2 files changed

+47
-42
lines changed

Lib/test/test_dis.py

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -453,52 +453,53 @@ def foo(a: int, b: str) -> str:
453453
"""
454454

455455
dis_traceback = """\
456-
%4d RESUME 0
456+
%4d RESUME 0
457457
458-
%4d NOP
458+
%4d NOP
459459
460-
%4d L1: LOAD_SMALL_INT 1
461-
LOAD_SMALL_INT 0
462-
--> BINARY_OP 11 (/)
463-
POP_TOP
460+
%4d L1: LOAD_SMALL_INT 1
461+
LOAD_SMALL_INT 0
462+
--> BINARY_OP 11 (/)
463+
POP_TOP
464464
465-
%4d L2: LOAD_FAST_CHECK 1 (tb)
466-
RETURN_VALUE
465+
%4d L2: LOAD_FAST_CHECK 1 (tb)
466+
RETURN_VALUE
467467
468-
-- L3: PUSH_EXC_INFO
468+
-- L3: PUSH_EXC_INFO
469469
470-
%4d LOAD_GLOBAL 0 (Exception)
471-
CHECK_EXC_MATCH
472-
POP_JUMP_IF_FALSE 24 (to L7)
473-
NOT_TAKEN
474-
STORE_FAST 0 (e)
470+
%4d LOAD_GLOBAL 0 (Exception)
471+
CHECK_EXC_MATCH
472+
POP_JUMP_IF_FALSE 24 (to L9)
473+
L4: NOT_TAKEN
474+
L5: STORE_FAST 0 (e)
475475
476-
%4d L4: LOAD_FAST 0 (e)
477-
LOAD_ATTR 2 (__traceback__)
478-
STORE_FAST 1 (tb)
479-
L5: POP_EXCEPT
480-
LOAD_CONST 1 (None)
481-
STORE_FAST 0 (e)
482-
DELETE_FAST 0 (e)
476+
%4d L6: LOAD_FAST 0 (e)
477+
LOAD_ATTR 2 (__traceback__)
478+
STORE_FAST 1 (tb)
479+
L7: POP_EXCEPT
480+
LOAD_CONST 1 (None)
481+
STORE_FAST 0 (e)
482+
DELETE_FAST 0 (e)
483483
484-
%4d LOAD_FAST 1 (tb)
485-
RETURN_VALUE
484+
%4d LOAD_FAST 1 (tb)
485+
RETURN_VALUE
486486
487-
-- L6: LOAD_CONST 1 (None)
488-
STORE_FAST 0 (e)
489-
DELETE_FAST 0 (e)
490-
RERAISE 1
487+
-- L8: LOAD_CONST 1 (None)
488+
STORE_FAST 0 (e)
489+
DELETE_FAST 0 (e)
490+
RERAISE 1
491491
492-
%4d L7: RERAISE 0
492+
%4d L9: RERAISE 0
493493
494-
-- L8: COPY 3
495-
POP_EXCEPT
496-
RERAISE 1
494+
-- L10: COPY 3
495+
POP_EXCEPT
496+
RERAISE 1
497497
ExceptionTable:
498498
L1 to L2 -> L3 [0]
499-
L3 to L4 -> L8 [1] lasti
500-
L4 to L5 -> L6 [1] lasti
501-
L6 to L8 -> L8 [1] lasti
499+
L3 to L4 -> L10 [1] lasti
500+
L5 to L6 -> L10 [1] lasti
501+
L6 to L7 -> L8 [1] lasti
502+
L8 to L10 -> L10 [1] lasti
502503
""" % (TRACEBACK_CODE.co_firstlineno,
503504
TRACEBACK_CODE.co_firstlineno + 1,
504505
TRACEBACK_CODE.co_firstlineno + 2,
@@ -567,11 +568,11 @@ def _with(c):
567568
%4d L3: PUSH_EXC_INFO
568569
WITH_EXCEPT_START
569570
TO_BOOL
570-
POP_JUMP_IF_TRUE 2 (to L4)
571-
NOT_TAKEN
572-
RERAISE 2
573-
L4: POP_TOP
574-
L5: POP_EXCEPT
571+
POP_JUMP_IF_TRUE 2 (to L6)
572+
L4: NOT_TAKEN
573+
L5: RERAISE 2
574+
L6: POP_TOP
575+
L7: POP_EXCEPT
575576
POP_TOP
576577
POP_TOP
577578
POP_TOP
@@ -581,12 +582,13 @@ def _with(c):
581582
LOAD_CONST 1 (None)
582583
RETURN_VALUE
583584
584-
-- L6: COPY 3
585+
-- L8: COPY 3
585586
POP_EXCEPT
586587
RERAISE 1
587588
ExceptionTable:
588589
L1 to L2 -> L3 [2] lasti
589-
L3 to L5 -> L6 [4] lasti
590+
L3 to L4 -> L8 [4] lasti
591+
L5 to L7 -> L8 [4] lasti
590592
""" % (_with.__code__.co_firstlineno,
591593
_with.__code__.co_firstlineno + 1,
592594
_with.__code__.co_firstlineno + 2,

Python/flowgraph.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,10 @@ basicblock_addop(basicblock *b, int opcode, int oparg, location loc)
199199
cfg_instr *i = &b->b_instr[off];
200200
i->i_opcode = opcode;
201201
i->i_oparg = oparg;
202-
i->i_target = NULL;
203202
i->i_loc = loc;
203+
// memory is already zero initialized
204+
assert(i->i_target == NULL);
205+
assert(i->i_except == NULL);
204206

205207
return SUCCESS;
206208
}
@@ -1104,6 +1106,7 @@ basicblock_remove_redundant_nops(basicblock *bb) {
11041106
assert(dest <= bb->b_iused);
11051107
int num_removed = bb->b_iused - dest;
11061108
bb->b_iused = dest;
1109+
memset(&bb->b_instr[dest], 0, sizeof(cfg_instr) * num_removed);
11071110
return num_removed;
11081111
}
11091112

0 commit comments

Comments
 (0)