From: <pj...@us...> - 2009-05-04 06:07:20
|
Revision: 6291 http://jython.svn.sourceforge.net/jython/?rev=6291&view=rev Author: pjenvey Date: 2009-05-04 06:07:19 +0000 (Mon, 04 May 2009) Log Message: ----------- o sequence cmp and generic __contains__ should compare identity for equality -- so change them to use equals (analogous to CPython's PyObject_RichCompareBool) instead of plain _eq (analogous to its PyObject_RichCompare) o fix list/tuple equals on their subclasses Modified Paths: -------------- trunk/jython/src/org/python/core/PyList.java trunk/jython/src/org/python/core/PyObject.java trunk/jython/src/org/python/core/PySequence.java trunk/jython/src/org/python/core/PyTuple.java trunk/jython/src/org/python/modules/_collections/PyDeque.java Added Paths: ----------- trunk/jython/Lib/test/test_seq_jy.py Added: trunk/jython/Lib/test/test_seq_jy.py =================================================================== --- trunk/jython/Lib/test/test_seq_jy.py (rev 0) +++ trunk/jython/Lib/test/test_seq_jy.py 2009-05-04 06:07:19 UTC (rev 6291) @@ -0,0 +1,60 @@ +"""Additional seq_tests + +Made for Jython. +""" +import unittest +from collections import deque +from test import test_support + +class SeqTestCase(unittest.TestCase): + + types2test = list, tuple, deque + + def test_seq_item_equality(self): + eq_called = [] + class Foo(object): + def __eq__(self, other): + eq_called.append(other) + return False + for type2test in self.types2test: + foo = Foo() + seq1 = type2test([foo]) + self.assertEqual(seq1, seq1) + self.assertEqual(cmp(seq1, seq1), 0) + seq2 = type2test([foo]) + self.assertEqual(seq1, seq2) + self.assertEqual(cmp(seq1, seq2), 0) + self.assertTrue(foo in seq1) + self.assertFalse(eq_called) + + def test_seq_subclass_equality(self): + # Various combinations of PyObject._eq, overriden Object.equals, + # and cmp implementations + for type2test in self.types2test: + class Foo(type2test): + def __eq__(self, other): + return False + l = type2test(['bar', 'baz']) + foo = Foo(l) + self.assertNotEqual(l, foo) + self.assertEqual(cmp(l, foo), 1) + self.assertEqual(cmp(foo, foo), 0) + + seqs1 = type2test([l, foo]) + seqs2 = type2test([l, foo]) + self.assertEqual(seqs1, seqs1) + self.assertEqual(seqs1, seqs2) + self.assertEqual(cmp(seqs1, seqs2), 0) + self.assertTrue(foo in seqs1) + if hasattr(seqs1, 'count'): + self.assertTrue(seqs1.count(foo), 1) + if hasattr(seqs1, 'index'): + self.assertEqual(seqs1.index(foo), 1) + + +def test_main(): + test_support.run_unittest(SeqTestCase) + + +if __name__ == "__main__": + test_main() Modified: trunk/jython/src/org/python/core/PyList.java =================================================================== --- trunk/jython/src/org/python/core/PyList.java 2009-05-03 21:39:08 UTC (rev 6290) +++ trunk/jython/src/org/python/core/PyList.java 2009-05-04 06:07:19 UTC (rev 6291) @@ -946,13 +946,16 @@ } @Override - public synchronized boolean equals(Object o) { - if (o instanceof PyList) { - return (((PyList) o).list.equals(list)); - } else if (o instanceof List && !(o instanceof PyTuple)) { - List oList = (List) o; - return oList.equals(list); + public synchronized boolean equals(Object other) { + if (this == other) { + return true; } + if (other instanceof PyList) { + return _eq((PyList)other).__nonzero__(); + } else if (other instanceof List && !(other instanceof PyTuple)) { + List otherList = (List)other; + return list.equals(otherList); + } return false; } Modified: trunk/jython/src/org/python/core/PyObject.java =================================================================== --- trunk/jython/src/org/python/core/PyObject.java 2009-05-03 21:39:08 UTC (rev 6290) +++ trunk/jython/src/org/python/core/PyObject.java 2009-05-04 06:07:19 UTC (rev 6291) @@ -1676,7 +1676,7 @@ final boolean object___contains__(PyObject o) { for (PyObject item : asIterable()) { - if (o._eq(item).__nonzero__()) { + if (o.equals(item)) { return true; } } Modified: trunk/jython/src/org/python/core/PySequence.java =================================================================== --- trunk/jython/src/org/python/core/PySequence.java 2009-05-03 21:39:08 UTC (rev 6290) +++ trunk/jython/src/org/python/core/PySequence.java 2009-05-04 06:07:19 UTC (rev 6291) @@ -219,7 +219,7 @@ ol2 = o2.__len__(); } for (int i = 0; i < ol1 && i < ol2; i++) { - if (!o1.__getitem__(i)._eq(o2.__getitem__(i)).__nonzero__()) { + if (!o1.__getitem__(i).equals(o2.__getitem__(i))) { return i; } } Modified: trunk/jython/src/org/python/core/PyTuple.java =================================================================== --- trunk/jython/src/org/python/core/PyTuple.java 2009-05-03 21:39:08 UTC (rev 6290) +++ trunk/jython/src/org/python/core/PyTuple.java 2009-05-04 06:07:19 UTC (rev 6291) @@ -470,12 +470,15 @@ } @Override - public boolean equals(Object o) { - if (o instanceof PyTuple) { - return Arrays.equals(array, ((PyTuple) o).array); - } else if (o instanceof List && !(o instanceof PyList)) { - return o.equals(this); + public boolean equals(Object other) { + if (this == other) { + return true; } + if (other instanceof PyTuple) { + return _eq((PyTuple)other).__nonzero__(); + } else if (other instanceof List && !(other instanceof PyList)) { + return other.equals(this); + } return false; } Modified: trunk/jython/src/org/python/modules/_collections/PyDeque.java =================================================================== --- trunk/jython/src/org/python/modules/_collections/PyDeque.java 2009-05-03 21:39:08 UTC (rev 6290) +++ trunk/jython/src/org/python/modules/_collections/PyDeque.java 2009-05-04 06:07:19 UTC (rev 6291) @@ -443,7 +443,7 @@ ol2 = o2.__len__(); } for (int i = 0 ; i < ol1 && i < ol2; i++) { - if (!o1.__getitem__(i)._eq(o2.__getitem__(i)).__nonzero__()) { + if (!o1.__getitem__(i).equals(o2.__getitem__(i))) { return i; } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |