[Python-checkins] python/nondist/sandbox/setobj setobject.c, 1.11, 1.12 test_set.py, 1.11, 1.12

rhettinger at users.sourceforge.net rhettinger at users.sourceforge.net
Fri Nov 14 17:07:15 EST 2003


Update of /cvsroot/python/python/nondist/sandbox/setobj
In directory sc8-pr-cvs1:/tmp/cvs-serv27514

Modified Files:
	setobject.c test_set.py 
Log Message:
Add __reduce__() for pickle support

Index: setobject.c
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/setobj/setobject.c,v
retrieving revision 1.11
retrieving revision 1.12
diff -C2 -d -r1.11 -r1.12
*** setobject.c	14 Nov 2003 13:54:26 -0000	1.11
--- setobject.c	14 Nov 2003 22:07:13 -0000	1.12
***************
*** 14,18 ****
  */
  
! /* Fast dictionary access macros */ 
  
  #define DICT_CONTAINS(d, k)  (d->ob_type->tp_as_sequence->sq_contains(d, k))
--- 14,18 ----
  */
  
! /* Fast access macros */ 
  
  #define DICT_CONTAINS(d, k)  (d->ob_type->tp_as_sequence->sq_contains(d, k))
***************
*** 738,741 ****
--- 738,756 ----
  PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.");
  
+ PyObject *
+ set_reduce(setobject *so)
+ {
+ 	PyObject *keys, *args, *result;
+ 
+ 	keys = PyDict_Keys(so->data);
+ 	args = PyTuple_Pack(1, keys);
+ 	result = PyTuple_Pack(2, so->ob_type, args);
+ 	Py_DECREF(args);
+ 	Py_DECREF(keys);
+ 	return result;
+ }
+ 
+ PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
+ 
  static PySequenceMethods set_as_sequence = {
  	(inquiry)set_len,		/* sq_length */
***************
*** 776,779 ****
--- 791,796 ----
  	{"pop",		(PyCFunction)set_pop,		METH_NOARGS,
  	 pop_doc},
+ 	{"__reduce__",	(PyCFunction)set_reduce,	METH_NOARGS,
+ 	 reduce_doc},
  	{"remove",	(PyCFunction)set_remove,	METH_O,
  	 remove_doc},
***************
*** 892,895 ****
--- 909,914 ----
  	{"issuperset",(PyCFunction)set_issuperset,	METH_O,
  	 issuperset_doc},
+ 	{"__reduce__",	(PyCFunction)set_reduce,	METH_NOARGS,
+ 	 reduce_doc},
  	{"symmetric_difference",(PyCFunction)set_symmetric_difference,	METH_O,
  	 symmetric_difference_doc},

Index: test_set.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/setobj/test_set.py,v
retrieving revision 1.11
retrieving revision 1.12
diff -C2 -d -r1.11 -r1.12
*** test_set.py	14 Nov 2003 13:54:26 -0000	1.11
--- test_set.py	14 Nov 2003 22:07:13 -0000	1.12
***************
*** 2,5 ****
--- 2,6 ----
  import unittest
  from test import test_support
+ import pickle
  
  class TestJointOps(unittest.TestCase):
***************
*** 114,117 ****
--- 115,126 ----
          self.failIf(q > r)
          self.failIf(q >= r)
+ 
+     def test_pickling(self):
+         # Note, pickling requires set() and frozenset() to be builtins
+         __builtins__.__dict__['set'] = set
+         __builtins__.__dict__['frozenset'] = frozenset
+         p = pickle.dumps(self.s)
+         dup = pickle.loads(p)
+         self.assertEqual(self.s, dup, "%s != %s" % (self.s, dup))
  
  class TestSet(TestJointOps):





More information about the Python-checkins mailing list