Skip to content

Commit 940e932

Browse files
committed
Add assertions for async with body
1 parent f0dbc4b commit 940e932

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

Lib/test/test_coroutines.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,35 +1203,41 @@ class CM:
12031203
def __aenter__(self):
12041204
pass
12051205

1206+
body_executed = False
12061207
async def foo():
12071208
async with CM():
1208-
pass
1209+
body_executed = True
12091210

12101211
with self.assertRaisesRegex(AttributeError, '__aexit__'):
12111212
run_async(foo())
1213+
self.assertFalse(body_executed)
12121214

12131215
def test_with_3(self):
12141216
class CM:
12151217
def __aexit__(self):
12161218
pass
12171219

1220+
body_executed = False
12181221
async def foo():
12191222
async with CM():
1220-
pass
1223+
body_executed = True
12211224

12221225
with self.assertRaisesRegex(AttributeError, '__aenter__'):
12231226
run_async(foo())
1227+
self.assertFalse(body_executed)
12241228

12251229
def test_with_4(self):
12261230
class CM:
12271231
pass
12281232

1233+
body_executed = False
12291234
async def foo():
12301235
async with CM():
1231-
pass
1236+
body_executed = True
12321237

12331238
with self.assertRaisesRegex(AttributeError, '__aenter__'):
12341239
run_async(foo())
1240+
self.assertFalse(body_executed)
12351241

12361242
def test_with_5(self):
12371243
# While this test doesn't make a lot of sense,

Python/ceval.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3154,8 +3154,8 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
31543154
}
31553155

31563156
case TARGET(BEFORE_ASYNC_WITH): {
3157-
_Py_IDENTIFIER(__aexit__);
31583157
_Py_IDENTIFIER(__aenter__);
3158+
_Py_IDENTIFIER(__aexit__);
31593159
PyObject *mgr = TOP();
31603160
PyObject *enter = special_lookup(tstate, mgr, &PyId___aenter__);
31613161
PyObject *res;
@@ -3189,8 +3189,8 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
31893189
}
31903190

31913191
case TARGET(SETUP_WITH): {
3192-
_Py_IDENTIFIER(__exit__);
31933192
_Py_IDENTIFIER(__enter__);
3193+
_Py_IDENTIFIER(__exit__);
31943194
PyObject *mgr = TOP();
31953195
PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__);
31963196
PyObject *res;

0 commit comments

Comments
 (0)