Skip to content

Commit a8b3215

Browse files
committed
gh-98831: rewrite BEFORE_ASYNC_WITH and END_ASYNC_FOR in the instruction definition DSL
1 parent 29a858b commit a8b3215

File tree

3 files changed

+40
-38
lines changed

3 files changed

+40
-38
lines changed

Python/bytecodes.c

+15-20
Original file line numberDiff line numberDiff line change
@@ -770,18 +770,16 @@ dummy_func(
770770
ERROR_IF(val == NULL, error);
771771
}
772772

773-
// stack effect: (__0, __1 -- )
774-
inst(END_ASYNC_FOR) {
775-
PyObject *val = POP();
776-
assert(val && PyExceptionInstance_Check(val));
777-
if (PyErr_GivenExceptionMatches(val, PyExc_StopAsyncIteration)) {
778-
Py_DECREF(val);
779-
Py_DECREF(POP());
773+
inst(END_ASYNC_FOR, (awaitable, exc -- )) {
774+
assert(exc && PyExceptionInstance_Check(exc));
775+
if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
776+
DECREF_INPUTS();
780777
}
781778
else {
782-
PyObject *exc = Py_NewRef(PyExceptionInstance_Class(val));
783-
PyObject *tb = PyException_GetTraceback(val);
784-
_PyErr_Restore(tstate, exc, val, tb);
779+
Py_INCREF(exc);
780+
PyObject *typ = Py_NewRef(PyExceptionInstance_Class(exc));
781+
PyObject *tb = PyException_GetTraceback(exc);
782+
_PyErr_Restore(tstate, typ, exc, tb);
785783
goto exception_unwind;
786784
}
787785
}
@@ -2266,10 +2264,7 @@ dummy_func(
22662264
DISPATCH_INLINED(gen_frame);
22672265
}
22682266

2269-
// stack effect: ( -- __0)
2270-
inst(BEFORE_ASYNC_WITH) {
2271-
PyObject *mgr = TOP();
2272-
PyObject *res;
2267+
inst(BEFORE_ASYNC_WITH, (mgr -- exit, res)) {
22732268
PyObject *enter = _PyObject_LookupSpecial(mgr, &_Py_ID(__aenter__));
22742269
if (enter == NULL) {
22752270
if (!_PyErr_Occurred(tstate)) {
@@ -2280,7 +2275,7 @@ dummy_func(
22802275
}
22812276
goto error;
22822277
}
2283-
PyObject *exit = _PyObject_LookupSpecial(mgr, &_Py_ID(__aexit__));
2278+
exit = _PyObject_LookupSpecial(mgr, &_Py_ID(__aexit__));
22842279
if (exit == NULL) {
22852280
if (!_PyErr_Occurred(tstate)) {
22862281
_PyErr_Format(tstate, PyExc_TypeError,
@@ -2292,13 +2287,13 @@ dummy_func(
22922287
Py_DECREF(enter);
22932288
goto error;
22942289
}
2295-
SET_TOP(exit);
2296-
Py_DECREF(mgr);
2290+
DECREF_INPUTS();
22972291
res = _PyObject_CallNoArgs(enter);
22982292
Py_DECREF(enter);
2299-
if (res == NULL)
2300-
goto error;
2301-
PUSH(res);
2293+
if (res == NULL) {
2294+
Py_DECREF(exit);
2295+
ERROR_IF(true, error);
2296+
}
23022297
PREDICT(GET_AWAITABLE);
23032298
}
23042299

Python/generated_cases.c.h

+21-14
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
@@ -109,7 +109,7 @@ _PyOpcode_num_popped(int opcode, int oparg) {
109109
case PREP_RERAISE_STAR:
110110
return 2;
111111
case END_ASYNC_FOR:
112-
return -1;
112+
return 2;
113113
case CLEANUP_THROW:
114114
return -1;
115115
case LOAD_ASSERTION_ERROR:
@@ -271,7 +271,7 @@ _PyOpcode_num_popped(int opcode, int oparg) {
271271
case FOR_ITER_GEN:
272272
return -1;
273273
case BEFORE_ASYNC_WITH:
274-
return -1;
274+
return 1;
275275
case BEFORE_WITH:
276276
return 1;
277277
case WITH_EXCEPT_START:
@@ -455,7 +455,7 @@ _PyOpcode_num_pushed(int opcode, int oparg) {
455455
case PREP_RERAISE_STAR:
456456
return 1;
457457
case END_ASYNC_FOR:
458-
return -1;
458+
return 0;
459459
case CLEANUP_THROW:
460460
return -1;
461461
case LOAD_ASSERTION_ERROR:
@@ -617,7 +617,7 @@ _PyOpcode_num_pushed(int opcode, int oparg) {
617617
case FOR_ITER_GEN:
618618
return -1;
619619
case BEFORE_ASYNC_WITH:
620-
return -1;
620+
return 2;
621621
case BEFORE_WITH:
622622
return 2;
623623
case WITH_EXCEPT_START:

0 commit comments

Comments
 (0)