Skip to content

Commit f0dbc4b

Browse files
committed
Reorder the __aenter__ and __aexit__ checks for async with
1 parent b2b4a51 commit f0dbc4b

File tree

2 files changed

+10
-13
lines changed

2 files changed

+10
-13
lines changed

Lib/test/test_coroutines.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,17 +1224,13 @@ async def foo():
12241224

12251225
def test_with_4(self):
12261226
class CM:
1227-
def __enter__(self):
1228-
pass
1229-
1230-
def __exit__(self):
1231-
pass
1227+
pass
12321228

12331229
async def foo():
12341230
async with CM():
12351231
pass
12361232

1237-
with self.assertRaisesRegex(AttributeError, '__aexit__'):
1233+
with self.assertRaisesRegex(AttributeError, '__aenter__'):
12381234
run_async(foo())
12391235

12401236
def test_with_5(self):

Python/ceval.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3156,18 +3156,19 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
31563156
case TARGET(BEFORE_ASYNC_WITH): {
31573157
_Py_IDENTIFIER(__aexit__);
31583158
_Py_IDENTIFIER(__aenter__);
3159-
31603159
PyObject *mgr = TOP();
3161-
PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__),
3162-
*enter;
3160+
PyObject *enter = special_lookup(tstate, mgr, &PyId___aenter__);
31633161
PyObject *res;
3164-
if (exit == NULL)
3162+
if (enter == NULL) {
3163+
goto error;
3164+
}
3165+
PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__);
3166+
if (exit == NULL) {
3167+
Py_DECREF(enter);
31653168
goto error;
3169+
}
31663170
SET_TOP(exit);
3167-
enter = special_lookup(tstate, mgr, &PyId___aenter__);
31683171
Py_DECREF(mgr);
3169-
if (enter == NULL)
3170-
goto error;
31713172
res = _PyObject_CallNoArg(enter);
31723173
Py_DECREF(enter);
31733174
if (res == NULL)

0 commit comments

Comments
 (0)