[Zodb-checkins] CVS: Zope/lib/python/persistent -
cPersistence.c:1.72.8.29
Jim Fulton
cvs-admin at zope.org
Mon Nov 3 11:20:10 EST 2003
Update of /cvs-repository/Zope/lib/python/persistent
In directory cvs.zope.org:/tmp/cvs-serv19785/lib/python/persistent
Modified Files:
Tag: zodb33-devel-branch
cPersistence.c
Log Message:
Changed to use shared pickling support, providing much better
implementations of __reduce__, __getattr__, and __setattr__.
=== Zope/lib/python/persistent/cPersistence.c 1.72.8.28 => 1.72.8.29 ===
--- Zope/lib/python/persistent/cPersistence.c:1.72.8.28 Tue Oct 28 16:28:33 2003
+++ Zope/lib/python/persistent/cPersistence.c Mon Nov 3 11:20:09 2003
@@ -56,15 +56,6 @@
return 0;
}
-static PyObject *
-callmethod(PyObject *self, PyObject *name)
-{
- self=PyObject_GetAttr(self,name);
- if (self)
- ASSIGN(self,PyObject_CallObject(self,NULL));
- return self;
-}
-
static void ghostify(cPersistentObject*);
/* Load the state of the object, unghostifying it. Upon success, return 1.
@@ -218,6 +209,13 @@
return Py_None;
}
+
+#include "pickle/pickle.c"
+
+
+
+
+
/* Return the object's state, a dict or None.
If the object has no dict, it's state is None.
@@ -231,153 +229,24 @@
static PyObject *
Per__getstate__(cPersistentObject *self)
{
- PyObject **dictptr, *__dict__, *d;
- PyObject *k, *v;
- int pos = 0, copy = 0;
-
/* XXX Should it be an error to call __getstate__() on a ghost? */
if (!unghostify(self))
return NULL;
- dictptr = _PyObject_GetDictPtr((PyObject *)self);
- if (!(dictptr && *dictptr)) {
- Py_INCREF(Py_None);
- return Py_None;
- }
-
- __dict__ = *dictptr;
-
- while (PyDict_Next(__dict__, &pos, &k, &v)) {
- if (!PyString_Check(k)) {
- PyErr_Format(PyExc_TypeError,
- "expected string for attribute name, found %s",
- k->ob_type->tp_name);
- return NULL;
- }
- if (strncmp(PyString_AS_STRING(k), "_v_", 3) == 0) {
- copy = 1;
- break;
- }
- }
-
- if (copy) {
- d = PyDict_New();
- if (!d)
- return NULL;
- pos = 0;
- while (PyDict_Next(__dict__, &pos, &k, &v)) {
- if (strncmp(PyString_AS_STRING(k), "_v_", 3) == 0)
- continue;
- if (PyDict_SetItem(d, k, v) < 0) {
- Py_DECREF(d);
- return NULL;
- }
- }
- __dict__ = d;
- }
- else
- Py_INCREF(__dict__);
-
- return __dict__;
-}
-
-static PyObject *
-Per__setstate__(cPersistentObject *self, PyObject *v)
-{
- PyObject **dictptr;
- PyObject *__dict__, *keys=0, *key=0, *e=0;
- int l, i;
-
- dictptr = _PyObject_GetDictPtr((PyObject *)self);
- if (dictptr) {
- if (v != Py_None) {
- if (!*dictptr) {
- *dictptr = PyDict_New();
- if (!*dictptr)
- return NULL;
- }
- __dict__ = *dictptr;
-
- if (PyDict_Check(v)) {
- i = 0;
- while (PyDict_Next(v, &i, &key, &e)) {
- if (PyDict_SetItem(__dict__, key, e) < 0)
- return NULL;
- }
- }
- else {
- UNLESS(keys=callmethod(v,py_keys)) goto err;
- UNLESS(-1 != (l=PyObject_Length(keys))) goto err;
-
- for(i=0; i < l; i++) {
- UNLESS_ASSIGN(key,PySequence_GetItem(keys,i)) goto err;
- UNLESS_ASSIGN(e,PyObject_GetItem(v,key)) goto err;
- UNLESS(-1 != PyDict_SetItem(__dict__,key,e)) goto err;
- }
-
- Py_XDECREF(key);
- Py_XDECREF(e);
- Py_DECREF(keys);
- }
- }
- }
- Py_INCREF(Py_None);
- return Py_None;
- err:
- Py_XDECREF(key);
- Py_XDECREF(e);
- Py_XDECREF(keys);
- return NULL;
-}
-
-static PyObject *
-Per__reduce__(cPersistentObject *self)
-{
- PyObject *state, *args=NULL, *result, *__getstate__;
-
- __getstate__ = PyObject_GetAttr((PyObject*)self, py___getstate__);
- if (!__getstate__)
- return NULL;
-
- state = PyObject_CallObject(__getstate__, NULL);
- Py_DECREF(__getstate__);
- if (!state)
- return NULL;
-
- args = PyTuple_New(1);
- if (!args)
- goto err;
-
- Py_INCREF(self->ob_type);
- PyTuple_SET_ITEM(args, 0, (PyObject *)self->ob_type);
-
- result = PyTuple_New(3);
- if (!result)
- goto err;
-
- Py_INCREF(py_simple_new);
- PyTuple_SET_ITEM(result, 0, py_simple_new);
- PyTuple_SET_ITEM(result, 1, args);
- PyTuple_SET_ITEM(result, 2, state);
-
- return result;
-
- err:
- Py_DECREF(state);
- Py_XDECREF(args);
- return NULL;
+ /* XXX shouldn't we increment stickyness? */
+ return pickle___getstate__((PyObject*)self);
}
static struct PyMethodDef Per_methods[] = {
{"_p_deactivate", (PyCFunction)Per__p_deactivate, METH_NOARGS,
"_p_deactivate() -- Deactivate the object"},
- {"__reduce__", (PyCFunction)Per__reduce__, METH_NOARGS,
- "__reduce__ -- Support for traditional pickling of persistent objects"},
{"__getstate__", (PyCFunction)Per__getstate__, METH_NOARGS,
- "__getstate__() -- Return the state of the object" },
- {"__setstate__", (PyCFunction)Per__setstate__, METH_O,
- "__setstate__(v) -- Restore the saved state of the object from v" },
+ pickle___getstate__doc },
+
+ PICKLE_SETSTATE_DEF
+ PICKLE_GETNEWARGS_DEF
+ PICKLE_REDUCE_DEF
{NULL, NULL} /* sentinel */
};
@@ -838,6 +707,9 @@
initcPersistence(void)
{
PyObject *m, *s;
+
+ if (pickle_setup() < 0)
+ return;
if (init_strings() < 0)
return;
More information about the Zodb-checkins
mailing list