gh-98831: rewrite BEFORE_ASYNC_WITH and END_ASYNC_FOR in the instruction definition DSL (#101458)

This commit is contained in:
Irit Katriel 2023-01-31 18:47:50 +00:00 committed by GitHub
parent f80db6cef0
commit 0062f538d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 38 deletions

View File

@ -770,18 +770,16 @@ dummy_func(
ERROR_IF(val == NULL, error); ERROR_IF(val == NULL, error);
} }
// stack effect: (__0, __1 -- ) inst(END_ASYNC_FOR, (awaitable, exc -- )) {
inst(END_ASYNC_FOR) { assert(exc && PyExceptionInstance_Check(exc));
PyObject *val = POP(); if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
assert(val && PyExceptionInstance_Check(val)); DECREF_INPUTS();
if (PyErr_GivenExceptionMatches(val, PyExc_StopAsyncIteration)) {
Py_DECREF(val);
Py_DECREF(POP());
} }
else { else {
PyObject *exc = Py_NewRef(PyExceptionInstance_Class(val)); Py_INCREF(exc);
PyObject *tb = PyException_GetTraceback(val); PyObject *typ = Py_NewRef(PyExceptionInstance_Class(exc));
_PyErr_Restore(tstate, exc, val, tb); PyObject *tb = PyException_GetTraceback(exc);
_PyErr_Restore(tstate, typ, exc, tb);
goto exception_unwind; goto exception_unwind;
} }
} }
@ -2266,10 +2264,7 @@ dummy_func(
DISPATCH_INLINED(gen_frame); DISPATCH_INLINED(gen_frame);
} }
// stack effect: ( -- __0) inst(BEFORE_ASYNC_WITH, (mgr -- exit, res)) {
inst(BEFORE_ASYNC_WITH) {
PyObject *mgr = TOP();
PyObject *res;
PyObject *enter = _PyObject_LookupSpecial(mgr, &_Py_ID(__aenter__)); PyObject *enter = _PyObject_LookupSpecial(mgr, &_Py_ID(__aenter__));
if (enter == NULL) { if (enter == NULL) {
if (!_PyErr_Occurred(tstate)) { if (!_PyErr_Occurred(tstate)) {
@ -2280,7 +2275,7 @@ dummy_func(
} }
goto error; goto error;
} }
PyObject *exit = _PyObject_LookupSpecial(mgr, &_Py_ID(__aexit__)); exit = _PyObject_LookupSpecial(mgr, &_Py_ID(__aexit__));
if (exit == NULL) { if (exit == NULL) {
if (!_PyErr_Occurred(tstate)) { if (!_PyErr_Occurred(tstate)) {
_PyErr_Format(tstate, PyExc_TypeError, _PyErr_Format(tstate, PyExc_TypeError,
@ -2292,13 +2287,13 @@ dummy_func(
Py_DECREF(enter); Py_DECREF(enter);
goto error; goto error;
} }
SET_TOP(exit); DECREF_INPUTS();
Py_DECREF(mgr);
res = _PyObject_CallNoArgs(enter); res = _PyObject_CallNoArgs(enter);
Py_DECREF(enter); Py_DECREF(enter);
if (res == NULL) if (res == NULL) {
goto error; Py_DECREF(exit);
PUSH(res); ERROR_IF(true, error);
}
PREDICT(GET_AWAITABLE); PREDICT(GET_AWAITABLE);
} }

View File

@ -981,18 +981,21 @@
} }
TARGET(END_ASYNC_FOR) { TARGET(END_ASYNC_FOR) {
PyObject *val = POP(); PyObject *exc = PEEK(1);
assert(val && PyExceptionInstance_Check(val)); PyObject *awaitable = PEEK(2);
if (PyErr_GivenExceptionMatches(val, PyExc_StopAsyncIteration)) { assert(exc && PyExceptionInstance_Check(exc));
Py_DECREF(val); if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
Py_DECREF(POP()); Py_DECREF(awaitable);
Py_DECREF(exc);
} }
else { else {
PyObject *exc = Py_NewRef(PyExceptionInstance_Class(val)); Py_INCREF(exc);
PyObject *tb = PyException_GetTraceback(val); PyObject *typ = Py_NewRef(PyExceptionInstance_Class(exc));
_PyErr_Restore(tstate, exc, val, tb); PyObject *tb = PyException_GetTraceback(exc);
_PyErr_Restore(tstate, typ, exc, tb);
goto exception_unwind; goto exception_unwind;
} }
STACK_SHRINK(2);
DISPATCH(); DISPATCH();
} }
@ -2699,7 +2702,8 @@
} }
TARGET(BEFORE_ASYNC_WITH) { TARGET(BEFORE_ASYNC_WITH) {
PyObject *mgr = TOP(); PyObject *mgr = PEEK(1);
PyObject *exit;
PyObject *res; PyObject *res;
PyObject *enter = _PyObject_LookupSpecial(mgr, &_Py_ID(__aenter__)); PyObject *enter = _PyObject_LookupSpecial(mgr, &_Py_ID(__aenter__));
if (enter == NULL) { if (enter == NULL) {
@ -2711,7 +2715,7 @@
} }
goto error; goto error;
} }
PyObject *exit = _PyObject_LookupSpecial(mgr, &_Py_ID(__aexit__)); exit = _PyObject_LookupSpecial(mgr, &_Py_ID(__aexit__));
if (exit == NULL) { if (exit == NULL) {
if (!_PyErr_Occurred(tstate)) { if (!_PyErr_Occurred(tstate)) {
_PyErr_Format(tstate, PyExc_TypeError, _PyErr_Format(tstate, PyExc_TypeError,
@ -2723,13 +2727,16 @@
Py_DECREF(enter); Py_DECREF(enter);
goto error; goto error;
} }
SET_TOP(exit);
Py_DECREF(mgr); Py_DECREF(mgr);
res = _PyObject_CallNoArgs(enter); res = _PyObject_CallNoArgs(enter);
Py_DECREF(enter); Py_DECREF(enter);
if (res == NULL) if (res == NULL) {
goto error; Py_DECREF(exit);
PUSH(res); if (true) goto pop_1_error;
}
STACK_GROW(1);
POKE(1, res);
POKE(2, exit);
PREDICT(GET_AWAITABLE); PREDICT(GET_AWAITABLE);
DISPATCH(); DISPATCH();
} }

View File

@ -109,7 +109,7 @@ _PyOpcode_num_popped(int opcode, int oparg) {
case PREP_RERAISE_STAR: case PREP_RERAISE_STAR:
return 2; return 2;
case END_ASYNC_FOR: case END_ASYNC_FOR:
return -1; return 2;
case CLEANUP_THROW: case CLEANUP_THROW:
return -1; return -1;
case LOAD_ASSERTION_ERROR: case LOAD_ASSERTION_ERROR:
@ -271,7 +271,7 @@ _PyOpcode_num_popped(int opcode, int oparg) {
case FOR_ITER_GEN: case FOR_ITER_GEN:
return -1; return -1;
case BEFORE_ASYNC_WITH: case BEFORE_ASYNC_WITH:
return -1; return 1;
case BEFORE_WITH: case BEFORE_WITH:
return 1; return 1;
case WITH_EXCEPT_START: case WITH_EXCEPT_START:
@ -455,7 +455,7 @@ _PyOpcode_num_pushed(int opcode, int oparg) {
case PREP_RERAISE_STAR: case PREP_RERAISE_STAR:
return 1; return 1;
case END_ASYNC_FOR: case END_ASYNC_FOR:
return -1; return 0;
case CLEANUP_THROW: case CLEANUP_THROW:
return -1; return -1;
case LOAD_ASSERTION_ERROR: case LOAD_ASSERTION_ERROR:
@ -617,7 +617,7 @@ _PyOpcode_num_pushed(int opcode, int oparg) {
case FOR_ITER_GEN: case FOR_ITER_GEN:
return -1; return -1;
case BEFORE_ASYNC_WITH: case BEFORE_ASYNC_WITH:
return -1; return 2;
case BEFORE_WITH: case BEFORE_WITH:
return 2; return 2;
case WITH_EXCEPT_START: case WITH_EXCEPT_START: