[Zope-Checkins] CVS: Zope3/lib/python/Persistence/BTrees - BucketTemplate.c:1.5

Jeremy Hylton jeremy@zope.com
Wed, 12 Jun 2002 19:50:08 -0400


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

Modified Files:
	BucketTemplate.c 
Log Message:
Change bucket_repr() to get the name of the class from self->ob_type->tp_name.

If the bucket type is a subclass of a regular bucket type, it's
important to have the subclass's name in the repr.

XXX Is this code too complicated?  Perhaps it should always allocated
a buffer dynamically.


=== Zope3/lib/python/Persistence/BTrees/BucketTemplate.c 1.4 => 1.5 ===
 bucket_repr(Bucket *self)
 {
-  static PyObject *format;
-  PyObject *r, *t;
+    PyObject *i, *r;
+    char repr[10000];
+    int rv;
 
-  if (format == NULL) {
-      format = PyString_FromString(MOD_NAME_PREFIX "Bucket(%s)");
-      if (format == NULL)
-	  return NULL;
-  }
-  UNLESS (t=PyTuple_New(1)) return NULL;
-  UNLESS (r=bucket_items(self,NULL)) goto err;
-  PyTuple_SET_ITEM(t,0,r);
-  r=t;
-  ASSIGN(r,PyString_Format(format,r));
-  return r;
-err:
-  Py_DECREF(t);
-  return NULL;
+    i = bucket_items(self, NULL);
+    if (!i)
+	return NULL;
+    r = PyObject_Repr(i);
+    Py_DECREF(i);
+    if (!r) {
+	return NULL;
+    }
+    rv = PyOS_snprintf(repr, sizeof(repr), 
+		       "%s(%s)", self->ob_type->tp_name,
+		       PyString_AS_STRING(r));
+    if (rv > 0 && rv < sizeof(repr)) {
+	Py_DECREF(r);
+	return PyString_FromStringAndSize(repr, strlen(repr));
+    } 
+    else {
+	/* The static buffer wasn't big enough */
+	int size;
+	PyObject *s;
+
+	/* 3 for the parens and the null byte */
+	size = strlen(self->ob_type->tp_name) + PyString_GET_SIZE(r) + 3;
+	s = PyString_FromStringAndSize(NULL, size);
+	if (!s) {
+	    Py_DECREF(r);
+	    return r;
+	}
+	PyOS_snprintf(PyString_AS_STRING(s), size,
+		      "%s(%s)", self->ob_type->tp_name, PyString_AS_STRING(r));
+	Py_DECREF(r);
+	return s;
+    }
 }
 
 static PyTypeObject BucketType = {