[Python-checkins] CVS: python/dist/src/Objects structseq.c,1.3,1.4

Michael Hudson mwh@users.sourceforge.net
Tue, 05 Mar 2002 05:28:00 -0800


Update of /cvsroot/python/python/dist/src/Objects
In directory usw-pr-cvs1:/tmp/cvs-serv27068/Objects

Modified Files:
	structseq.c 
Log Message:
A fix & test for 

[ 496873 ] structseqs unpicklable

by adding a __reduce__ method to structseqs.

Will also commit this to the 2.2.1 branch momentarily.



Index: structseq.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/structseq.c,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** structseq.c	28 Nov 2001 20:56:44 -0000	1.3
--- structseq.c	5 Mar 2002 13:27:57 -0000	1.4
***************
*** 189,192 ****
--- 189,213 ----
  }
  
+ static PyObject *
+ structseq_reduce(PyStructSequence* self)
+ {
+ 	PyObject* tup;
+ 	long n_fields;
+ 	int i;
+ 	
+ 	n_fields = REAL_SIZE(self);
+ 	tup = PyTuple_New(n_fields);
+ 	if (!tup) {
+ 		return NULL;
+ 	}
+ 
+ 	for (i = 0; i < n_fields; i++) {
+ 		Py_INCREF(self->ob_item[i]);
+ 		PyTuple_SET_ITEM(tup, i, self->ob_item[i]);
+ 	}
+ 	
+ 	return Py_BuildValue("(O(O))", self->ob_type, tup);
+ }
+ 
  static PySequenceMethods structseq_as_sequence = {
  	(inquiry)structseq_length,
***************
*** 200,203 ****
--- 221,230 ----
  };
  
+ static PyMethodDef structseq_methods[] = {
+ 	{"__reduce__", (PyCFunction)structseq_reduce, 
+ 	 METH_NOARGS, NULL},
+ 	{NULL, NULL}
+ };
+ 
  static PyTypeObject _struct_sequence_template = {
  	PyObject_HEAD_INIT(&PyType_Type)
***************
*** 229,233 ****
  	0,					/* tp_iter */
  	0,					/* tp_iternext */
! 	0,	             			/* tp_methods */
          NULL,			             	/* tp_members */
  	0,			          	/* tp_getset */
--- 256,260 ----
  	0,					/* tp_iter */
  	0,					/* tp_iternext */
! 	structseq_methods,      		/* tp_methods */
          NULL,			             	/* tp_members */
  	0,			          	/* tp_getset */
***************
*** 283,285 ****
--- 310,314 ----
  	PyDict_SetItemString(dict, real_length_key, 
  		       PyInt_FromLong((long) n_members));
+ 	PyDict_SetItemString(dict, "__safe_for_unpickling__", 
+ 		       PyInt_FromLong(1));
  }