@@ -66,6 +66,7 @@ static PyObject * unicode_concatenate(PyObject *, PyObject *,
6666static PyObject * special_lookup (PyObject * , _Py_Identifier * );
6767static int check_args_iterable (PyObject * func , PyObject * vararg );
6868static void format_kwargs_mapping_error (PyObject * func , PyObject * kwargs );
69+ static void format_awaitable_error (PyTypeObject * , int );
6970
7071#define NAME_ERROR_MSG \
7172 "name '%.200s' is not defined"
@@ -2040,6 +2041,11 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
20402041 PyObject * iterable = TOP ();
20412042 PyObject * iter = _PyCoro_GetAwaitableIter (iterable );
20422043
2044+ if (iter == NULL ) {
2045+ format_awaitable_error (Py_TYPE (iterable ),
2046+ _Py_OPCODE (next_instr [-2 ]));
2047+ }
2048+
20432049 Py_DECREF (iterable );
20442050
20452051 if (iter != NULL && PyCoro_CheckExact (iter )) {
@@ -5403,6 +5409,25 @@ format_exc_unbound(PyCodeObject *co, int oparg)
54035409 }
54045410}
54055411
5412+ static void
5413+ format_awaitable_error (PyTypeObject * type , int prevopcode )
5414+ {
5415+ if (type -> tp_as_async == NULL || type -> tp_as_async -> am_await == NULL ) {
5416+ if (prevopcode == BEFORE_ASYNC_WITH ) {
5417+ PyErr_Format (PyExc_TypeError ,
5418+ "'async with' received an object from __aenter__ "
5419+ "that does not implement __await__: %.100s" ,
5420+ type -> tp_name );
5421+ }
5422+ else if (prevopcode == WITH_CLEANUP_START ) {
5423+ PyErr_Format (PyExc_TypeError ,
5424+ "'async with' received an object from __aexit__ "
5425+ "that does not implement __await__: %.100s" ,
5426+ type -> tp_name );
5427+ }
5428+ }
5429+ }
5430+
54065431static PyObject *
54075432unicode_concatenate (PyObject * v , PyObject * w ,
54085433 PyFrameObject * f , const _Py_CODEUNIT * next_instr )
0 commit comments