Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
bpo-45711: remove unnecessary DUP_TOP and POP in exception handling
  • Loading branch information
iritkatriel committed Nov 9, 2021
commit ebd9c9f505fe19ef52f9f0734c8291ed47c4aa31
2 changes: 1 addition & 1 deletion Lib/importlib/_bootstrap_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ def _write_atomic(path, data, mode=0o666):
# Whenever MAGIC_NUMBER is changed, the ranges in the magic_values array
# in PC/launcher.c must also be updated.

MAGIC_NUMBER = (3462).to_bytes(2, 'little') + b'\r\n'
MAGIC_NUMBER = (3463).to_bytes(2, 'little') + b'\r\n'
_RAW_MAGIC_NUMBER = int.from_bytes(MAGIC_NUMBER, 'little') # For import.c

_PYCACHE = '__pycache__'
Expand Down
5 changes: 1 addition & 4 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -3907,7 +3907,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr
"inherit from BaseException is not "
"allowed";
PyObject *right = POP();
PyObject *left = POP();
PyObject *left = TOP();
if (PyTuple_Check(right)) {
Py_ssize_t i, length;
length = PyTuple_GET_SIZE(right);
Expand All @@ -3916,7 +3916,6 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr
if (!PyExceptionClass_Check(exc)) {
_PyErr_SetString(tstate, PyExc_TypeError,
cannot_catch_msg);
Py_DECREF(left);
Py_DECREF(right);
goto error;
}
Expand All @@ -3926,13 +3925,11 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr
if (!PyExceptionClass_Check(right)) {
_PyErr_SetString(tstate, PyExc_TypeError,
cannot_catch_msg);
Py_DECREF(left);
Py_DECREF(right);
goto error;
}
}
int res = PyErr_GivenExceptionMatches(left, right);
Py_DECREF(left);
Py_DECREF(right);
if (res > 0) {
/* Exception matches -- Do nothing */;
Expand Down
10 changes: 4 additions & 6 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -1127,7 +1127,7 @@ stack_effect(int opcode, int oparg, int jump)
case CONTAINS_OP:
return -1;
case JUMP_IF_NOT_EXC_MATCH:
return -2;
return -1;
case IMPORT_NAME:
return -1;
case IMPORT_FROM:
Expand Down Expand Up @@ -3222,16 +3222,15 @@ compiler_try_finally(struct compiler *c, stmt_ty s)
[] POP_BLOCK
[] JUMP_FORWARD L0

[tb, val, exc] L1: DUP )
[tb, val, exc, exc] <evaluate E1> )
[tb, val, exc, exc, E1] JUMP_IF_NOT_EXC_MATCH L2 ) only if E1
[tb, val, exc] L1: <evaluate E1> )
[tb, val, exc, E1] JUMP_IF_NOT_EXC_MATCH L2 ) only if E1
[tb, val, exc] POP
[tb, val] <assign to V1> (or POP if no V1)
[tb] POP
[] <code for S1>
JUMP_FORWARD L0

[tb, val, exc] L2: DUP
[tb, val, exc] L2: <evaluate E2>
.............................etc.......................

[tb, val, exc] Ln+1: RERAISE # re-raise exception
Expand Down Expand Up @@ -3281,7 +3280,6 @@ compiler_try_except(struct compiler *c, stmt_ty s)
if (except == NULL)
return 0;
if (handler->v.ExceptHandler.type) {
ADDOP(c, DUP_TOP);
VISIT(c, expr, handler->v.ExceptHandler.type);
ADDOP_JUMP(c, JUMP_IF_NOT_EXC_MATCH, except);
NEXT_BLOCK(c);
Expand Down