[Zope-Checkins] CVS: Zope3/lib/python/Persistence/BTrees - BTreeModuleTemplate.c:1.1.2.3

Jeremy Hylton jeremy@zope.com
Wed, 27 Feb 2002 00:41:28 -0500


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

Modified Files:
      Tag: Zope-3x-branch
	BTreeModuleTemplate.c 
Log Message:
Reformat IndexError(), PreviousBucket(), firstBucketOffset(),
    lastBucketOffset().
Add a few comments.

(In search of refcount bugs, none found yet.)



=== Zope3/lib/python/Persistence/BTrees/BTreeModuleTemplate.c 1.1.2.2 => 1.1.2.3 ===
 IndexError(int i)
 {                              
-  PyObject *v;
+    PyObject *v;
 
-  v=PyInt_FromLong(i);
-  UNLESS (v) {
-    v=Py_None;
-    Py_INCREF(v);
-  }
-  PyErr_SetObject(PyExc_IndexError, v);
-  Py_DECREF(v);
-  return NULL;
+    v = PyInt_FromLong(i);
+    if (!v) {
+	v = Py_None;
+	Py_INCREF(v);
+    }
+    PyErr_SetObject(PyExc_IndexError, v);
+    Py_DECREF(v);
+    return NULL;
 }
 
+/* Returns a new reference to the bucket before current. 
+   Returns NULL and sets IndexError, i on error.
+ */
+
 static Bucket *
 PreviousBucket(Bucket *current, Bucket *first, int i)
 {
-  if (! first) return NULL;
-  if (first==current)
-    {
-      IndexError(i);
-      return NULL;
+    if (!first) 
+	return NULL;
+    if (first == current) {
+	IndexError(i);
+	return NULL;
     }
 
-  Py_INCREF(first);
-  while (1)
-    {
-      PER_USE_OR_RETURN(first,NULL);
-      if (first->next==current) 
-        {
-          PER_ALLOW_DEACTIVATION(first);
-          PER_ACCESSED(first);
-          return first;
-        }
-      else if (first->next)
-        {
-          Bucket *next = first->next;
-          Py_INCREF(next);
-          PER_ALLOW_DEACTIVATION(first);
-          PER_ACCESSED(first);
-          Py_DECREF(first);
-          first=next;
-        }
-      else
-        {
-          PER_ALLOW_DEACTIVATION(first);
-          PER_ACCESSED(first);
-          Py_DECREF(first);
-          IndexError(i);
-          return NULL;
+    Py_INCREF(first);
+    while (1) {
+	/* XXX Doesn't this leak a reference to first on error? */
+	PER_USE_OR_RETURN(first, NULL);
+	if (first->next == current) {
+	    PER_ALLOW_DEACTIVATION(first);
+	    PER_ACCESSED(first);
+	    return first;
+        } else if (first->next) {
+	    Bucket *next = first->next;
+	    Py_INCREF(next);
+	    PER_ALLOW_DEACTIVATION(first);
+	    PER_ACCESSED(first);
+	    Py_DECREF(first);
+	    first = next;
+        } else {
+	    PER_ALLOW_DEACTIVATION(first);
+	    PER_ACCESSED(first);
+	    Py_DECREF(first);
+	    IndexError(i);
+	    return NULL;
         }
     }
 }
@@ -162,38 +161,48 @@
 static int 
 firstBucketOffset(Bucket **bucket, int *offset)
 {
-  Bucket *b;
+    Bucket *b;
 
-  *offset = (*bucket)->len - 1;
-  while ((*bucket)->len < 1)
-    {
-      b=(*bucket)->next;
-      if (b==NULL) return 0;
-      Py_INCREF(b);
-      PER_ALLOW_DEACTIVATION((*bucket));
-      ASSIGNB((*bucket), b);
-      UNLESS (PER_USE(*bucket)) return -1;
-      *offset = 0;
+    *offset = (*bucket)->len - 1;
+    while ((*bucket)->len < 1) {
+	b = (*bucket)->next;
+	if (b == NULL) 
+	    return 0;
+	Py_INCREF(b);
+	PER_ALLOW_DEACTIVATION((*bucket));
+	Py_DECREF((*bucket));
+	*bucket = b;
+	if (!PER_USE(*bucket)) {
+	    Py_DECREF(*bucket);
+	    return -1;
+	}
+	*offset = 0;
     }
-  return 1;
+    Py_DECREF(*bucket);
+    return 1;
 }
 
 static int 
 lastBucketOffset(Bucket **bucket, int *offset, Bucket *firstbucket, int i)
 {
-  Bucket *b;
+    Bucket *b;
 
-  *offset = (*bucket)->len - 1;
-  while ((*bucket)->len < 1)
-    {
-      b=PreviousBucket((*bucket), firstbucket, i);
-      if (b==NULL) return 0;
-      PER_ALLOW_DEACTIVATION((*bucket));
-      ASSIGNB((*bucket), b);
-      UNLESS (PER_USE(*bucket)) return -1;
-      *offset = (*bucket)->len - 1;
+    *offset = (*bucket)->len - 1;
+    while ((*bucket)->len < 1) {
+	b = PreviousBucket((*bucket), firstbucket, i);
+	if (b == NULL) 
+	    return 0;
+	PER_ALLOW_DEACTIVATION((*bucket));
+	Py_DECREF(*bucket);
+	*bucket = b;
+	if (!PER_USE(*bucket)) {
+	    /* XXX Doesn't this leak a reference to a bucket? */
+	    return -1;
+	}
+	*offset = (*bucket)->len - 1;
     }
-  return 1;
+    Py_DECREF(*bucket);
+    return 1;
 }
 
 static void *