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

Jeremy Hylton jeremy@zope.com
Mon, 24 Jun 2002 15:41:00 -0400


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

Modified Files:
	cPersistence.c 
Log Message:
The default __setstate__() now requires that its argument be a dictionary.

This change removes a chunk of unused code that attempted to set the
state from mapping-like objects.  Also eliminates the need for
_PyPersist_MappingKeys().


=== Zope3/lib/python/Persistence/cPersistence.c 1.6 => 1.7 ===
 */
 
-/* A helper for setstate that is more efficient than PyMapping_Keys().
- */
-
-PyObject *
-_PyPersist_MappingKeys(PyObject *map)
-{
-    static PyObject *s_keys = NULL;
-    PyObject *meth, *args, *keys;
-    if (s_keys == NULL) 
-	s_keys = PyString_InternFromString("keys");
-    meth = PyObject_GetAttr(map, s_keys);
-    if (meth == NULL)
-	return NULL;
-    args = PyTuple_New(0);
-    if (args == NULL) {
-	Py_DECREF(meth);
-	return NULL;
-    }
-    keys = PyObject_Call(meth, args, NULL);
-    Py_DECREF(args);
-    Py_DECREF(meth);
-    return keys;
-}
-
 /* A helper function that registers a persistent object with its data
    manager.
 */
@@ -263,6 +239,8 @@
 {
     PyObject **pdict;
     PyObject *dict;
+    PyObject *k, *v;
+    int pos = 0;
 
     if (state == Py_None) {
 	Py_INCREF(Py_None);
@@ -276,65 +254,22 @@
 	    return NULL;
     }
     dict = *pdict;
+    
+    if (!PyDict_Check(state)) {
+	PyErr_SetString(PyExc_TypeError, "state must be a dictionary");
+	return NULL;
+    }
 
-    if (PyDict_Check(state)) {
-	PyObject *k, *v;
-	int pos = 0;
-
-	while (PyDict_Next(state, &pos, &k, &v)) {
-	    if (PyString_Check(k)) {
-		char *attrname = PyString_AS_STRING(k);
-		if (strncmp(attrname, "_p_", 3) == 0)
-		    continue;
-	    }
-	    if (PyDict_SetItem(dict, k, v) < 0)
-		return NULL;
+    while (PyDict_Next(state, &pos, &k, &v)) {
+	if (PyString_Check(k)) {
+	    char *attrname = PyString_AS_STRING(k);
+	    if (strncmp(attrname, "_p_", 3) == 0)
+		continue;
 	}
-    } else {
-	/* If it's not a dict, it may some other kind of mapping. 
-	   The old cPersistence supported this code, but I'm not sure
-	   if it is necessary for the new Persistence machinery.
-	 */ 
-	
-	PyObject *keys, *key = NULL, *item = NULL;
-	int i, size;
-
-	keys = _PyPersist_MappingKeys(state);
-	if (keys == NULL)
+	if (PyDict_SetItem(dict, k, v) < 0)
 	    return NULL;
-	size = PyObject_Length(keys);
-	
-	for (i = 0; i < size; i++) {
-	    key = PySequence_GetItem(keys, i);
-	    if (key == NULL)
-		goto error;
-	    if (PyString_Check(key)) {
-		char *attrname = PyString_AS_STRING(key);
-		if (strncmp(attrname, "_p_", 3) == 0) {
-		    Py_DECREF(key);
-		    continue;
-		}
-	    }
-	    item = PyObject_GetItem(state, key);
-	    if (item == NULL)
-		goto error;
-	    if (PyDict_SetItem(dict, key, item) < 0)
-		goto error;
-	    Py_DECREF(key);
-	    Py_DECREF(item);
-	    item = NULL;
-	}
-
-	Py_DECREF(keys);
-	Py_INCREF(Py_None);
-	return Py_None;
-
-    error:
-	Py_DECREF(keys);
-	Py_XDECREF(key);
-	Py_XDECREF(item);
-	return NULL;
     }
+
     Py_INCREF(Py_None);
     return Py_None;
 }