From: <pj...@us...> - 2009-10-24 04:34:17
|
Revision: 6897 http://jython.svn.sourceforge.net/jython/?rev=6897&view=rev Author: pjenvey Date: 2009-10-24 04:34:03 +0000 (Sat, 24 Oct 2009) Log Message: ----------- treat numbers as smaller in _default_cmp's fallback fixes #1449 Modified Paths: -------------- trunk/jython/Lib/test/test_builtin_jy.py trunk/jython/Lib/test/test_cmp_jy.py trunk/jython/NEWS trunk/jython/src/org/python/core/PyObject.java Modified: trunk/jython/Lib/test/test_builtin_jy.py =================================================================== --- trunk/jython/Lib/test/test_builtin_jy.py 2009-10-24 04:21:18 UTC (rev 6896) +++ trunk/jython/Lib/test/test_builtin_jy.py 2009-10-24 04:34:03 UTC (rev 6897) @@ -25,6 +25,11 @@ return name self.assertEqual(dir(Foo()), []) + def test_numeric_cmp(self): + # http://bugs.jython.org/issue1449 + for numeric in 1, 2L, 3.0, 4j: + self.assertTrue(numeric < Ellipsis) + class LoopTest(unittest.TestCase): def test_break(self): Modified: trunk/jython/Lib/test/test_cmp_jy.py =================================================================== --- trunk/jython/Lib/test/test_cmp_jy.py 2009-10-24 04:21:18 UTC (rev 6896) +++ trunk/jython/Lib/test/test_cmp_jy.py 2009-10-24 04:34:03 UTC (rev 6897) @@ -60,15 +60,14 @@ baz.cmp = lambda other : Foo() self.assertEqual(cmp(100, baz), 0) baz.cmp = lambda other : NotImplemented - self.assertEqual(cmp(100, baz), 1) + # CPython is faulty here (returns 1) + self.assertEqual(cmp(100, baz), -1 if test_support.is_jython else 1) baz.cmp = lambda other: Bar() self.assertRaises(ValueError, cmp, 100, baz) baz.cmp = lambda other: 1 / 0 self.assertRaises(ZeroDivisionError, cmp, 100, baz) del Baz.__cmp__ - # CPython handles numbers differently than other types in - # object.c:default_3way_compare, and gets 1 here. we don't care - self.assert_(cmp(100, baz) in (-1, 1)) + self.assertEqual(cmp(100, baz), -1) def test_cmp_stops_short(self): class Foo(object): Modified: trunk/jython/NEWS =================================================================== --- trunk/jython/NEWS 2009-10-24 04:21:18 UTC (rev 6896) +++ trunk/jython/NEWS 2009-10-24 04:34:03 UTC (rev 6897) @@ -4,6 +4,7 @@ Bugs Fixed - [ 1478 ] defaultdict & weakref.WeakKeyDictionary [TypeError: first argument must be callable] - [ 1487 ] Import of module with latin-1 chars fails on utf-8 file encoding + - [ 1449 ] Ellipsis comparison different from Python 2.5 to Jython 2.5 - Fix runtime issues during exitfuncs triggered via SystemRestart (such as during Django or Pylons development mode reloading) Modified: trunk/jython/src/org/python/core/PyObject.java =================================================================== --- trunk/jython/src/org/python/core/PyObject.java 2009-10-24 04:21:18 UTC (rev 6896) +++ trunk/jython/src/org/python/core/PyObject.java 2009-10-24 04:34:03 UTC (rev 6897) @@ -1299,25 +1299,33 @@ private final int _default_cmp(PyObject other) { int result; - if (this._is(other).__nonzero__()) + if (_is(other).__nonzero__()) return 0; /* None is smaller than anything */ - if (this == Py.None) + if (this == Py.None) { return -1; - if (other == Py.None) + } + if (other == Py.None) { return 1; + } // No rational way to compare these, so ask their classes to compare - PyType this_type = this.getType(); - PyType other_type = other.getType(); - if (this_type == other_type) { - return Py.id(this) < Py.id(other)? -1: 1; + PyType type = getType(); + PyType otherType = other.getType(); + if (type == otherType) { + return Py.id(this) < Py.id(other) ? -1 : 1; } - result = this_type.fastGetName().compareTo(other_type.fastGetName()); - if (result == 0) - return Py.id(this_type)<Py.id(other_type)? -1: 1; - return result < 0? -1: 1; + + // different type: compare type names; numbers are smaller + String typeName = isNumberType() ? "" : type.fastGetName(); + String otherTypeName = otherType.isNumberType() ? "" : otherType.fastGetName(); + result = typeName.compareTo(otherTypeName); + if (result == 0) { + // Same type name, or (more likely) incomparable numeric types + return Py.id(type) < Py.id(otherType) ? -1 : 1; + } + return result < 0 ? -1 : 1; } private final int _cmp_unsafe(PyObject other) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |