From: <cg...@us...> - 2007-07-30 03:21:26
|
Revision: 3365 http://jython.svn.sourceforge.net/jython/?rev=3365&view=rev Author: cgroves Date: 2007-07-29 20:21:24 -0700 (Sun, 29 Jul 2007) Log Message: ----------- added reversed builtin to support PEP 322 from patch #1761139 from mehendran Modified Paths: -------------- branches/2.3/Lib/test/test_enumerate.py branches/2.3/src/org/python/core/__builtin__.java Added Paths: ----------- branches/2.3/src/org/python/core/PyReversedIterator.java Modified: branches/2.3/Lib/test/test_enumerate.py =================================================================== --- branches/2.3/Lib/test/test_enumerate.py 2007-07-30 02:33:35 UTC (rev 3364) +++ branches/2.3/Lib/test/test_enumerate.py 2007-07-30 03:21:24 UTC (rev 3365) @@ -131,9 +131,23 @@ seq = range(10,20000,2) res = zip(range(20000), seq) +class TestReversed(unittest.TestCase): + def test_simple(self): + class A: + def __getitem__(self, i): + if i < 5: + return str(i) + raise StopIteration + def __len__(self): + return 5 + for data in 'abc', range(5), tuple(enumerate('abc')), A(): + self.assertEqual(list(data)[::-1], list(reversed(data))) + self.assertRaises(TypeError, reversed, {}) + def test_main(verbose=None): - testclasses = (EnumerateTestCase, SubclassTestCase, TestEmpty, TestBig) + testclasses = (EnumerateTestCase, SubclassTestCase, TestEmpty, TestBig, + TestReversed) for test in testclasses: test_support.run_unittest(test) Added: branches/2.3/src/org/python/core/PyReversedIterator.java =================================================================== --- branches/2.3/src/org/python/core/PyReversedIterator.java (rev 0) +++ branches/2.3/src/org/python/core/PyReversedIterator.java 2007-07-30 03:21:24 UTC (rev 3365) @@ -0,0 +1,34 @@ +package org.python.core; + +/** + * An iterator that yields the objects from a sequence-like object in reverse + * order. + */ +public class PyReversedIterator extends PyIterator { + + /** + * Creates an iterator that first yields the item at __len__ - 1 on seq and + * returns the objects in descending order from there down to 0. + * + * @param seq - + * an object that supports __getitem__ and __len__ + */ + public PyReversedIterator(PyObject seq) { + this.seq = seq; + idx = seq.__len__(); + if(idx > 0) { + idx = idx - 1; + } + } + + public PyObject __iternext__() { + if(idx >= 0) { + return seq.__finditem__(idx--); + } + return null; + } + + private PyObject seq; + + private int idx; +} Modified: branches/2.3/src/org/python/core/__builtin__.java =================================================================== --- branches/2.3/src/org/python/core/__builtin__.java 2007-07-30 02:33:35 UTC (rev 3364) +++ branches/2.3/src/org/python/core/__builtin__.java 2007-07-30 03:21:24 UTC (rev 3365) @@ -110,6 +110,8 @@ return fancyCall(new PyObject[] { arg1 }); case 43: return fancyCall(new PyObject[] { arg1 }); + case 45: + return __builtin__.reversed(arg1); default: throw info.unexpectedCall(1, false); } @@ -386,9 +388,8 @@ dict.__setitem__("vars", new BuiltinFunctions("vars", 41, 0, 1)); dict.__setitem__("xrange", new BuiltinFunctions("xrange", 42, 1, 3)); dict.__setitem__("zip", new BuiltinFunctions("zip", 43, 1, -1)); - + dict.__setitem__("reversed", new BuiltinFunctions("reversed", 45, 1)); dict.__setitem__("__import__", new ImportFunction()); - } public static PyObject abs(PyObject o) { @@ -1016,7 +1017,16 @@ } return result; } - + + public static PyObject reversed(PyObject seq) { + if(hasattr(seq, "__getitem__") && hasattr(seq, "__len__") && + !hasattr(seq, "keys")) { + return new PyReversedIterator(seq); + } else { + throw Py.TypeError("argument to reversed() must be a sequence"); + } + } + public static PyObject sum(PyObject seq) { return sum(seq, Py.Zero); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |