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

Jeremy Hylton jeremy at zope.com
Thu Jul 3 19:45:17 EDT 2003


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

Modified Files:
      Tag: zodb33-devel-branch
	cPersistence.c 
Log Message:
Rewrite __getstate__() for clarity.


=== ZODB3/Persistence/cPersistence.c 1.72.8.13 => 1.72.8.14 ===
--- ZODB3/Persistence/cPersistence.c:1.72.8.13	Thu Jul  3 18:22:05 2003
+++ ZODB3/Persistence/cPersistence.c	Thu Jul  3 18:45:10 2003
@@ -269,51 +269,67 @@
     return Py_None;
 }
 
+/* Return the object's state, a dict or None.
+
+   If the object has no dict, it's state is None.
+   Otherwise, return a dict containing all the attributes that
+   don't start with "_v_".  
+
+   The caller should not modify this dict, as it may be a reference to
+   the object's __dict__.
+*/
+
 static PyObject *
-Per__getstate__(cPersistentObject *self, PyObject *args)
+Per__getstate__(cPersistentObject *self)
 {
-    PyObject **dictptr, *__dict__, *d=0;
-
-    if (!checknoargs(args))
-        return NULL;
+    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) {
-        PyObject *k, *v;
-        int pos;
-        char *ck;
-
-	__dict__ = *dictptr;
-	  
-        for(pos=0; PyDict_Next(__dict__, &pos, &k, &v); ) {
-            if (PyString_Check(k) && (ck=PyString_AS_STRING(k)) &&
-               (*ck=='_' && ck[1]=='v' && ck[2]=='_'))
-	    {
-                if ((d=PyDict_New()) == NULL)
-                    goto err;
-                
-                for (pos=0; PyDict_Next(__dict__, &pos, &k, &v); )
-                    UNLESS(PyString_Check(k) && (ck=PyString_AS_STRING(k)) &&
-                           (*ck=='_' && ck[1]=='v' && ck[2]=='_'))
-                    {
-                        if (PyDict_SetItem(d,k,v) < 0)
-                            goto err;
-                    }
-                return d;
+    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
-        __dict__ = Py_None;
+	Py_INCREF(__dict__);
 
-    Py_INCREF(__dict__);
     return __dict__;
-  err:
-    Py_XDECREF(d);
-    return NULL;
 }  
 
 static PyObject *
@@ -372,7 +388,7 @@
    "DEPRECATED: use self._p_changed=1"},
   {"_p_deactivate",	(PyCFunction)Per__p_deactivate,	METH_VARARGS,
    "_p_deactivate(oid) -- Deactivate the object"},
-  {"__getstate__",	(PyCFunction)Per__getstate__,	METH_VARARGS,
+  {"__getstate__",	(PyCFunction)Per__getstate__,	METH_NOARGS,
    "__getstate__() -- Return the state of the object" },
   {"__setstate__",	(PyCFunction)Per__setstate__,	METH_VARARGS,
    "__setstate__(v) -- Restore the saved state of the object from v" },




More information about the Zodb-checkins mailing list