2121_PyGen_Finalize (PyObject * self )
2222{
2323 PyGenObject * gen = (PyGenObject * )self ;
24- PyObject * res ;
24+ PyObject * res = NULL ;
2525 PyObject * error_type , * error_value , * error_traceback ;
2626
2727 if (gen -> gi_frame == NULL || gen -> gi_frame -> f_stacktop == NULL )
@@ -33,23 +33,26 @@ _PyGen_Finalize(PyObject *self)
3333
3434 /* If `gen` is a coroutine, and if it was never awaited on,
3535 issue a RuntimeWarning. */
36- if (gen -> gi_code != NULL
37- && ((PyCodeObject * )gen -> gi_code )-> co_flags & CO_COROUTINE
38- && gen -> gi_frame -> f_lasti == -1
39- && ! PyErr_Occurred ()
40- && PyErr_WarnFormat (PyExc_RuntimeWarning , 1 ,
41- "coroutine '%.50S' was never awaited" ,
42- gen -> gi_qualname )) {
43- res = NULL ; /* oops, exception */
36+ if (gen -> gi_code != NULL &&
37+ ((PyCodeObject * )gen -> gi_code )-> co_flags & CO_COROUTINE &&
38+ gen -> gi_frame -> f_lasti == -1 ) {
39+ if (! error_value ) {
40+ PyErr_WarnFormat (PyExc_RuntimeWarning , 1 ,
41+ "coroutine '%.50S' was never awaited" ,
42+ gen -> gi_qualname );
43+ }
4444 }
4545 else {
4646 res = gen_close (gen , NULL );
4747 }
4848
49- if (res == NULL )
50- PyErr_WriteUnraisable (self );
51- else
49+ if (res == NULL ) {
50+ if (PyErr_Occurred ())
51+ PyErr_WriteUnraisable (self );
52+ }
53+ else {
5254 Py_DECREF (res );
55+ }
5356
5457 /* Restore the saved exception. */
5558 PyErr_Restore (error_type , error_value , error_traceback );
0 commit comments