@@ -69,6 +69,7 @@ static PyObject * unicode_concatenate(PyObject *, PyObject *,
6969static PyObject * special_lookup (PyObject * , _Py_Identifier * );
7070static int check_args_iterable (PyObject * func , PyObject * vararg );
7171static void format_kwargs_mapping_error (PyObject * func , PyObject * kwargs );
72+ static void format_awaitable_error (PyTypeObject * , int );
7273
7374#define NAME_ERROR_MSG \
7475 "name '%.200s' is not defined"
@@ -1739,6 +1740,11 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
17391740 PyObject * iterable = TOP ();
17401741 PyObject * iter = _PyCoro_GetAwaitableIter (iterable );
17411742
1743+ if (iter == NULL ) {
1744+ format_awaitable_error (Py_TYPE (iterable ),
1745+ _Py_OPCODE (next_instr [-2 ]));
1746+ }
1747+
17421748 Py_DECREF (iterable );
17431749
17441750 if (iter != NULL && PyCoro_CheckExact (iter )) {
@@ -4948,6 +4954,25 @@ format_exc_unbound(PyCodeObject *co, int oparg)
49484954 }
49494955}
49504956
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+
49514976static PyObject *
49524977unicode_concatenate (PyObject * v , PyObject * w ,
49534978 PyFrameObject * f , const _Py_CODEUNIT * next_instr )
0 commit comments