From: <pj...@us...> - 2008-11-16 22:53:00
|
Revision: 5582 http://jython.svn.sourceforge.net/jython/?rev=5582&view=rev Author: pjenvey Date: 2008-11-16 22:52:56 +0000 (Sun, 16 Nov 2008) Log Message: ----------- fix another binop corner case -- trigger the binop 'flip' when left_type < right_type (in terms of the reversed mro) instead of left_type != right_type Modified Paths: -------------- trunk/jython/Lib/test/test_descr_jy.py trunk/jython/src/org/python/core/PyObject.java trunk/jython/src/org/python/core/PyType.java Modified: trunk/jython/Lib/test/test_descr_jy.py =================================================================== --- trunk/jython/Lib/test/test_descr_jy.py 2008-11-14 22:54:53 UTC (rev 5581) +++ trunk/jython/Lib/test/test_descr_jy.py 2008-11-16 22:52:56 UTC (rev 5582) @@ -208,7 +208,22 @@ raises(bexc, bexc_msg, lambda : func(B())) self.assertEqual(func(C()), cresult) + def test_overriding_base_binop(self): + class MulBase(object): + def __init__(self, value): + self.value = value + def __mul__(self, other): + return self.value * other.value + def __rmul__(self, other): + return other.value * self.value + class DoublerBase(MulBase): + def __mul__(self, other): + return 2 * (self.value * other.value) + class AnotherDoubler(DoublerBase): + pass + self.assertEquals(DoublerBase(2) * AnotherDoubler(3), 12) + class InPlaceTestCase(unittest.TestCase): def test_iadd(self): Modified: trunk/jython/src/org/python/core/PyObject.java =================================================================== --- trunk/jython/src/org/python/core/PyObject.java 2008-11-14 22:54:53 UTC (rev 5581) +++ trunk/jython/src/org/python/core/PyObject.java 2008-11-16 22:52:56 UTC (rev 5582) @@ -1771,14 +1771,14 @@ * test_descr.subclass_right_op. */ PyObject o1 = this; - PyObject[] where = new PyObject[1]; - PyObject where1 = null, where2 = null; - PyObject impl1 = t1.lookup_where(left, where); + int[] where = new int[1]; + int where1, where2; + PyObject impl1 = t1.lookup_where_index(left, where); where1 = where[0]; - PyObject impl2 = t2.lookup_where(right, where); + PyObject impl2 = t2.lookup_where_index(right, where); where2 = where[0]; - if (impl2 != null && where1 != where2 && (t2.isSubType(t1) || - isStrUnicodeSpecialCase(t1, t2, op))) { + if (impl2 != null && where1 < where2 && (t2.isSubType(t1) || + isStrUnicodeSpecialCase(t1, t2, op))) { PyObject tmp = o1; o1 = o2; o2 = tmp; Modified: trunk/jython/src/org/python/core/PyType.java =================================================================== --- trunk/jython/src/org/python/core/PyType.java 2008-11-14 22:54:53 UTC (rev 5581) +++ trunk/jython/src/org/python/core/PyType.java 2008-11-16 22:52:56 UTC (rev 5582) @@ -825,6 +825,34 @@ return null; } + /** + * Like lookup but also provides (in where[0]) the index of the type in the reversed + * mro -- that is, how many subtypes away from the base object the type is. + * + * @param name attribute name (must be interned) + * @param where an int[] with a length of at least 1 + * @return found PyObject or null + */ + public PyObject lookup_where_index(String name, int[] where) { + PyObject[] mro = this.mro; + if (mro == null) { + return null; + } + int i = mro.length; + for (PyObject t : mro) { + i--; + PyObject dict = t.fastGetDict(); + if (dict != null) { + PyObject obj = dict.__finditem__(name); + if (obj != null) { + where[0] = i; + return obj; + } + } + } + return null; + } + public PyObject super_lookup(PyType ref, String name) { PyObject[] mro = this.mro; if (mro == null) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |