[Zodb-checkins] CVS: Zope/lib/python/BTrees - BucketTemplate.c:1.17.2.3
Jeremy Hylton
jeremy@zope.com
Fri, 2 Nov 2001 12:26:45 -0500
Update of /cvs-repository/Zope/lib/python/BTrees
In directory cvs.zope.org:/tmp/cvs-serv21942
Modified Files:
Tag: Zope-2_4-branch
BucketTemplate.c
Log Message:
Fix memory leak.
>From the trunk.
=== Zope/lib/python/BTrees/BucketTemplate.c 1.17.2.2 => 1.17.2.3 ===
}
+/**
+ ** Mapping_update()
+ **
+ ** Accepts a sequence of 2-tuples or any object with an items()
+ ** method that returns a sequence of 2-tuples.
+ **
+ */
+
static PyObject *
Mapping_update(PyObject *self, PyObject *args)
{
- PyObject *seq=0, *o, *t, *v, *tb, *k;
+ PyObject *seq=0, *o, *t, *v, *tb, *k, *items = NULL;
int i, ind;
UNLESS(PyArg_ParseTuple(args, "|O:update", &seq)) return NULL;
- if (seq)
+ if (!seq)
+ {
+ Py_INCREF(Py_None);
+ return Py_None;
+ }
+
+ if (!PySequence_Check(seq))
{
+ items = PyObject_GetAttr(seq, items_str);
+ UNLESS(items) return NULL;
+ ASSIGN(items, PyObject_CallObject(items, NULL));
+ UNLESS(items) return NULL;
+ /* items is DECREFed on exit, seq is not */
+ seq = items;
+ }
- if (PySequence_Check(seq))
- {
- Py_INCREF(seq);
- }
+ for (i=0; ; i++)
+ {
+ o = PySequence_GetItem(seq, i);
+ UNLESS (o)
+ {
+ PyErr_Fetch(&t, &v, &tb);
+ if (t != PyExc_IndexError)
+ {
+ PyErr_Restore(t, v, tb);
+ goto err;
+ }
+ Py_XDECREF(t);
+ Py_XDECREF(v);
+ Py_XDECREF(tb);
+ break;
+ }
+ ind = PyArg_ParseTuple(o, "OO;items must be 2-item tuples", &k, &v);
+ if (ind)
+ ind = PyObject_SetItem(self, k, v);
else
- {
- seq=PyObject_GetAttr(seq, items_str);
- UNLESS(seq) return NULL;
- ASSIGN(seq, PyObject_CallObject(seq, NULL));
- UNLESS(seq) return NULL;
- }
-
- for (i=0; ; i++)
- {
- UNLESS (o=PySequence_GetItem(seq, i))
- {
- PyErr_Fetch(&t, &v, &tb);
- if (t != PyExc_IndexError)
- {
- PyErr_Restore(t, v, tb);
- goto err;
- }
- Py_XDECREF(t);
- Py_XDECREF(v);
- Py_XDECREF(tb);
- break;
- }
- ind=PyArg_ParseTuple(o, "OO;items must be 2-item tuples", &k, &v);
- if (ind)
- ind = PyObject_SetItem(self, k, v);
- else
- ind=-1;
- Py_DECREF(o);
- if (ind < 0) goto err;
- }
+ ind = -1;
+ Py_DECREF(o);
+ if (ind < 0) goto err;
}
+ Py_XDECREF(items);
Py_INCREF(Py_None);
return Py_None;
err:
- Py_DECREF(seq);
+ Py_XDECREF(items);
return NULL;
}