@@ -2145,6 +2145,7 @@ surf_fblits(pgSurfaceObject *self, PyObject *const *args, Py_ssize_t nargs)
21452145 PyObject * blit_sequence , * item , * src_surf , * blit_pos ;
21462146 int blend_flags = 0 ; /* Default flag is 0, opaque */
21472147 int error = 0 ;
2148+ int is_generator = 0 ;
21482149
21492150 if (nargs == 0 || nargs > 2 ) {
21502151 error = FBLITS_ERR_INCORRECT_ARGS_NUM ;
@@ -2216,11 +2217,11 @@ surf_fblits(pgSurfaceObject *self, PyObject *const *args, Py_ssize_t nargs)
22162217 }
22172218 /* Generator path */
22182219 else if (PyIter_Check (blit_sequence )) {
2220+ is_generator = 1 ;
22192221 while ((item = PyIter_Next (blit_sequence ))) {
22202222 /* Check that the item is a tuple of length 2 */
22212223 if (!PyTuple_Check (item ) || PyTuple_GET_SIZE (item ) != 2 ) {
22222224 error = FBLITS_ERR_TUPLE_REQUIRED ;
2223- Py_DECREF (item );
22242225 goto on_error ;
22252226 }
22262227
@@ -2229,8 +2230,6 @@ surf_fblits(pgSurfaceObject *self, PyObject *const *args, Py_ssize_t nargs)
22292230 src_surf = PyTuple_GET_ITEM (item , 0 );
22302231 blit_pos = PyTuple_GET_ITEM (item , 1 );
22312232
2232- Py_DECREF (item );
2233-
22342233 /* Check that the source is a Surface */
22352234 if (!pgSurface_Check (src_surf )) {
22362235 error = BLITS_ERR_SOURCE_NOT_SURFACE ;
@@ -2262,6 +2261,8 @@ surf_fblits(pgSurfaceObject *self, PyObject *const *args, Py_ssize_t nargs)
22622261 error = BLITS_ERR_BLIT_FAIL ;
22632262 goto on_error ;
22642263 }
2264+
2265+ Py_DECREF (item );
22652266 }
22662267 }
22672268 else {
@@ -2272,6 +2273,9 @@ surf_fblits(pgSurfaceObject *self, PyObject *const *args, Py_ssize_t nargs)
22722273 Py_RETURN_NONE ;
22732274
22742275on_error :
2276+ if (is_generator ) {
2277+ Py_XDECREF (item );
2278+ }
22752279 switch (error ) {
22762280 case BLITS_ERR_SEQUENCE_REQUIRED :
22772281 return RAISE (
0 commit comments