[Zope-Checkins] CVS: Zope3/lib/python/Persistence - cPersistence.c:1.1.2.2

Jeremy Hylton jeremy@zope.com
Wed, 13 Feb 2002 08:26:14 -0500


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

Modified Files:
      Tag: Zope-3x-branch
	cPersistence.c 
Log Message:
Add module init function.

Make sure _PyPersist_Register() and _PyPersist_Load() handle an object
with no data manager without error.



=== Zope3/lib/python/Persistence/cPersistence.c 1.1.2.1 => 1.1.2.2 ===
     PyObject *meth, *arg, *result;
 
+    if (self->po_dm == NULL) {
+	Py_INCREF(Py_None);
+	return Py_None;
+    }
     if (s_register == NULL) 
 	s_register = PyString_InternFromString("register");
-    meth = PyObject_GetAttr((PyObject *)self, s_register);
+    meth = PyObject_GetAttr((PyObject *)self->po_dm, s_register);
     if (meth == NULL)
 	return NULL;
     arg = PyTuple_New(1);
@@ -67,9 +71,13 @@
     static PyObject *s_setstate = NULL;
     PyObject *meth, *arg, *result;
 
+    if (self->po_dm == NULL) {
+	Py_INCREF(Py_None);
+	return Py_None;
+    }
     if (s_setstate == NULL) 
 	s_setstate = PyString_InternFromString("setstate");
-    meth = PyObject_GetAttr((PyObject *)self, s_setstate);
+    meth = PyObject_GetAttr((PyObject *)self->po_dm, s_setstate);
     if (meth == NULL)
 	return NULL;
     arg = PyTuple_New(1);
@@ -312,6 +320,8 @@
     {"_p_jar", T_OBJECT, offsetof(PyPersistObject, po_dm)},
     {"_p_oid", T_OBJECT, offsetof(PyPersistObject, po_oid)},
     {"_p_serial", T_OBJECT, offsetof(PyPersistObject, po_serial)},
+    /* XXX should state should be exposed? */
+    {"_p_state", T_INT, offsetof(PyPersistObject, po_state)},
 };
 
 /* The crucial methods for a persistent object are the tp_getattr and
@@ -523,7 +533,30 @@
 	0,					/* tp_dictoffset */
 	0,					/* tp_init */
 	0,					/* tp_alloc */
-	0,					/* tp_new */
+	PyType_GenericNew,			/* tp_new */
 };
 
+static PyMethodDef PyPersist_methods[] = {
+    {NULL, NULL}
+};
+
+void 
+initcPersistence(void)
+{
+    PyObject *m, *d;
 
+    if (PyType_Ready(&persist_type) < 0)
+	return;
+
+    m = Py_InitModule3("cPersistence", PyPersist_methods, 
+		       PyPersist_doc_string);
+    if (m == NULL)
+	return;
+    d = PyModule_GetDict(m);
+    if (d == NULL)
+	return;
+
+    Py_INCREF(&persist_type);
+    if (PyDict_SetItemString(d, "Persistent", (PyObject *)&persist_type) < 0)
+	return;
+}