Skip to content

bpo-39048: Look up __aenter__ before __aexit__ in the async with statement #17609

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

Merged
merged 5 commits into from
Jan 14, 2020
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
2 changes: 1 addition & 1 deletion Doc/reference/compound_stmts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -844,8 +844,8 @@ The following code::
is semantically equivalent to::

manager = (EXPRESSION)
aexit = type(manager).__aexit__
aenter = type(manager).__aenter__
aexit = type(manager).__aexit__
value = await aenter(manager)
hit_except = False

Expand Down
20 changes: 11 additions & 9 deletions Lib/test/test_coroutines.py
Original file line number Diff line number Diff line change
Expand Up @@ -1203,39 +1203,41 @@ class CM:
def __aenter__(self):
pass

body_executed = False
async def foo():
async with CM():
pass
body_executed = True

with self.assertRaisesRegex(AttributeError, '__aexit__'):
run_async(foo())
self.assertFalse(body_executed)

def test_with_3(self):
class CM:
def __aexit__(self):
pass

body_executed = False
async def foo():
async with CM():
pass
body_executed = True

with self.assertRaisesRegex(AttributeError, '__aenter__'):
run_async(foo())
self.assertFalse(body_executed)

def test_with_4(self):
class CM:
def __enter__(self):
pass

def __exit__(self):
pass
pass

body_executed = False
async def foo():
async with CM():
pass
body_executed = True

with self.assertRaisesRegex(AttributeError, '__aexit__'):
with self.assertRaisesRegex(AttributeError, '__aenter__'):
run_async(foo())
self.assertFalse(body_executed)

def test_with_5(self):
# While this test doesn't make a lot of sense,
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -1219,6 +1219,7 @@ Elena Oat
Jon Oberheide
Milan Oberkirch
Pascal Oberndoerfer
Géry Ogam
Jeffrey Ollie
Adam Olsen
Bryan Olson
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Improve the displayed error message when incorrect types are passed to ``async
with`` statements by looking up the :meth:`__aenter__` special method before
the :meth:`__aexit__` special method when entering an asynchronous context
manager. Patch by Géry Ogam.
19 changes: 10 additions & 9 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -3154,20 +3154,21 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
}

case TARGET(BEFORE_ASYNC_WITH): {
_Py_IDENTIFIER(__aexit__);
_Py_IDENTIFIER(__aenter__);

_Py_IDENTIFIER(__aexit__);
PyObject *mgr = TOP();
PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__),
*enter;
PyObject *enter = special_lookup(tstate, mgr, &PyId___aenter__);
PyObject *res;
if (exit == NULL)
if (enter == NULL) {
goto error;
}
PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__);
if (exit == NULL) {
Py_DECREF(enter);
goto error;
}
SET_TOP(exit);
enter = special_lookup(tstate, mgr, &PyId___aenter__);
Py_DECREF(mgr);
if (enter == NULL)
goto error;
res = _PyObject_CallNoArg(enter);
Py_DECREF(enter);
if (res == NULL)
Expand All @@ -3188,8 +3189,8 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
}

case TARGET(SETUP_WITH): {
_Py_IDENTIFIER(__exit__);
_Py_IDENTIFIER(__enter__);
_Py_IDENTIFIER(__exit__);
PyObject *mgr = TOP();
PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__);
PyObject *res;
Expand Down