Skip to content

Commit 8c183cd

Browse files
authored
gh-98831: rewrite CHECK_EG_MATCH opcode in the instruction definition DSL (#101269)
1 parent 7589d71 commit 8c183cd

File tree

4 files changed

+29
-60
lines changed

4 files changed

+29
-60
lines changed

Python/bytecodes.c

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1894,44 +1894,24 @@ dummy_func(
18941894
b = Py_NewRef((res^oparg) ? Py_True : Py_False);
18951895
}
18961896

1897-
// stack effect: ( -- )
1898-
inst(CHECK_EG_MATCH) {
1899-
PyObject *match_type = POP();
1897+
inst(CHECK_EG_MATCH, (exc_value, match_type -- rest, match)) {
19001898
if (check_except_star_type_valid(tstate, match_type) < 0) {
1901-
Py_DECREF(match_type);
1902-
goto error;
1899+
DECREF_INPUTS();
1900+
ERROR_IF(true, error);
19031901
}
19041902

1905-
PyObject *exc_value = TOP();
1906-
PyObject *match = NULL, *rest = NULL;
1903+
match = NULL;
1904+
rest = NULL;
19071905
int res = exception_group_match(exc_value, match_type,
19081906
&match, &rest);
1909-
Py_DECREF(match_type);
1910-
if (res < 0) {
1911-
goto error;
1912-
}
1907+
DECREF_INPUTS();
1908+
ERROR_IF(res < 0, error);
19131909

1914-
if (match == NULL || rest == NULL) {
1915-
assert(match == NULL);
1916-
assert(rest == NULL);
1917-
goto error;
1918-
}
1919-
if (Py_IsNone(match)) {
1920-
PUSH(match);
1921-
Py_XDECREF(rest);
1922-
}
1923-
else {
1924-
/* Total or partial match - update the stack from
1925-
* [val]
1926-
* to
1927-
* [rest, match]
1928-
* (rest can be Py_None)
1929-
*/
1930-
1931-
SET_TOP(rest);
1932-
PUSH(match);
1910+
assert((match == NULL) == (rest == NULL));
1911+
ERROR_IF(match == NULL, error);
1912+
1913+
if (!Py_IsNone(match)) {
19331914
PyErr_SetExcInfo(NULL, Py_NewRef(match), NULL);
1934-
Py_DECREF(exc_value);
19351915
}
19361916
}
19371917

Python/ceval.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1946,7 +1946,7 @@ exception_group_match(PyObject* exc_value, PyObject *match_type,
19461946
}
19471947
/* no match */
19481948
*match = Py_NewRef(Py_None);
1949-
*rest = Py_NewRef(Py_None);
1949+
*rest = Py_NewRef(exc_value);
19501950
return 0;
19511951
}
19521952

Python/generated_cases.c.h

Lines changed: 16 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/opcode_metadata.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ static const struct {
119119
[COMPARE_AND_BRANCH_STR] = { 2, 0, DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IBC0 },
120120
[IS_OP] = { 2, 1, DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
121121
[CONTAINS_OP] = { 2, 1, DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
122-
[CHECK_EG_MATCH] = { -1, -1, DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IX },
122+
[CHECK_EG_MATCH] = { 2, 2, DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IX },
123123
[CHECK_EXC_MATCH] = { 2, 2, DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IX },
124124
[IMPORT_NAME] = { 2, 1, DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
125125
[IMPORT_FROM] = { 1, 2, DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },

0 commit comments

Comments
 (0)