Skip to content

Commit 0675b8f

Browse files
authored
gh-98831: rewrite RERAISE and CLEANUP_THROW in the instruction definition DSL (#101511)
1 parent ee21110 commit 0675b8f

File tree

3 files changed

+34
-34
lines changed

3 files changed

+34
-34
lines changed

Python/bytecodes.c

+11-17
Original file line numberDiff line numberDiff line change
@@ -739,10 +739,10 @@ dummy_func(
739739
Py_XSETREF(exc_info->exc_value, exc_value);
740740
}
741741

742-
// stack effect: (__0 -- )
743-
inst(RERAISE) {
742+
inst(RERAISE, (values[oparg], exc -- values[oparg])) {
743+
assert(oparg >= 0 && oparg <= 2);
744744
if (oparg) {
745-
PyObject *lasti = PEEK(oparg + 1);
745+
PyObject *lasti = values[0];
746746
if (PyLong_Check(lasti)) {
747747
frame->prev_instr = _PyCode_CODE(frame->f_code) + PyLong_AsLong(lasti);
748748
assert(!_PyErr_Occurred(tstate));
@@ -753,11 +753,11 @@ dummy_func(
753753
goto error;
754754
}
755755
}
756-
PyObject *val = POP();
757-
assert(val && PyExceptionInstance_Check(val));
758-
PyObject *exc = Py_NewRef(PyExceptionInstance_Class(val));
759-
PyObject *tb = PyException_GetTraceback(val);
760-
_PyErr_Restore(tstate, exc, val, tb);
756+
assert(exc && PyExceptionInstance_Check(exc));
757+
Py_INCREF(exc);
758+
PyObject *typ = Py_NewRef(PyExceptionInstance_Class(exc));
759+
PyObject *tb = PyException_GetTraceback(exc);
760+
_PyErr_Restore(tstate, typ, exc, tb);
761761
goto exception_unwind;
762762
}
763763

@@ -784,18 +784,12 @@ dummy_func(
784784
}
785785
}
786786

787-
// stack effect: (__0, __1 -- )
788-
inst(CLEANUP_THROW) {
787+
inst(CLEANUP_THROW, (sub_iter, last_sent_val, exc_value -- value)) {
789788
assert(throwflag);
790-
PyObject *exc_value = TOP();
791789
assert(exc_value && PyExceptionInstance_Check(exc_value));
792790
if (PyErr_GivenExceptionMatches(exc_value, PyExc_StopIteration)) {
793-
PyObject *value = ((PyStopIterationObject *)exc_value)->value;
794-
Py_INCREF(value);
795-
Py_DECREF(POP()); // The StopIteration.
796-
Py_DECREF(POP()); // The last sent value.
797-
Py_DECREF(POP()); // The delegated sub-iterator.
798-
PUSH(value);
791+
value = Py_NewRef(((PyStopIterationObject *)exc_value)->value);
792+
DECREF_INPUTS();
799793
}
800794
else {
801795
PyObject *exc_type = Py_NewRef(Py_TYPE(exc_value));

Python/generated_cases.c.h

+19-13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/opcode_metadata.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,13 @@ _PyOpcode_num_popped(int opcode, int oparg, bool jump) {
105105
case POP_EXCEPT:
106106
return 1;
107107
case RERAISE:
108-
return -1;
108+
return oparg + 1;
109109
case PREP_RERAISE_STAR:
110110
return 2;
111111
case END_ASYNC_FOR:
112112
return 2;
113113
case CLEANUP_THROW:
114-
return -1;
114+
return 3;
115115
case LOAD_ASSERTION_ERROR:
116116
return 0;
117117
case LOAD_BUILD_CLASS:
@@ -451,13 +451,13 @@ _PyOpcode_num_pushed(int opcode, int oparg, bool jump) {
451451
case POP_EXCEPT:
452452
return 0;
453453
case RERAISE:
454-
return -1;
454+
return oparg;
455455
case PREP_RERAISE_STAR:
456456
return 1;
457457
case END_ASYNC_FOR:
458458
return 0;
459459
case CLEANUP_THROW:
460-
return -1;
460+
return 1;
461461
case LOAD_ASSERTION_ERROR:
462462
return 1;
463463
case LOAD_BUILD_CLASS:

0 commit comments

Comments
 (0)