Skip to content

Commit 8da9704

Browse files
committed
gh-145749: Optimize named exception blocks
1 parent 2f4e4ec commit 8da9704

File tree

2 files changed

+13
-23
lines changed

2 files changed

+13
-23
lines changed

Lib/test/test_dis.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -469,24 +469,22 @@ def foo(a: int, b: str) -> str:
469469
470470
%4d LOAD_GLOBAL 0 (Exception)
471471
CHECK_EXC_MATCH
472-
POP_JUMP_IF_FALSE 24 (to L9)
472+
POP_JUMP_IF_FALSE 22 (to L9)
473473
L4: NOT_TAKEN
474474
L5: STORE_FAST 0 (e)
475475
476476
%4d L6: LOAD_FAST 0 (e)
477477
LOAD_ATTR 2 (__traceback__)
478478
STORE_FAST 1 (tb)
479479
L7: POP_EXCEPT
480-
LOAD_CONST 1 (None)
480+
PUSH_NULL
481481
STORE_FAST 0 (e)
482-
DELETE_FAST 0 (e)
483482
484483
%4d LOAD_FAST 1 (tb)
485484
RETURN_VALUE
486485
487-
-- L8: LOAD_CONST 1 (None)
486+
-- L8: PUSH_NULL
488487
STORE_FAST 0 (e)
489-
DELETE_FAST 0 (e)
490488
RERAISE 1
491489
492490
%4d L9: RERAISE 0

Python/codegen.c

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2526,7 +2526,7 @@ codegen_try_except(compiler *c, stmt_ty s)
25262526
try:
25272527
# body
25282528
finally:
2529-
name = None # in case body contains "del name"
2529+
name = <NULL> # in case body contains "del name"
25302530
del name
25312531
*/
25322532

@@ -2541,26 +2541,22 @@ codegen_try_except(compiler *c, stmt_ty s)
25412541
/* second # body */
25422542
VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
25432543
_PyCompile_PopFBlock(c, COMPILE_FBLOCK_HANDLER_CLEANUP, cleanup_body);
2544-
/* name = None; del name; # Mark as artificial */
2544+
/* name = <NULL>; del name; # Mark as artificial */
25452545
ADDOP(c, NO_LOCATION, POP_BLOCK);
25462546
ADDOP(c, NO_LOCATION, POP_BLOCK);
25472547
ADDOP(c, NO_LOCATION, POP_EXCEPT);
2548-
ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None);
2548+
ADDOP(c, NO_LOCATION, PUSH_NULL);
25492549
RETURN_IF_ERROR(
25502550
codegen_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Store));
2551-
RETURN_IF_ERROR(
2552-
codegen_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Del));
25532551
ADDOP_JUMP(c, NO_LOCATION, JUMP_NO_INTERRUPT, end);
25542552

25552553
/* except: */
25562554
USE_LABEL(c, cleanup_end);
25572555

2558-
/* name = None; del name; # artificial */
2559-
ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None);
2556+
/* name = <NULL>; del name; # artificial */
2557+
ADDOP(c, NO_LOCATION, PUSH_NULL);
25602558
RETURN_IF_ERROR(
25612559
codegen_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Store));
2562-
RETURN_IF_ERROR(
2563-
codegen_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Del));
25642560

25652561
ADDOP_I(c, NO_LOCATION, RERAISE, 1);
25662562
}
@@ -2725,7 +2721,7 @@ codegen_try_star_except(compiler *c, stmt_ty s)
27252721
try:
27262722
# body
27272723
finally:
2728-
name = None # in case body contains "del name"
2724+
name = <NULL> # in case body contains "del name"
27292725
del name
27302726
*/
27312727
/* second try: */
@@ -2739,27 +2735,23 @@ codegen_try_star_except(compiler *c, stmt_ty s)
27392735
/* second # body */
27402736
VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
27412737
_PyCompile_PopFBlock(c, COMPILE_FBLOCK_HANDLER_CLEANUP, cleanup_body);
2742-
/* name = None; del name; # artificial */
2738+
/* name = <NULL>; del name; # artificial */
27432739
ADDOP(c, NO_LOCATION, POP_BLOCK);
27442740
if (handler->v.ExceptHandler.name) {
2745-
ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None);
2741+
ADDOP(c, NO_LOCATION, PUSH_NULL);
27462742
RETURN_IF_ERROR(
27472743
codegen_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Store));
2748-
RETURN_IF_ERROR(
2749-
codegen_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Del));
27502744
}
27512745
ADDOP_JUMP(c, NO_LOCATION, JUMP_NO_INTERRUPT, except);
27522746

27532747
/* except: */
27542748
USE_LABEL(c, cleanup_end);
27552749

2756-
/* name = None; del name; # artificial */
2750+
/* name = <NULL>; del name; # artificial */
27572751
if (handler->v.ExceptHandler.name) {
2758-
ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None);
2752+
ADDOP(c, NO_LOCATION, PUSH_NULL);
27592753
RETURN_IF_ERROR(
27602754
codegen_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Store));
2761-
RETURN_IF_ERROR(
2762-
codegen_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Del));
27632755
}
27642756

27652757
/* add exception raised to the res list */

0 commit comments

Comments
 (0)