Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 3 additions & 5 deletions Lib/test/test_dis.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,24 +470,22 @@ def foo(a: int, b: str) -> str:

%4d LOAD_GLOBAL 0 (Exception)
CHECK_EXC_MATCH
POP_JUMP_IF_FALSE 24 (to L9)
POP_JUMP_IF_FALSE 22 (to L9)
L4: NOT_TAKEN
L5: STORE_FAST 0 (e)

%4d L6: LOAD_FAST 0 (e)
LOAD_ATTR 2 (__traceback__)
STORE_FAST 1 (tb)
L7: POP_EXCEPT
LOAD_COMMON_CONSTANT 7 (None)
PUSH_NULL
STORE_FAST 0 (e)
DELETE_FAST 0 (e)

%4d LOAD_FAST 1 (tb)
RETURN_VALUE

-- L8: LOAD_COMMON_CONSTANT 7 (None)
-- L8: PUSH_NULL
STORE_FAST 0 (e)
DELETE_FAST 0 (e)
RERAISE 1

%4d L9: RERAISE 0
Expand Down
8 changes: 4 additions & 4 deletions Lib/test/test_monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -1955,8 +1955,8 @@ def func():
('branch', 'func', 4, 4),
('line', 'func', 5),
('line', 'meth', 1),
('jump', 'func', 5, '[offset=122]'),
('branch', 'func', '[offset=126]', '[offset=132]'),
('jump', 'func', 5, '[offset=118]'),
('branch', 'func', '[offset=122]', '[offset=128]'),
('line', 'get_events', 11)])

self.check_events(func, recorders = FLOW_AND_LINE_RECORDERS, expected = [
Expand All @@ -1970,8 +1970,8 @@ def func():
('line', 'func', 5),
('line', 'meth', 1),
('return', 'meth', None),
('jump', 'func', 5, '[offset=122]'),
('branch', 'func', '[offset=126]', '[offset=132]'),
('jump', 'func', 5, '[offset=118]'),
('branch', 'func', '[offset=122]', '[offset=128]'),
('return', 'func', None),
('line', 'get_events', 11)])

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Named exception blocks are now faster as they no longer use a ``DELETE_FAST`` instruction.
64 changes: 44 additions & 20 deletions Python/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -2331,6 +2331,46 @@ codegen_continue(compiler *c, location loc)
return SUCCESS;
}

static int
codegen_clear_exception_name(compiler *c, identifier name)
{
PyObject *mangled = _PyCompile_MaybeMangle(c, name);
if (mangled == NULL) {
return ERROR;
}

int scope = _PyST_GetScope(SYMTABLE_ENTRY(c), mangled);
if (scope == -1) {
Py_DECREF(mangled);
return ERROR;
}

_PyCompile_optype optype;
Py_ssize_t arg = 0;
if (_PyCompile_ResolveNameop(
c, mangled, scope, &optype, &arg) < 0) {
Py_DECREF(mangled);
return ERROR;
}

if (optype == COMPILE_OP_FAST) {
ADDOP(c, NO_LOCATION, PUSH_NULL);
ADDOP_N(c, NO_LOCATION, STORE_FAST_MAYBE_NULL, mangled, varnames);
Py_DECREF(mangled);
return SUCCESS;
}

Py_DECREF(mangled);

ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None);
RETURN_IF_ERROR(
codegen_nameop(c, NO_LOCATION, name, Store));
RETURN_IF_ERROR(
codegen_nameop(c, NO_LOCATION, name, Del));

return SUCCESS;
}


/* Code generated for "try: <body> finally: <finalbody>" is as follows:

Expand Down Expand Up @@ -2574,22 +2614,14 @@ codegen_try_except(compiler *c, stmt_ty s)
ADDOP(c, NO_LOCATION, POP_BLOCK);
ADDOP(c, NO_LOCATION, POP_BLOCK);
ADDOP(c, NO_LOCATION, POP_EXCEPT);
ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None);
RETURN_IF_ERROR(
codegen_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Store));
RETURN_IF_ERROR(
codegen_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Del));
codegen_clear_exception_name(c, handler->v.ExceptHandler.name);
ADDOP_JUMP(c, NO_LOCATION, JUMP_NO_INTERRUPT, end);

/* except: */
USE_LABEL(c, cleanup_end);

/* name = None; del name; # artificial */
ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None);
RETURN_IF_ERROR(
codegen_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Store));
RETURN_IF_ERROR(
codegen_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Del));
codegen_clear_exception_name(c, handler->v.ExceptHandler.name);

ADDOP_I(c, NO_LOCATION, RERAISE, 1);
}
Expand Down Expand Up @@ -2771,11 +2803,7 @@ codegen_try_star_except(compiler *c, stmt_ty s)
/* name = None; del name; # artificial */
ADDOP(c, NO_LOCATION, POP_BLOCK);
if (handler->v.ExceptHandler.name) {
ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None);
RETURN_IF_ERROR(
codegen_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Store));
RETURN_IF_ERROR(
codegen_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Del));
codegen_clear_exception_name(c, handler->v.ExceptHandler.name);
}
ADDOP_JUMP(c, NO_LOCATION, JUMP_NO_INTERRUPT, except);

Expand All @@ -2784,11 +2812,7 @@ codegen_try_star_except(compiler *c, stmt_ty s)

/* name = None; del name; # artificial */
if (handler->v.ExceptHandler.name) {
ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None);
RETURN_IF_ERROR(
codegen_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Store));
RETURN_IF_ERROR(
codegen_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Del));
codegen_clear_exception_name(c, handler->v.ExceptHandler.name);
}

/* add exception raised to the res list */
Expand Down
Loading