Commit 02e79f40 authored by Jeremy Hylton's avatar Jeremy Hylton

Simplify _BTree_setstate() based on changes in Zope3.

parent 8cd1aea1
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
****************************************************************************/ ****************************************************************************/
#define BTREETEMPLATE_C "$Id: BTreeTemplate.c,v 1.62 2002/06/18 22:56:01 tim_one Exp $\n" #define BTREETEMPLATE_C "$Id: BTreeTemplate.c,v 1.63 2002/06/19 20:20:07 jeremy Exp $\n"
/* /*
** _BTree_get ** _BTree_get
...@@ -763,93 +763,90 @@ err: ...@@ -763,93 +763,90 @@ err:
static int static int
_BTree_setstate(BTree *self, PyObject *state, int noval) _BTree_setstate(BTree *self, PyObject *state, int noval)
{ {
PyObject *items, *firstbucket=0; PyObject *items, *firstbucket=0;
BTreeItem *d; BTreeItem *d;
int len, l, i, copied=1; int len, l, i, copied=1;
if (_BTree_clear(self) < 0) return -1; if (_BTree_clear(self) < 0)
return -1;
if (state != Py_None) /* The state of a BTree can be one of the following:
{ None -- an empty BTree
A one-tuple -- a single bucket btree
A two-tuple -- a BTree with more than one bucket
See comments for BTree_getstate() for the details.
*/
if (!PyArg_ParseTuple(state,"O|O",&items, &firstbucket)) if (state == Py_None)
return -1; return 0;
if ((len=PyTuple_Size(items)) < 0) return -1; if (!PyArg_ParseTuple(state, "O|O:__setstate__", &items, &firstbucket))
len=(len+1)/2; return -1;
assert(len > 0);
assert(self->size == 0); /* XXX we called _BTree_clear() above! */
assert(self->data == NULL); /* ditto */
if (len > self->size)
{
UNLESS (d = PyRealloc(self->data, sizeof(BTreeItem)*len)) return -1;
self->data = d;
self->size = len;
}
for (i = 0, d = self->data, l = 0; i < len; i++, d++) len = PyTuple_Size(items);
{ if (len < 0)
if (i) return -1;
{ len = (len + 1) / 2;
COPY_KEY_FROM_ARG(d->key, PyTuple_GET_ITEM(items,l), copied); assert(len > 0);
l++;
UNLESS (copied) return -1; assert(self->size == 0); /* XXX we called _BTree_clear() above! */
INCREF_KEY(d->key); assert(self->data == NULL); /* ditto */
} self->data = PyMalloc(sizeof(BTreeItem) * len);
d->child = SIZED(PyTuple_GET_ITEM(items,l)); if (self->data == NULL)
if (PyTuple_Check(d->child)) return -1;
{ self->size = len;
if (noval)
{ for (i = 0, d = self->data, l = 0; i < len; i++, d++) {
d->child = SIZED(PyObject_CallObject(OBJECT(&SetType), PyObject *v;
NULL)); if (i) { /* skip the first key slot */
UNLESS (d->child) return -1; COPY_KEY_FROM_ARG(d->key, PyTuple_GET_ITEM(items,l), copied);
if (_set_setstate(BUCKET(d->child), l++;
PyTuple_GET_ITEM(items,l)) if (!copied)
< 0) return -1; return -1;
} INCREF_KEY(d->key);
else }
{ v = PyTuple_GET_ITEM(items, l);
d->child = SIZED(PyObject_CallObject(OBJECT(&BucketType), if (PyTuple_Check(v)) {
NULL)); /* Handle the special case in __getstate__() for a BTree
UNLESS (d->child) return -1; with a single bucket. */
if (_bucket_setstate(BUCKET(d->child), if (noval) {
PyTuple_GET_ITEM(items,l)) d->child = SIZED(PyObject_CallObject(OBJECT(&SetType),
< 0) return -1; NULL));
} UNLESS (d->child) return -1;
} if (_set_setstate(BUCKET(d->child), v) < 0)
else return -1;
{ }
Py_INCREF(d->child); else {
} d->child = SIZED(PyObject_CallObject(OBJECT(&BucketType),
l++; NULL));
} UNLESS (d->child) return -1;
if (_bucket_setstate(BUCKET(d->child), v) < 0)
return -1;
}
}
else {
d->child = (Sized *)v;
Py_INCREF(v);
}
l++;
}
assert(len > 0); if (!firstbucket)
if (len) firstbucket = OBJECT(self->data->child);
{
if (! firstbucket)
firstbucket = OBJECT(self->data->child);
UNLESS (ExtensionClassSubclassInstance_Check( if (!ExtensionClassSubclassInstance_Check(
firstbucket, firstbucket, noval ? &SetType : &BucketType)) {
noval ? &SetType : &BucketType)) PyErr_SetString(PyExc_TypeError, "No firstbucket in non-empty BTree");
{ return -1;
PyErr_SetString(PyExc_TypeError, }
"No firstbucket in non-empty BTree");
return -1;
}
self->firstbucket = BUCKET(firstbucket); self->firstbucket = BUCKET(firstbucket);
Py_INCREF(firstbucket); Py_INCREF(firstbucket);
assert(firstbucket->ob_refcnt > 1); assert(firstbucket->ob_refcnt > 1);
}
self->len = len; self->len = len;
}
return 0; return 0;
} }
static PyObject * static PyObject *
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment