Skip to content

Commit 3a1efad

Browse files
committed
improve bytecode insertion
1 parent d155803 commit 3a1efad

2 files changed

Lines changed: 54 additions & 22 deletions

File tree

Lib/test/test_monitoring.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1955,8 +1955,8 @@ def func():
19551955
('branch', 'func', 4, 4),
19561956
('line', 'func', 5),
19571957
('line', 'meth', 1),
1958-
('jump', 'func', 5, '[offset=122]'),
1959-
('branch', 'func', '[offset=126]', '[offset=132]'),
1958+
('jump', 'func', 5, '[offset=118]'),
1959+
('branch', 'func', '[offset=122]', '[offset=128]'),
19601960
('line', 'get_events', 11)])
19611961

19621962
self.check_events(func, recorders = FLOW_AND_LINE_RECORDERS, expected = [
@@ -1970,8 +1970,8 @@ def func():
19701970
('line', 'func', 5),
19711971
('line', 'meth', 1),
19721972
('return', 'meth', None),
1973-
('jump', 'func', 5, '[offset=122]'),
1974-
('branch', 'func', '[offset=126]', '[offset=132]'),
1973+
('jump', 'func', 5, '[offset=118]'),
1974+
('branch', 'func', '[offset=122]', '[offset=128]'),
19751975
('return', 'func', None),
19761976
('line', 'get_events', 11)])
19771977

Python/codegen.c

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2331,6 +2331,46 @@ codegen_continue(compiler *c, location loc)
23312331
return SUCCESS;
23322332
}
23332333

2334+
static int
2335+
codegen_clear_exception_name(compiler *c, identifier name)
2336+
{
2337+
PyObject *mangled = _PyCompile_MaybeMangle(c, name);
2338+
if (mangled == NULL) {
2339+
return ERROR;
2340+
}
2341+
2342+
int scope = _PyST_GetScope(SYMTABLE_ENTRY(c), mangled);
2343+
if (scope == -1) {
2344+
Py_DECREF(mangled);
2345+
return ERROR;
2346+
}
2347+
2348+
_PyCompile_optype optype;
2349+
Py_ssize_t arg = 0;
2350+
if (_PyCompile_ResolveNameop(
2351+
c, mangled, scope, &optype, &arg) < 0) {
2352+
Py_DECREF(mangled);
2353+
return ERROR;
2354+
}
2355+
2356+
if (optype == COMPILE_OP_FAST) {
2357+
ADDOP(c, NO_LOCATION, PUSH_NULL);
2358+
ADDOP_N(c, NO_LOCATION, STORE_FAST_MAYBE_NULL, mangled, varnames);
2359+
Py_DECREF(mangled);
2360+
return SUCCESS;
2361+
}
2362+
2363+
Py_DECREF(mangled);
2364+
2365+
ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None);
2366+
RETURN_IF_ERROR(
2367+
codegen_nameop(c, NO_LOCATION, name, Store));
2368+
RETURN_IF_ERROR(
2369+
codegen_nameop(c, NO_LOCATION, name, Del));
2370+
2371+
return SUCCESS;
2372+
}
2373+
23342374

23352375
/* Code generated for "try: <body> finally: <finalbody>" is as follows:
23362376
@@ -2555,7 +2595,7 @@ codegen_try_except(compiler *c, stmt_ty s)
25552595
try:
25562596
# body
25572597
finally:
2558-
name = <NULL> # in case body contains "del name"
2598+
name = None # in case body contains "del name"
25592599
del name
25602600
*/
25612601

@@ -2570,22 +2610,18 @@ codegen_try_except(compiler *c, stmt_ty s)
25702610
/* second # body */
25712611
VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
25722612
_PyCompile_PopFBlock(c, COMPILE_FBLOCK_HANDLER_CLEANUP, cleanup_body);
2573-
/* name = <NULL>; del name; # Mark as artificial */
2613+
/* name = None; del name; # Mark as artificial */
25742614
ADDOP(c, NO_LOCATION, POP_BLOCK);
25752615
ADDOP(c, NO_LOCATION, POP_BLOCK);
25762616
ADDOP(c, NO_LOCATION, POP_EXCEPT);
2577-
ADDOP(c, NO_LOCATION, PUSH_NULL);
2578-
RETURN_IF_ERROR(
2579-
codegen_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Store));
2617+
codegen_clear_exception_name(c, handler->v.ExceptHandler.name);
25802618
ADDOP_JUMP(c, NO_LOCATION, JUMP_NO_INTERRUPT, end);
25812619

25822620
/* except: */
25832621
USE_LABEL(c, cleanup_end);
25842622

2585-
/* name = <NULL>; del name; # artificial */
2586-
ADDOP(c, NO_LOCATION, PUSH_NULL);
2587-
RETURN_IF_ERROR(
2588-
codegen_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Store));
2623+
/* name = None; del name; # artificial */
2624+
codegen_clear_exception_name(c, handler->v.ExceptHandler.name);
25892625

25902626
ADDOP_I(c, NO_LOCATION, RERAISE, 1);
25912627
}
@@ -2750,7 +2786,7 @@ codegen_try_star_except(compiler *c, stmt_ty s)
27502786
try:
27512787
# body
27522788
finally:
2753-
name = <NULL> # in case body contains "del name"
2789+
name = None # in case body contains "del name"
27542790
del name
27552791
*/
27562792
/* second try: */
@@ -2764,23 +2800,19 @@ codegen_try_star_except(compiler *c, stmt_ty s)
27642800
/* second # body */
27652801
VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
27662802
_PyCompile_PopFBlock(c, COMPILE_FBLOCK_HANDLER_CLEANUP, cleanup_body);
2767-
/* name = <NULL>; del name; # artificial */
2803+
/* name = None; del name; # artificial */
27682804
ADDOP(c, NO_LOCATION, POP_BLOCK);
27692805
if (handler->v.ExceptHandler.name) {
2770-
ADDOP(c, NO_LOCATION, PUSH_NULL);
2771-
RETURN_IF_ERROR(
2772-
codegen_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Store));
2806+
codegen_clear_exception_name(c, handler->v.ExceptHandler.name);
27732807
}
27742808
ADDOP_JUMP(c, NO_LOCATION, JUMP_NO_INTERRUPT, except);
27752809

27762810
/* except: */
27772811
USE_LABEL(c, cleanup_end);
27782812

2779-
/* name = <NULL>; del name; # artificial */
2813+
/* name = None; del name; # artificial */
27802814
if (handler->v.ExceptHandler.name) {
2781-
ADDOP(c, NO_LOCATION, PUSH_NULL);
2782-
RETURN_IF_ERROR(
2783-
codegen_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Store));
2815+
codegen_clear_exception_name(c, handler->v.ExceptHandler.name);
27842816
}
27852817

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

0 commit comments

Comments
 (0)