diff options
| author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2018-04-04 14:09:00 (GMT) |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-04-04 14:09:00 (GMT) |
| commit | fcd4e03e08a2d4ec1cde17beb66e2b22a052500f (patch) | |
| tree | 9ecbabb82b5afa78bc8b81b617f83da67b954a4b /Python | |
| parent | b8fc2d658094941250116a48577f54d1f6300362 (diff) | |
| download | cpython-fcd4e03e08a2d4ec1cde17beb66e2b22a052500f.zip cpython-fcd4e03e08a2d4ec1cde17beb66e2b22a052500f.tar.gz cpython-fcd4e03e08a2d4ec1cde17beb66e2b22a052500f.tar.bz2 | |
bpo-29922: Improve error messages in 'async with' (GH-6352)
when __aenter__() or __aexit__() return non-awaitable object.
(cherry picked from commit a68f2f0578bbf812fa2264d0e0bb388340d6e230)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Diffstat (limited to 'Python')
| -rw-r--r-- | Python/ceval.c | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/Python/ceval.c b/Python/ceval.c index af5eb99..b37205e 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -69,6 +69,7 @@ static PyObject * unicode_concatenate(PyObject *, PyObject *, static PyObject * special_lookup(PyObject *, _Py_Identifier *); static int check_args_iterable(PyObject *func, PyObject *vararg); static void format_kwargs_mapping_error(PyObject *func, PyObject *kwargs); +static void format_awaitable_error(PyTypeObject *, int); #define NAME_ERROR_MSG \ "name '%.200s' is not defined" @@ -1739,6 +1740,11 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag) PyObject *iterable = TOP(); PyObject *iter = _PyCoro_GetAwaitableIter(iterable); + if (iter == NULL) { + format_awaitable_error(Py_TYPE(iterable), + _Py_OPCODE(next_instr[-2])); + } + Py_DECREF(iterable); if (iter != NULL && PyCoro_CheckExact(iter)) { @@ -4948,6 +4954,25 @@ format_exc_unbound(PyCodeObject *co, int oparg) } } +static void +format_awaitable_error(PyTypeObject *type, int prevopcode) +{ + if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) { + if (prevopcode == BEFORE_ASYNC_WITH) { + PyErr_Format(PyExc_TypeError, + "'async with' received an object from __aenter__ " + "that does not implement __await__: %.100s", + type->tp_name); + } + else if (prevopcode == WITH_CLEANUP_START) { + PyErr_Format(PyExc_TypeError, + "'async with' received an object from __aexit__ " + "that does not implement __await__: %.100s", + type->tp_name); + } + } +} + static PyObject * unicode_concatenate(PyObject *v, PyObject *w, PyFrameObject *f, const _Py_CODEUNIT *next_instr) |
