bpo-29922: Improve error messages in 'async with' (GH-6352) · python/cpython@a68f2f0 (original) (raw)
`@@ -69,6 +69,7 @@ static PyObject * unicode_concatenate(PyObject *, PyObject *,
`
69
69
`static PyObject * special_lookup(PyObject *, _Py_Identifier *);
`
70
70
`static int check_args_iterable(PyObject *func, PyObject *vararg);
`
71
71
`static void format_kwargs_mapping_error(PyObject *func, PyObject *kwargs);
`
``
72
`+
static void format_awaitable_error(PyTypeObject *, int);
`
72
73
``
73
74
`#define NAME_ERROR_MSG \
`
74
75
` "name '%.200s' is not defined"
`
`@@ -1736,6 +1737,11 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
`
1736
1737
`PyObject *iterable = TOP();
`
1737
1738
`PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
`
1738
1739
``
``
1740
`+
if (iter == NULL) {
`
``
1741
`+
format_awaitable_error(Py_TYPE(iterable),
`
``
1742
`+
_Py_OPCODE(next_instr[-2]));
`
``
1743
`+
}
`
``
1744
+
1739
1745
`Py_DECREF(iterable);
`
1740
1746
``
1741
1747
`if (iter != NULL && PyCoro_CheckExact(iter)) {
`
`@@ -4985,6 +4991,25 @@ format_exc_unbound(PyCodeObject *co, int oparg)
`
4985
4991
` }
`
4986
4992
`}
`
4987
4993
``
``
4994
`+
static void
`
``
4995
`+
format_awaitable_error(PyTypeObject *type, int prevopcode)
`
``
4996
`+
{
`
``
4997
`+
if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
`
``
4998
`+
if (prevopcode == BEFORE_ASYNC_WITH) {
`
``
4999
`+
PyErr_Format(PyExc_TypeError,
`
``
5000
`+
"'async with' received an object from aenter "
`
``
5001
`+
"that does not implement await: %.100s",
`
``
5002
`+
type->tp_name);
`
``
5003
`+
}
`
``
5004
`+
else if (prevopcode == WITH_CLEANUP_START) {
`
``
5005
`+
PyErr_Format(PyExc_TypeError,
`
``
5006
`+
"'async with' received an object from aexit "
`
``
5007
`+
"that does not implement await: %.100s",
`
``
5008
`+
type->tp_name);
`
``
5009
`+
}
`
``
5010
`+
}
`
``
5011
`+
}
`
``
5012
+
4988
5013
`static PyObject *
`
4989
5014
`unicode_concatenate(PyObject *v, PyObject *w,
`
4990
5015
`PyFrameObject *f, const _Py_CODEUNIT *next_instr)
`