From: <pj...@us...> - 2009-05-06 05:46:17
|
Revision: 6303 http://jython.svn.sourceforge.net/jython/?rev=6303&view=rev Author: pjenvey Date: 2009-05-06 05:46:00 +0000 (Wed, 06 May 2009) Log Message: ----------- fix binops disallowing right side binop attempts (throwing their own TypeErrors), cleanup _binary_sanity_check Modified Paths: -------------- trunk/jython/Lib/test/test_set_jy.py trunk/jython/src/org/python/core/BaseSet.java Modified: trunk/jython/Lib/test/test_set_jy.py =================================================================== --- trunk/jython/Lib/test/test_set_jy.py 2009-05-06 05:25:18 UTC (rev 6302) +++ trunk/jython/Lib/test/test_set_jy.py 2009-05-06 05:46:00 UTC (rev 6303) @@ -1,11 +1,30 @@ +import unittest from test import test_support -import unittest -from java.util import Random -from javatests import PySetInJavaTest +if test_support.is_jython: + from java.util import Random + from javatests import PySetInJavaTest -class SetInJavaTest(unittest.TestCase): - "Tests for derived dict behaviour" +class SetTestCase(unittest.TestCase): + + def test_binops(self): + class Foo(object): + __rsub__ = lambda self, other: 'rsub' + __ror__ = lambda self, other: 'ror' + __rand__ = lambda self, other: 'rand' + __rxor__ = lambda self, other: 'rxor' + foo = Foo() + s = set() + self.assertEqual(s - foo, 'rsub') + self.assertEqual(s | foo, 'ror') + self.assertEqual(s & foo, 'rand') + self.assertEqual(s ^ foo, 'rxor') + + +class SetInJavaTestCase(unittest.TestCase): + + """Tests for derived dict behaviour""" + def test_using_PySet_as_Java_Set(self): PySetInJavaTest.testPySetAsJavaSet() @@ -16,20 +35,26 @@ if isinstance(v, unicode): self.assertEquals("value", v) else: - v.nextInt()#Should be a java.util.Random; ensure we can call it + # Should be a java.util.Random; ensure we can call it + v.nextInt() def test_java_accessing_items_added_in_python(self): - # Test a type that should be coerced into a Java type, a Java instance - # that should be wrapped, and a Python instance that should pass - # through as itself with str, Random and tuple respectively. + # Test a type that should be coerced into a Java type, a Java + # instance that should be wrapped, and a Python instance that + # should pass through as itself with str, Random and tuple + # respectively. s = set(["value", Random(), ("tuple", "of", "stuff")]) PySetInJavaTest.accessAndRemovePySetItems(s) - self.assertEquals(0, len(s))# Check that the Java removal affected the underlying set - + # Check that the Java removal affected the underlying set + self.assertEquals(0, len(s)) def test_main(): - test_support.run_unittest(SetInJavaTest) + tests = [SetTestCase] + if test_support.is_jython: + tests.append(SetInJavaTestCase) + test_support.run_unittest(*tests) + if __name__ == '__main__': test_main() Modified: trunk/jython/src/org/python/core/BaseSet.java =================================================================== --- trunk/jython/src/org/python/core/BaseSet.java 2009-05-06 05:25:18 UTC (rev 6302) +++ trunk/jython/src/org/python/core/BaseSet.java 2009-05-06 05:46:00 UTC (rev 6303) @@ -58,7 +58,7 @@ final PyObject baseset___or__(PyObject other) { if (!(other instanceof BaseSet)) { - throw Py.TypeError("Not Implemented"); + return null; } return baseset_union(other); } @@ -78,7 +78,7 @@ final PyObject baseset___and__(PyObject other) { if (!(other instanceof BaseSet)) { - throw Py.TypeError("Not Implemented"); + return null; } return baseset_intersection(other); } @@ -98,7 +98,7 @@ final PyObject baseset___sub__(PyObject other) { if (!(other instanceof BaseSet)) { - throw Py.TypeError("Not Implemented"); + return null; } return baseset_difference(other); } @@ -135,7 +135,7 @@ final PyObject baseset___xor__(PyObject other) { if (!(other instanceof BaseSet)) { - throw Py.TypeError("Not Implemented"); + return null; } return baseset_symmetric_difference(other); } @@ -266,8 +266,7 @@ } final PyObject baseset___le__(PyObject other) { - _binary_sanity_check(other); - return baseset_issubset(other); + return baseset_issubset(asBaseSet(other)); } public PyObject __ge__(PyObject other) { @@ -275,8 +274,7 @@ } final PyObject baseset___ge__(PyObject other) { - _binary_sanity_check(other); - return baseset_issuperset(other); + return baseset_issuperset(asBaseSet(other)); } public PyObject __lt__(PyObject other) { @@ -284,7 +282,7 @@ } final PyObject baseset___lt__(PyObject other) { - BaseSet bs = _binary_sanity_check(other); + BaseSet bs = asBaseSet(other); return Py.newBoolean(size() < bs.size() && baseset_issubset(other).__nonzero__()); } @@ -293,7 +291,7 @@ } final PyObject baseset___gt__(PyObject other) { - BaseSet bs = _binary_sanity_check(other); + BaseSet bs = asBaseSet(other); return Py.newBoolean(size() > bs.size() && baseset_issuperset(other).__nonzero__()); } @@ -385,12 +383,18 @@ return buf.toString(); } - protected final BaseSet _binary_sanity_check(PyObject other) throws PyIgnoreMethodTag { - try { + /** + * Casts other as BaseSet, throwing a TypeError tailored for the rich comparison + * methods when not applicable. + * + * @param other a PyObject + * @return a BaseSet + */ + protected final BaseSet asBaseSet(PyObject other) { + if (other instanceof BaseSet) { return (BaseSet)other; - } catch (ClassCastException e) { - throw Py.TypeError("Binary operation only permitted between sets"); } + throw Py.TypeError("can only compare to a set"); } /** This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |