From: <pj...@us...> - 2010-07-27 23:44:58
|
Revision: 7082 http://jython.svn.sourceforge.net/jython/?rev=7082&view=rev Author: pjenvey Date: 2010-07-27 23:44:47 +0000 (Tue, 27 Jul 2010) Log Message: ----------- fix array breaking __radd__ fallbacks fixes #1622 Modified Paths: -------------- trunk/jython/Lib/test/test_array.py trunk/jython/Lib/test/test_array_jy.py trunk/jython/NEWS trunk/jython/src/org/python/core/PyArray.java Modified: trunk/jython/Lib/test/test_array.py =================================================================== --- trunk/jython/Lib/test/test_array.py 2010-07-20 05:20:12 UTC (rev 7081) +++ trunk/jython/Lib/test/test_array.py 2010-07-27 23:44:47 UTC (rev 7082) @@ -291,10 +291,13 @@ ) b = array.array(self.badtypecode()) - self.assertRaises(TypeError, a.__add__, b) + if test_support.is_jython: + self.assertRaises(TypeError, operator.add, a, b) + self.assertRaises(TypeError, operator.add, a, "bad") + else: + self.assertRaises(TypeError, a.__add__, b) + self.assertRaises(TypeError, a.__add__, "bad") - self.assertRaises(TypeError, a.__add__, "bad") - def test_iadd(self): a = array.array(self.typecode, self.example[::-1]) b = a @@ -306,10 +309,13 @@ ) b = array.array(self.badtypecode()) - self.assertRaises(TypeError, a.__add__, b) + if test_support.is_jython: + self.assertRaises(TypeError, operator.add, a, b) + self.assertRaises(TypeError, operator.iadd, a, "bad") + else: + self.assertRaises(TypeError, a.__add__, b) + self.assertRaises(TypeError, a.__iadd__, "bad") - self.assertRaises(TypeError, a.__iadd__, "bad") - def test_mul(self): a = 5*array.array(self.typecode, self.example) self.assertEqual( Modified: trunk/jython/Lib/test/test_array_jy.py =================================================================== --- trunk/jython/Lib/test/test_array_jy.py 2010-07-20 05:20:12 UTC (rev 7081) +++ trunk/jython/Lib/test/test_array_jy.py 2010-07-27 23:44:47 UTC (rev 7082) @@ -4,10 +4,7 @@ import os import unittest from test import test_support -from array import array, zeros -from java.lang import String -from java.lang.reflect import Array -from java.util import Arrays +from array import array class ArrayJyTestCase(unittest.TestCase): @@ -15,12 +12,17 @@ # While jarray is still being phased out, just flex the initializers. # The rest of the test for array will catch all the big problems. import jarray + from java.lang import String jarray.array(range(5), 'i') jarray.array([String("a"), String("b"), String("c")], String) jarray.zeros(5, 'i') jarray.zeros(5, String) def test_java_object_arrays(self): + from array import zeros + from java.lang import String + from java.lang.reflect import Array + from java.util import Arrays jStringArr = array(String, [String("a"), String("b"), String("c")]) self.assert_( Arrays.equals(jStringArr.typecode, 'java.lang.String'), @@ -32,6 +34,7 @@ self.assertEqual(jStringArr, eval(str(jStringArr))) def test_java_compat(self): + from array import zeros from java.awt import Color hsb = Color.RGBtoHSB(0,255,255, None) self.assertEqual(hsb, array('f', [0.5,1,1]), @@ -68,9 +71,24 @@ self.assertEqual(x, array('i', range(5) * 4)) +class ArrayOpsTestCase(unittest.TestCase): + + def test_ops(self): + # http://bugs.jython.org/issue1622 + class Foo(object): + def __radd__(self, that): + return '__radd__' + ar = array('i', range(5)) + self.assertEqual('__radd__', ar + Foo()) + ar += Foo() + self.assertEqual('__radd__', ar) + + def test_main(): - test_support.run_unittest(ArrayJyTestCase, - ToFromfileTestCase) + tests = [ToFromfileTestCase, ArrayOpsTestCase] + if test_support.is_jython: + tests.extend([ArrayJyTestCase]) + test_support.run_unittest(*tests) if __name__ == "__main__": Modified: trunk/jython/NEWS =================================================================== --- trunk/jython/NEWS 2010-07-20 05:20:12 UTC (rev 7081) +++ trunk/jython/NEWS 2010-07-27 23:44:47 UTC (rev 7082) @@ -6,6 +6,7 @@ - [ 1506 ] Jython applies PEP263 pattern for determining source-code encoding on noncomments - [ 1630 ] threading.Thread lacks __tojava__ method - [ 1558 ] PyFunction to single method interface wrapping does not handle java.lang.Object methods + - [ 1622 ] array type prevents __radd__ fallback Jython 2.5.2b1 Bugs Fixed Modified: trunk/jython/src/org/python/core/PyArray.java =================================================================== --- trunk/jython/src/org/python/core/PyArray.java 2010-07-20 05:20:12 UTC (rev 7081) +++ trunk/jython/src/org/python/core/PyArray.java 2010-07-27 23:44:47 UTC (rev 7082) @@ -304,17 +304,16 @@ return array___iadd__(other); } - @ExposedMethod + @ExposedMethod(type = MethodType.BINARY) final PyObject array___iadd__(PyObject other) { - PyArray otherArr = null; if (!(other instanceof PyArray)) { - throw Py.TypeError(String.format("can only append array (not \"%.200s\") to array", - other.getType().fastGetName())); + return null; } - otherArr = (PyArray)other; + + PyArray otherArr = (PyArray)other; if (!otherArr.typecode.equals(this.typecode)) { - throw Py.TypeError("can only append arrays of the same type, " - + "expected '" + this.type + ", found " + otherArr.type); + throw Py.TypeError("can only append arrays of the same type, expected '" + + this.type + ", found " + otherArr.type); } delegate.appendArray(otherArr.delegate.copyArray()); return this; @@ -332,16 +331,16 @@ * a PyArray to be added to the instance * @return the result of the addition as a new PyArray instance */ - @ExposedMethod + @ExposedMethod(type = MethodType.BINARY) final PyObject array___add__(PyObject other) { - PyArray otherArr = null; - if(!(other instanceof PyArray)) { - throw Py.TypeError("can only append another array to an array"); + if (!(other instanceof PyArray)) { + return null; } - otherArr = (PyArray)other; - if(!otherArr.typecode.equals(this.typecode)) { - throw Py.TypeError("can only append arrays of the same type, " - + "expected '" + this.type + ", found " + otherArr.type); + + PyArray otherArr = (PyArray)other; + if (!otherArr.typecode.equals(this.typecode)) { + throw Py.TypeError("can only append arrays of the same type, expected '" + this.type + + ", found " + otherArr.type); } PyArray ret = new PyArray(this); ret.delegate.appendArray(otherArr.delegate.copyArray()); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |