bpo-29922: Improve error messages in 'async with' (GH-6352) · python/cpython@fcd4e03 (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"
`
`@@ -1739,6 +1740,11 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
`
1739
1740
`PyObject *iterable = TOP();
`
1740
1741
`PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
`
1741
1742
``
``
1743
`+
if (iter == NULL) {
`
``
1744
`+
format_awaitable_error(Py_TYPE(iterable),
`
``
1745
`+
_Py_OPCODE(next_instr[-2]));
`
``
1746
`+
}
`
``
1747
+
1742
1748
`Py_DECREF(iterable);
`
1743
1749
``
1744
1750
`if (iter != NULL && PyCoro_CheckExact(iter)) {
`
`@@ -4948,6 +4954,25 @@ format_exc_unbound(PyCodeObject *co, int oparg)
`
4948
4954
` }
`
4949
4955
`}
`
4950
4956
``
``
4957
`+
static void
`
``
4958
`+
format_awaitable_error(PyTypeObject *type, int prevopcode)
`
``
4959
`+
{
`
``
4960
`+
if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
`
``
4961
`+
if (prevopcode == BEFORE_ASYNC_WITH) {
`
``
4962
`+
PyErr_Format(PyExc_TypeError,
`
``
4963
`+
"'async with' received an object from aenter "
`
``
4964
`+
"that does not implement await: %.100s",
`
``
4965
`+
type->tp_name);
`
``
4966
`+
}
`
``
4967
`+
else if (prevopcode == WITH_CLEANUP_START) {
`
``
4968
`+
PyErr_Format(PyExc_TypeError,
`
``
4969
`+
"'async with' received an object from aexit "
`
``
4970
`+
"that does not implement await: %.100s",
`
``
4971
`+
type->tp_name);
`
``
4972
`+
}
`
``
4973
`+
}
`
``
4974
`+
}
`
``
4975
+
4951
4976
`static PyObject *
`
4952
4977
`unicode_concatenate(PyObject *v, PyObject *w,
`
4953
4978
`PyFrameObject *f, const _Py_CODEUNIT *next_instr)
`