Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-98831: rewrite RERAISE and CLEANUP_THROW in the instruction definition DSL #101511

Merged
merged 3 commits into from
Feb 2, 2023
Merged
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
28 changes: 11 additions & 17 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -739,10 +739,10 @@ dummy_func(
Py_XSETREF(exc_info->exc_value, exc_value);
}

// stack effect: (__0 -- )
inst(RERAISE) {
inst(RERAISE, (values[oparg], exc -- values[oparg])) {
assert(oparg >= 0 && oparg <= 2);
if (oparg) {
PyObject *lasti = PEEK(oparg + 1);
PyObject *lasti = values[0];
if (PyLong_Check(lasti)) {
frame->prev_instr = _PyCode_CODE(frame->f_code) + PyLong_AsLong(lasti);
assert(!_PyErr_Occurred(tstate));
Expand All @@ -753,11 +753,11 @@ dummy_func(
goto error;
}
}
PyObject *val = POP();
assert(val && PyExceptionInstance_Check(val));
PyObject *exc = Py_NewRef(PyExceptionInstance_Class(val));
PyObject *tb = PyException_GetTraceback(val);
_PyErr_Restore(tstate, exc, val, tb);
assert(exc && PyExceptionInstance_Check(exc));
Py_INCREF(exc);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Subtle: this is needed because it stays on the stack when we goto exception_unwind, so the latter will POP and DECREF it.

PyObject *typ = Py_NewRef(PyExceptionInstance_Class(exc));
PyObject *tb = PyException_GetTraceback(exc);
_PyErr_Restore(tstate, typ, exc, tb);
goto exception_unwind;
}

Expand All @@ -784,18 +784,12 @@ dummy_func(
}
}

// stack effect: (__0, __1 -- )
inst(CLEANUP_THROW) {
inst(CLEANUP_THROW, (sub_iter, last_sent_val, exc_value -- value)) {
assert(throwflag);
PyObject *exc_value = TOP();
assert(exc_value && PyExceptionInstance_Check(exc_value));
if (PyErr_GivenExceptionMatches(exc_value, PyExc_StopIteration)) {
PyObject *value = ((PyStopIterationObject *)exc_value)->value;
Py_INCREF(value);
Py_DECREF(POP()); // The StopIteration.
Py_DECREF(POP()); // The last sent value.
Py_DECREF(POP()); // The delegated sub-iterator.
PUSH(value);
value = Py_NewRef(((PyStopIterationObject *)exc_value)->value);
DECREF_INPUTS();
}
else {
PyObject *exc_type = Py_NewRef(Py_TYPE(exc_value));
Expand Down
32 changes: 19 additions & 13 deletions Python/generated_cases.c.h

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

8 changes: 4 additions & 4 deletions Python/opcode_metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,13 @@ _PyOpcode_num_popped(int opcode, int oparg, bool jump) {
case POP_EXCEPT:
return 1;
case RERAISE:
return -1;
return oparg + 1;
case PREP_RERAISE_STAR:
return 2;
case END_ASYNC_FOR:
return 2;
case CLEANUP_THROW:
return -1;
return 3;
case LOAD_ASSERTION_ERROR:
return 0;
case LOAD_BUILD_CLASS:
Expand Down Expand Up @@ -451,13 +451,13 @@ _PyOpcode_num_pushed(int opcode, int oparg, bool jump) {
case POP_EXCEPT:
return 0;
case RERAISE:
return -1;
return oparg;
case PREP_RERAISE_STAR:
return 1;
case END_ASYNC_FOR:
return 0;
case CLEANUP_THROW:
return -1;
return 1;
case LOAD_ASSERTION_ERROR:
return 1;
case LOAD_BUILD_CLASS:
Expand Down