[Zodb-checkins] CVS: ZODB3/Persistence - cPersistence.c:1.72.8.12

Jeremy Hylton jeremy at zope.com
Thu Jul 3 18:40:16 EDT 2003


Update of /cvs-repository/ZODB3/Persistence
In directory cvs.zope.org:/tmp/cvs-serv24006

Modified Files:
      Tag: zodb33-devel-branch
	cPersistence.c 
Log Message:
Handle instances with no dict allocated in setstate and getstate.


=== ZODB3/Persistence/cPersistence.c 1.72.8.11 => 1.72.8.12 ===
--- ZODB3/Persistence/cPersistence.c:1.72.8.11	Thu Jul  3 17:07:44 2003
+++ ZODB3/Persistence/cPersistence.c	Thu Jul  3 17:40:11 2003
@@ -262,16 +262,6 @@
     return Py_None;
 }
 
-/* Load the object's state if necessary and become sticky */
-static int
-Per_setstate(cPersistentObject *self)
-{
-    if (!unghostify(self))
-        return -1;
-    self->state = cPersistent_STICKY_STATE;
-    return 0;
-}
-
 static PyObject *
 Per__getstate__(cPersistentObject *self, PyObject *args)
 {
@@ -284,7 +274,7 @@
         return NULL;
 
     dictptr = _PyObject_GetDictPtr((PyObject *)self);
-    if (dictptr) {
+    if (dictptr && *dictptr) {
         PyObject *k, *v;
         int pos;
         char *ck;
@@ -322,48 +312,51 @@
 static PyObject *
 Per__setstate__(cPersistentObject *self, PyObject *args)
 {
-  PyObject *__dict__, *v, *keys=0, *key=0, *e=0;
-  int l, i;
+    PyObject **dictptr;
+    PyObject *__dict__, *v, *keys=0, *key=0, *e=0;
+    int l, i;
 
-  if (_PyObject_GetDictPtr((PyObject *)self)) {
-       UNLESS(PyArg_ParseTuple(args, "O", &v)) return NULL;
-       if (v!=Py_None)
-	 {
-	     PyObject **dictptr = _PyObject_GetDictPtr((PyObject *)self);
-	     assert(dictptr);
-	     __dict__ = *dictptr;
-	   
-	   if (PyDict_Check(v))
-	     {
-	       for(i=0; 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;
+    dictptr = _PyObject_GetDictPtr((PyObject *)self);
+    if (dictptr) {
+	UNLESS(PyArg_ParseTuple(args, "O", &v)) return NULL;
+	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;
 }  
 
 
@@ -759,6 +752,16 @@
 /* -------------------------------------------------------- */
 
 typedef int (*intfunctionwithpythonarg)(PyObject*);
+
+/* Load the object's state if necessary and become sticky */
+static int
+Per_setstate(cPersistentObject *self)
+{
+    if (!unghostify(self))
+        return -1;
+    self->state = cPersistent_STICKY_STATE;
+    return 0;
+}
 
 static cPersistenceCAPIstruct
 truecPersistenceCAPI = {




More information about the Zodb-checkins mailing list