Skip to content

Commit

Permalink
gh-98831: rewrite CHECK_EG_MATCH opcode in the instruction definition…
Browse files Browse the repository at this point in the history
… DSL (#101269)
  • Loading branch information
iritkatriel authored Jan 24, 2023
1 parent 7589d71 commit 8c183cd
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 60 deletions.
42 changes: 11 additions & 31 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -1894,44 +1894,24 @@ dummy_func(
b = Py_NewRef((res^oparg) ? Py_True : Py_False);
}

// stack effect: ( -- )
inst(CHECK_EG_MATCH) {
PyObject *match_type = POP();
inst(CHECK_EG_MATCH, (exc_value, match_type -- rest, match)) {
if (check_except_star_type_valid(tstate, match_type) < 0) {
Py_DECREF(match_type);
goto error;
DECREF_INPUTS();
ERROR_IF(true, error);
}

PyObject *exc_value = TOP();
PyObject *match = NULL, *rest = NULL;
match = NULL;
rest = NULL;
int res = exception_group_match(exc_value, match_type,
&match, &rest);
Py_DECREF(match_type);
if (res < 0) {
goto error;
}
DECREF_INPUTS();
ERROR_IF(res < 0, error);

if (match == NULL || rest == NULL) {
assert(match == NULL);
assert(rest == NULL);
goto error;
}
if (Py_IsNone(match)) {
PUSH(match);
Py_XDECREF(rest);
}
else {
/* Total or partial match - update the stack from
* [val]
* to
* [rest, match]
* (rest can be Py_None)
*/

SET_TOP(rest);
PUSH(match);
assert((match == NULL) == (rest == NULL));
ERROR_IF(match == NULL, error);

if (!Py_IsNone(match)) {
PyErr_SetExcInfo(NULL, Py_NewRef(match), NULL);
Py_DECREF(exc_value);
}
}

Expand Down
2 changes: 1 addition & 1 deletion Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -1946,7 +1946,7 @@ exception_group_match(PyObject* exc_value, PyObject *match_type,
}
/* no match */
*match = Py_NewRef(Py_None);
*rest = Py_NewRef(Py_None);
*rest = Py_NewRef(exc_value);
return 0;
}

Expand Down
43 changes: 16 additions & 27 deletions Python/generated_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Python/opcode_metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ static const struct {
[COMPARE_AND_BRANCH_STR] = { 2, 0, DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IBC0 },
[IS_OP] = { 2, 1, DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
[CONTAINS_OP] = { 2, 1, DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
[CHECK_EG_MATCH] = { -1, -1, DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IX },
[CHECK_EG_MATCH] = { 2, 2, DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IX },
[CHECK_EXC_MATCH] = { 2, 2, DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IX },
[IMPORT_NAME] = { 2, 1, DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
[IMPORT_FROM] = { 1, 2, DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
Expand Down

0 comments on commit 8c183cd

Please sign in to comment.