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

rhettinger at users.sourceforge.net rhettinger at users.sourceforge.net
Thu Nov 13 12:47:30 EST 2003


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

Modified Files:
	setobject.c test_set.py 
Log Message:
Add __eq__() and  __ne__().  Test set of frozen sets.

Index: setobject.c
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/setobj/setobject.c,v
retrieving revision 1.5
retrieving revision 1.6
diff -C2 -d -r1.5 -r1.6
*** setobject.c	13 Nov 2003 17:14:37 -0000	1.5
--- setobject.c	13 Nov 2003 17:47:28 -0000	1.6
***************
*** 17,21 ****
  
  #define DICT_CONTAINS(d, k)  d->ob_type->tp_as_sequence->sq_contains(d, k)
! 
  
  /* set object **********************************************************/
--- 17,21 ----
  
  #define DICT_CONTAINS(d, k)  d->ob_type->tp_as_sequence->sq_contains(d, k)
! #define IS_SET(so)	(so->ob_type == &set_type || so->ob_type == &frozenset_type)
  
  /* set object **********************************************************/
***************
*** 318,321 ****
--- 318,339 ----
  }
  
+ static PyObject *
+ set_richcompare(PyObject *v, PyObject *w, int op)
+ {
+ 	PyObject *res;
+ 		
+ 	if (op == Py_EQ && ! IS_SET(w))
+ 		res = Py_False;
+ 	else if (op == Py_NE && ! IS_SET(w))
+ 		res = Py_True;
+ 	else if (op == Py_EQ || op == Py_NE) 
+ 		res = PyObject_RichCompare(((setobject *)v)->data, 
+ 			((setobject *)w)->data, op);
+ 	else
+ 		res = Py_NotImplemented;
+ 	Py_INCREF(res);
+ 	return res;
+  }
+ 
  static PySequenceMethods set_as_sequence = {
  	(inquiry)set_len,		/* sq_length */
***************
*** 379,383 ****
  	(traverseproc)set_traverse,	/* tp_traverse */
  	0,				/* tp_clear */
! 	0,				/* tp_richcompare */
  	0,				/* tp_weaklistoffset */
  	(getiterfunc)set_iter,		/* tp_iter */
--- 397,401 ----
  	(traverseproc)set_traverse,	/* tp_traverse */
  	0,				/* tp_clear */
! 	(richcmpfunc)set_richcompare,	/* tp_richcompare */
  	0,				/* tp_weaklistoffset */
  	(getiterfunc)set_iter,		/* tp_iter */

Index: test_set.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/setobj/test_set.py,v
retrieving revision 1.5
retrieving revision 1.6
diff -C2 -d -r1.5 -r1.6
*** test_set.py	13 Nov 2003 17:14:37 -0000	1.5
--- test_set.py	13 Nov 2003 17:47:28 -0000	1.6
***************
*** 61,64 ****
--- 61,71 ----
          self.assertEqual(hash(frozenset('abcdeb')), hash(frozenset('ebecda')))
  
+ class TestSetsOfSets(unittest.TestCase):
+ 
+     def testSetOfFrozensets(set):
+         t = map(frozenset, ['abcdef', 'bcd', 'bdcb', 'fed', 'fedccba'])
+         s = set(t)
+         self.assertEqual(len(s), 3)
+ 
  def test_main(verbose=None):
      from test import test_sets





More information about the Python-checkins mailing list