[Zope-Checkins] CVS: Zope3/lib/python/Persistence/BTrees - SetTemplate.c:1.1.2.5

Jeremy Hylton jeremy@zope.com
Thu, 28 Feb 2002 16:40:53 -0500


Update of /cvs-repository/Zope3/lib/python/Persistence/BTrees
In directory cvs.zope.org:/tmp/cvs-serv6887

Modified Files:
      Tag: Zope-3x-branch
	SetTemplate.c 
Log Message:
_Set_update() leaked an iterator when it returned successfully.

Redo logic to provide single exit path that always DECREFs iter.


=== Zope3/lib/python/Persistence/BTrees/SetTemplate.c 1.1.2.4 => 1.1.2.5 ===
 _Set_update(Bucket *self, PyObject *seq)
 {
-    int n = 0;
+    int n = -1;
     PyObject *iter, *v;
     int ind;
 
@@ -43,20 +43,27 @@
     while (1) {
 	v = PyIter_Next(iter);
 	if (v == NULL) {
-	    if (PyErr_Occurred()) {
-		Py_DECREF(iter);
-		return -1;
-	    } else
+	    if (PyErr_Occurred())
+		goto err;
+	    else
 		break;
 	}
 	ind = _bucket_set(self, v, Py_None, 1, 1, 0);
 	Py_DECREF(v);
-	if (ind < 0) {
-	    Py_DECREF(iter);
-	    return -1;
-	} else
+	if (ind < 0)
+	    goto err;
+	else
 	    n += ind;
     }
+    /* n starts out at -1, which is the error return value.  If
+       this point is reached, then there is no error.  n must be
+       incremented to account for the initial value of -1 instead of
+       0.  
+    */
+    n++;
+
+ err:
+    Py_DECREF(iter);
     return n;
 }