From: <cg...@us...> - 2008-12-02 10:54:30
|
Revision: 5676 http://jython.svn.sourceforge.net/jython/?rev=5676&view=rev Author: cgroves Date: 2008-12-02 10:54:26 +0000 (Tue, 02 Dec 2008) Log Message: ----------- Patch #1148 from Geoffrey French. Adds slice and negative index support to get, set and del methods on wrapped instances of java.util.List. Modified Paths: -------------- branches/newstyle-java-types/src/org/python/core/PyArray.java branches/newstyle-java-types/src/org/python/core/PyJavaType.java branches/newstyle-java-types/src/org/python/core/PyList.java branches/newstyle-java-types/src/org/python/core/PySequence.java branches/newstyle-java-types/src/org/python/core/ThreadState.java Added Paths: ----------- branches/newstyle-java-types/Lib/test/test_java_list_delegate.py branches/newstyle-java-types/src/org/python/core/SequenceIndexDelegate.java Added: branches/newstyle-java-types/Lib/test/test_java_list_delegate.py =================================================================== --- branches/newstyle-java-types/Lib/test/test_java_list_delegate.py (rev 0) +++ branches/newstyle-java-types/Lib/test/test_java_list_delegate.py 2008-12-02 10:54:26 UTC (rev 5676) @@ -0,0 +1,181 @@ +from java.util import ArrayList, List, Vector + +from copy import copy + +import unittest +import test.test_support + +class CollectionProxyTest(unittest.TestCase): + def _perform_op(self, value, op_func): + """ + Perform an operation + + value - the value to operate on + op_func - the function that applies the operation to value + + Returns: + the result of calling op_func, OR the exception that was raised in op_func + """ + try: + return op_func(value) + except Exception, e: + return type(e) + + def check_list(self, control, results, initial): + for result in results: + try: + len(result) + except: + print result + self.assertEquals(len(control), len(result), "%s is wrong for %s" % (type(result), initial)) + for pvalue, jvalue in zip(control, result): + self.assertEquals(pvalue, jvalue) + + def _list_op_test(self, initial_value, op_func, check_value): + """ + Tests a list operation + + Ensures that performing an operation on: + - a python list + - a java.util.List instance + + givens the same result in both cases + """ + lists = [list(initial_value), ArrayList(initial_value), Vector(initial_value)] + + results = [self._perform_op(l, op_func) for l in lists] + self.check_list(lists[0], lists[1:], initial_value) + if check_value or not isinstance(results[0], list): + for r in results[1:]: + self.assertEquals(results[0], r) + else: + self.check_list(results[0], results[1:], initial_value) + + def test_get_integer(self): + initial_value = range(0, 5) + + for i in xrange(-7, 7): + self._list_op_test(initial_value, lambda xs: xs[i], True) + + def test_set_integer(self): + initial_value = range(0, 5) + + def make_op_func(index): + def _f(xs): + xs[index] = 100 + return _f + + for i in xrange(-7, 7): + self._list_op_test(initial_value, make_op_func(i), True) + + def test_set_slice(self): + initial_value = range(0, 10) + + def make_op_func(i, j, k, v): + def _f(xs): + xs[i:j:k] = v + return _f + + for i in xrange(-12, 12): + for j in xrange(-12, 12): + for k in xrange(-12, 12): + self._list_op_test(initial_value, make_op_func(i, j, k, []), True) + self._list_op_test(initial_value, make_op_func(i, j, k, range(0,2)), True) + self._list_op_test(initial_value, make_op_func(i, j, k, range(0,4)), True) + self._list_op_test(initial_value, make_op_func(i, j, k, xrange(0,2)), True) + + def test_del_integer(self): + initial_value = range(0,5) + + def make_op_func(index): + def _f(xs): + del xs[index] + return _f + + for i in xrange(-7, 7): + self._list_op_test(initial_value, make_op_func(i), True) + + def test_del_slice(self): + initial_value = range(0,10) + + def make_op_func(i, j, k): + def _f(xs): + del xs[i:j:k] + return _f + + for i in xrange(-12, 12): + for j in xrange(-12, 12): + for k in xrange(-12, 12): + self._list_op_test(initial_value, make_op_func(i, j, k), True) + + def test_len(self): + jlist = ArrayList() + jlist.addAll(range(0, 10)) + + self.assert_(len(jlist) == 10) + + def test_iter(self): + jlist = ArrayList() + jlist.addAll(range(0, 10)) + + i = iter(jlist) + + x = list(i) + + self.assert_(x == range(0, 10)) + + def test_override_len(self): + class MyList (ArrayList): + def __len__(self): + return self.size() + 1; + + m = MyList() + m.addAll(range(0,10)) + + self.assert_(len(m) == 11) + + def test_override_iter(self): + class MyList (ArrayList): + def __iter__(self): + return iter(self.subList(0, self.size() - 1)); + + + m = MyList() + m.addAll(range(0,10)) + i = iter(m) + x = list(i) + + self.assert_(x == range(0, 9)) + + def test_override_getsetdelitem(self): + # Create an ArrayList subclass that provides some silly overrides for get/set/del item + class MyList (ArrayList): + def __getitem__(self, key): + return self.get(key) * 2; + + def __setitem__(self, key, value): + return self.set(key, value * 2); + + def __delitem__(self, key): + self.add(84) + + + m = MyList() + m.addAll(range(0,10)) + + self.assert_(m[1] == 2) + self.assert_(m.get(1) == 1) + + m[0] = 3 + self.assert_(m.get(0) == 6) + self.assert_(m[0] == 12) + + del m[0] + self.assert_(m.size() == 11) + self.assert_(m.get(10) == 84) + +def test_main(): + test.test_support.run_unittest(CollectionProxyTest) + +if __name__ == "__main__": + test_main() Modified: branches/newstyle-java-types/src/org/python/core/PyArray.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/PyArray.java 2008-12-02 07:47:23 UTC (rev 5675) +++ branches/newstyle-java-types/src/org/python/core/PyArray.java 2008-12-02 10:54:26 UTC (rev 5676) @@ -239,10 +239,7 @@ @ExposedMethod(defaults = "null") final void array___setslice__(PyObject start, PyObject stop, PyObject step, PyObject value) { - if(value == null) { - value = step; - step = null; - } + seq___setslice__(start, stop, step, value); } @@ -656,30 +653,15 @@ } /** - * Delete the slice defined by <em>start</em>, <em>stop</em> and - * <em>step</em> from the array. + * Delete the slice defined by <em>start</em> to <em>stop</em> from the array. * * @param start * starting index of slice * @param stop * finishing index of slice - * @param step - * stepping increment between start and stop */ - protected void delRange(int start, int stop, int step) { - if (step == 1) { - delegate.remove(start, stop); - } else if (step > 1) { - for (int i = start; i < stop; i += step) { - delegate.remove(i); - i--; - stop--; - } - } else if (step < 0) { - for (int i = start; i >= 0 && i >= stop; i += step) { - delegate.remove(i); - } - } + protected void delRange(int start, int stop) { + delegate.remove(start, stop); } @ExposedMethod @@ -1198,7 +1180,7 @@ * value to be inserted into array */ public void insert(int index, PyObject value) { - index = calculateIndex(index); + index = boundToSequence(index); if ("u".equals(typecode)) { int codepoint = getCodePoint(value); delegate.makeInsertSpace(index); @@ -1241,7 +1223,7 @@ if (delegate.getSize() == 0) { throw Py.IndexError("pop from empty array"); } - index = fixindex(index); + index = delegator.fixindex(index); if (index == -1) { throw Py.IndexError("pop index out of range"); } Modified: branches/newstyle-java-types/src/org/python/core/PyJavaType.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/PyJavaType.java 2008-12-02 07:47:23 UTC (rev 5675) +++ branches/newstyle-java-types/src/org/python/core/PyJavaType.java 2008-12-02 10:54:26 UTC (rev 5676) @@ -5,6 +5,7 @@ import java.lang.reflect.Member; import java.lang.reflect.Method; import java.lang.reflect.Modifier; +import java.util.ArrayList; import java.util.Collection; import java.util.Enumeration; import java.util.EventListener; @@ -445,32 +446,21 @@ PyBuiltinMethodNarrow listGetProxy = new ListMethod("__getitem__", 1, 1) { @Override public PyObject __call__(PyObject key) { - if (key instanceof PyInteger) { - return Py.java2py(asList().get(((PyInteger)key).getValue())); - } else { - throw Py.TypeError("only integer keys accepted"); - } + return new ListIndexDelegate(asList()).checkIdxAndGetItem(key); } }; PyBuiltinMethodNarrow listSetProxy = new ListMethod("__setitem__", 2, 2) { @Override public PyObject __call__(PyObject key, PyObject value) { - if (key instanceof PyInteger) { - asList().set(((PyInteger)key).getValue(), Py.tojava(value, Object.class)); - } else { - throw Py.TypeError("only integer keys accepted"); - } + new ListIndexDelegate(asList()).checkIdxAndSetItem(key, value); return Py.None; } }; PyBuiltinMethodNarrow listRemoveProxy = new ListMethod("__delitem__", 1, 1) { @Override public PyObject __call__(PyObject key) { - if (key instanceof PyInteger) { - return Py.java2py(asList().remove(((PyInteger)key).getValue())); - } else { - throw Py.TypeError("only integer keys accepted"); - } + new ListIndexDelegate(asList()).checkIdxAndDelItem(key); + return Py.None; } }; collectionProxies.put(List.class, new PyBuiltinMethod[] {listGetProxy, @@ -479,4 +469,86 @@ } return collectionProxies; } + + protected static class ListIndexDelegate extends SequenceIndexDelegate { + + private final List list; + + public ListIndexDelegate(List list) { + this.list = list; + } + @Override + public void delItem(int idx) { + list.remove(idx); + } + + @Override + public PyObject getItem(int idx) { + return Py.java2py(list.get(idx)); + } + + @Override + public PyObject getSlice(int start, int stop, int step) { + if (step > 0 && stop < start) { + stop = start; + } + int n = PySequence.sliceLength(start, stop, step); + List<Object> newList; + try { + newList = list.getClass().newInstance(); + } catch (Exception e) { + throw Py.JavaError(e); + } + int j = 0; + for (int i = start; j < n; i += step) { + newList.add(list.get(i)); + } + return Py.java2py(newList); + } + + @Override + public String getTypeName() { + return list.getClass().getName(); + } + + @Override + public int len() { + return list.size(); + } + + @Override + public void setItem(int idx, PyObject value) { + list.set(idx, value.__tojava__(Object.class)); + } + + @Override + public void setSlice(int start, int stop, int step, PyObject value) { + if (stop < start) { + stop = start; + } + if (step == 0) { + return; + } + if (value.javaProxy == this) { + List newseq = new ArrayList(len()); + for (Object object : ((List)value.javaProxy)) { + newseq.add(object); + } + value = Py.java2py(newseq); + } + int j = start; + for (PyObject obj : value.asIterable()) { + setItem(j, obj); + j += step; + } + } + + @Override + public void delItems(int start, int stop) { + int n = stop - start; + while (n-- > 0) { + delItem(start); + } + } + } } Modified: branches/newstyle-java-types/src/org/python/core/PyList.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/PyList.java 2008-12-02 07:47:23 UTC (rev 5675) +++ branches/newstyle-java-types/src/org/python/core/PyList.java 2008-12-02 10:54:26 UTC (rev 5676) @@ -49,9 +49,9 @@ append(item); } } - + private static List<PyObject> listify(Iterator<PyObject> iter) { - final List<PyObject> list = new LinkedList<PyObject>(); + final List<PyObject> list = new LinkedList<PyObject>(); while (iter.hasNext()) { list.add(iter.next()); } @@ -112,20 +112,8 @@ remove(i); } - protected void delRange(int start, int stop, int step) { - if(step == 1) { - remove(start, stop); - } else if(step > 1) { - for(int i = start; i < stop; i += step) { - remove(i); - i--; - stop--; - } - } else if(step < 0) { - for(int i = start; i >= 0 && i >= stop; i += step) { - remove(i); - } - } + protected void delRange(int start, int stop) { + remove(start, stop); } protected void set(int i, PyObject value) { @@ -164,13 +152,7 @@ otherArray = otherArray.clone(); } list.replaceSubArray(start, stop, otherArray, 0, n); - } else if(step > 1) { - int n = value.__len__(); - for(int i = 0, j = 0; i < n; i++, j += step) { - list.pyset(j + start, value.pyget(i)); - } - } else if(step < 0) { - int n = value.__len__(); + } else if(step != 0) { if(value == this) { PyList newseq = new PyList(); PyObject iter = value.__iter__(); @@ -179,7 +161,8 @@ } value = newseq; } - for(int i = 0, j = list.size() - 1; i < n; i++, j += step) { + int n = value.__len__(); + for (int i = 0, j = start; i < n; i++, j += step) { list.pyset(j, value.pyget(i)); } } @@ -289,7 +272,7 @@ System.arraycopy(array, 0, array, i * size, size); } gListAllocatedStatus = __len__(); - return this; + return this; } @Override @@ -409,10 +392,6 @@ @ExposedMethod(defaults = "null") final void list___setslice__(PyObject start, PyObject stop, PyObject step, PyObject value) { - if(value == null) { - value = step; - step = null; - } seq___setslice__(start, stop, step, value); } @@ -455,7 +434,7 @@ /** * Add a single element to the end of list. - * + * * @param o * the element to add. */ @@ -471,7 +450,7 @@ /** * Return the number elements in the list that equals the argument. - * + * * @param o * the argument to test for. Testing is done with the <code>==</code> operator. */ @@ -493,7 +472,7 @@ /** * return smallest index where an element in the list equals the argument. - * + * * @param o * the argument to test for. Testing is done with the <code>==</code> operator. */ @@ -530,8 +509,8 @@ private int _index(PyObject o, String message, int start, int stop) { // Follow Python 2.3+ behavior - int validStop = calculateIndex(stop); - int validStart = calculateIndex(start); + int validStop = boundToSequence(stop); + int validStart = boundToSequence(start); PyObject[] array = getArray(); for(int i = validStart; i < validStop && i < size(); i++) { if(array[i].equals(o)) { @@ -544,7 +523,7 @@ /** * Insert the argument element into the list at the specified index. <br> * Same as <code>s[index:index] = [o] if index >= 0</code>. - * + * * @param index * the position where the element will be inserted. * @param o @@ -570,7 +549,7 @@ * Remove the first occurence of the argument from the list. The elements arecompared with the * <code>==</code> operator. <br> * Same as <code>del s[s.index(x)]</code> - * + * * @param o * the element to search for and remove. */ @@ -616,7 +595,7 @@ /** * Removes and return the <code>n</code> indexed element in the list. - * + * * @param n * the index of the element to remove and return. */ @@ -644,7 +623,7 @@ /** * Append the elements in the argument sequence to the end of the list. <br> * Same as <code>s[len(s):len(s)] = o</code>. - * + * * @param o * the sequence of items to append to the list. */ @@ -691,11 +670,11 @@ * the sorting process down considerably; e.g. to sort a list in reverse order it is much faster * to use calls to the methods sort() and reverse() than to use the built-in function sort() * with a comparison function that reverses the ordering of the elements. - * + * * @param compare * the comparison function. */ - + /** * Sort the items of the list in place. Items is compared with the normal relative comparison * operators. @@ -723,7 +702,7 @@ MergeState ms = new MergeState(this, cmp, key, reverse.__nonzero__()); ms.sort(); } - + public int hashCode() { return list___hash__(); } Modified: branches/newstyle-java-types/src/org/python/core/PySequence.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/PySequence.java 2008-12-02 07:47:23 UTC (rev 5675) +++ branches/newstyle-java-types/src/org/python/core/PySequence.java 2008-12-02 10:54:26 UTC (rev 5676) @@ -80,9 +80,10 @@ getType().fastGetName())); } - protected void delRange(int start, int stop, int step) { + protected void delRange(int start, int stop) { throw Py.TypeError(String.format("'%s' object does not support item deletion", getType().fastGetName())); + } public boolean __nonzero__() { @@ -244,20 +245,11 @@ return ret; } - protected int fixindex(int index) { - int l = __len__(); - if(index < 0) { - index += l; - } - if(index < 0 || index >= l) { - return -1; - } else { - return index; - } - } - - // XXX: more appropriate naming of this vs fixindex - protected int calculateIndex(int index) { + /** + * Adjusts <code>index</code> such that it's >= 0 and <= __len__. If <code>index</code> starts + * off negative, it's treated as an index from the end of the sequence going back to the start. + */ + protected int boundToSequence(int index) { int length = __len__(); if(index < 0) { index = index += length; @@ -275,12 +267,7 @@ } final synchronized PyObject seq___finditem__(int index) { - index = fixindex(index); - if(index == -1) { - return null; - } else { - return pyget(index); - } + return delegator.checkIdxAndFindItem(index); } public PyObject __finditem__(PyObject index) { @@ -288,14 +275,7 @@ } final PyObject seq___finditem__(PyObject index) { - if (index.isIndex()) { - return seq___finditem__(index.asIndex(Py.IndexError)); - } else if (index instanceof PySlice) { - PySlice s = (PySlice)index; - return seq___getslice__(s.start, s.stop, s.step); - } else { - throw Py.TypeError(getType().fastGetName() + " indices must be integers"); - } + return delegator.checkIdxAndFindItem(index); } public PyObject __getitem__(PyObject index) { @@ -303,11 +283,7 @@ } final PyObject seq___getitem__(PyObject index) { - PyObject ret = __finditem__(index); - if(ret == null) { - throw Py.IndexError("index out of range: " + index); - } - return ret; + return delegator.checkIdxAndGetItem(index); } public boolean isMappingType() throws PyIgnoreMethodTag { @@ -322,13 +298,8 @@ return seq___getslice__(start, stop, step); } - final synchronized PyObject seq___getslice__(PyObject start, PyObject stop) { - return seq___getslice__(start, stop, null); - } - final synchronized PyObject seq___getslice__(PyObject start, PyObject stop, PyObject step) { - int[] indices = new PySlice(start, stop, step).indicesEx(__len__()); - return getslice(indices[0], indices[1], indices[2]); + return delegator.getSlice(new PySlice(start, stop, step)); } public synchronized void __setslice__(PyObject start, @@ -338,21 +309,15 @@ seq___setslice__(start, stop, step, value); } - final synchronized void seq___setslice__(PyObject start, PyObject stop, PyObject value) { - seq___setslice__(start, stop, null, value); - } - final synchronized void seq___setslice__(PyObject start, PyObject stop, PyObject step, PyObject value) { - PySlice slice = new PySlice(start, stop, step); - int[] indices = slice.indicesEx(__len__()); - if ((slice.step != Py.None) && value.__len__() != indices[3]) { - throw Py.ValueError(String.format("attempt to assign sequence of size %d to extended " - + "slice of size %d", value.__len__(), indices[3])); + if (value == null) { + value = step; + step = null; } - setslice(indices[0], indices[1], indices[2], value); + delegator.checkIdxAndSetSlice(new PySlice(start, stop, step), value); } public synchronized void __delslice__(PyObject start, PyObject stop, PyObject step) { @@ -360,35 +325,19 @@ } final synchronized void seq___delslice__(PyObject start, PyObject stop, PyObject step) { - int[] indices = new PySlice(start, stop, step).indicesEx(__len__()); - delRange(indices[0], indices[1], indices[2]); + delegator.checkIdxAndDelItem(new PySlice(start, stop, step)); } public synchronized void __setitem__(int index, PyObject value) { - seq___setitem__(index, value); + delegator.checkIdxAndSetItem(index, value); } - final synchronized void seq___setitem__(int index, PyObject value) { - int i = fixindex(index); - if(i == -1) { - throw Py.IndexError(getType().fastGetName() + " assignment index out of range"); - } - set(i, value); - } - public void __setitem__(PyObject index, PyObject value) { seq___setitem__(index, value); } final void seq___setitem__(PyObject index, PyObject value) { - if (index.isIndex()) { - seq___setitem__(index.asIndex(Py.IndexError), value); - } else if (index instanceof PySlice) { - PySlice s = (PySlice)index; - seq___setslice__(s.start, s.stop, s.step, value); - } else { - throw Py.TypeError(getType().fastGetName() + " indices must be integers"); - } + delegator.checkIdxAndSetItem(index, value); } public synchronized void __delitem__(PyObject index) { @@ -396,18 +345,7 @@ } final synchronized void seq___delitem__(PyObject index) { - if (index.isIndex()) { - int i = fixindex(index.asIndex(Py.IndexError)); - if (i == -1) { - throw Py.IndexError(getType().fastGetName() + " assignment index out of range"); - } - del(i); - } else if (index instanceof PySlice) { - PySlice s = (PySlice)index; - seq___delslice__(s.start, s.stop, s.step); - } else { - throw Py.TypeError(getType().fastGetName() + " indices must be integers"); - } + delegator.checkIdxAndDelItem(index); } public synchronized Object __tojava__(Class c) throws PyIgnoreMethodTag { @@ -451,4 +389,48 @@ } return null; } + + protected final SequenceIndexDelegate delegator = new SequenceIndexDelegate() { + + @Override + public String getTypeName() { + return getType().fastGetName(); + } + + @Override + public void setItem(int idx, PyObject value) { + set(idx, value); + } + + @Override + public void setSlice(int start, int stop, int step, PyObject value) { + setslice(start, stop, step, value); + } + + @Override + public int len() { + return __len__(); + } + + @Override + public void delItem(int idx) { + del(idx); + } + + @Override + public void delItems(int start, int stop) { + delRange(start, stop); + } + + + @Override + public PyObject getItem(int idx) { + return pyget(idx); + } + + @Override + public PyObject getSlice(int start, int stop, int step) { + return getslice(start, stop, step); + } + }; } Added: branches/newstyle-java-types/src/org/python/core/SequenceIndexDelegate.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/SequenceIndexDelegate.java (rev 0) +++ branches/newstyle-java-types/src/org/python/core/SequenceIndexDelegate.java 2008-12-02 10:54:26 UTC (rev 5676) @@ -0,0 +1,127 @@ +package org.python.core; + +/** + * Handles all the index checking and manipulation for get, set and del operations on a sequence. + */ +public abstract class SequenceIndexDelegate { + + public abstract int len(); + + public abstract PyObject getItem(int idx); + + public abstract void setItem(int idx, PyObject value); + + public abstract void delItem(int idx); + + public abstract PyObject getSlice(int start, int stop, int step); + + public abstract void setSlice(int start, int stop, int step, PyObject value); + + public abstract void delItems(int start, int stop); + + public abstract String getTypeName(); + + public void checkIdxAndSetItem(PyObject idx, PyObject value) { + if (idx.isIndex()) { + checkIdxAndSetItem(idx.asIndex(Py.IndexError), value); + } else if (idx instanceof PySlice) { + checkIdxAndSetSlice((PySlice)idx, value); + } else { + throw Py.TypeError(getTypeName() + " indices must be integers"); + } + } + + public void checkIdxAndSetSlice(PySlice slice, PyObject value) { + int[] indices = slice.indicesEx(len()); + if ((slice.step != Py.None) && value.__len__() != indices[3]) { + throw Py.ValueError(String.format("attempt to assign sequence of size %d to extended " + + "slice of size %d", value.__len__(), indices[3])); + } + setSlice(indices[0], indices[1], indices[2], value); + } + + public void checkIdxAndSetItem(int idx, PyObject value) { + setItem(checkIdx(idx), value); + } + + public void checkIdxAndDelItem(PyObject idx) { + if (idx.isIndex()) { + delItem(checkIdx(idx.asIndex(Py.IndexError))); + } else if (idx instanceof PySlice) { + int[] indices = ((PySlice)idx).indicesEx(len()); + delSlice(indices[0], indices[1], indices[2]); + } else { + throw Py.TypeError(getTypeName() + " indices must be integers"); + } + } + + public PyObject checkIdxAndGetItem(PyObject idx) { + PyObject res = checkIdxAndFindItem(idx); + if (res == null) { + throw Py.IndexError("index out of range: " + idx); + } + return res; + } + + public PyObject checkIdxAndFindItem(PyObject idx) { + if (idx.isIndex()) { + return checkIdxAndFindItem(idx.asIndex(Py.IndexError)); + } else if (idx instanceof PySlice) { + return getSlice((PySlice)idx); + } else { + throw Py.TypeError(getTypeName() + " indices must be integers"); + } + } + + public PyObject getSlice(PySlice slice) { + int[] indices = slice.indicesEx(len()); + return getSlice(indices[0], indices[1], indices[2]); + } + + public PyObject checkIdxAndFindItem(int idx) { + idx = fixindex(idx); + if(idx == -1) { + return null; + } else { + return getItem(idx); + } + } + + private int checkIdx(int idx) { + int i = fixindex(idx); + if (i == -1) { + throw Py.IndexError(getTypeName() + " assignment index out of range"); + } + return i; + } + + int fixindex(int index) { + int l = len(); + if(index < 0) { + index += l; + } + if(index < 0 || index >= l) { + return -1; + } else { + return index; + } + } + + private void delSlice(int start, int stop, int step) { + if(step == 1) { + if (stop > start) { + delItems(start, stop); + } + } else if(step > 1) { + for(int i = start; i < stop; i += step) { + delItem(i); + i--; + stop--; + } + } else if(step < 0) { + for(int i = start; i >= 0 && i >= stop; i += step) { + delItem(i); + } + } + } +} \ No newline at end of file Modified: branches/newstyle-java-types/src/org/python/core/ThreadState.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/ThreadState.java 2008-12-02 07:47:23 UTC (rev 5675) +++ branches/newstyle-java-types/src/org/python/core/ThreadState.java 2008-12-02 10:54:26 UTC (rev 5676) @@ -75,7 +75,7 @@ } for (int i = reprStack.size() - 1; i >= 0; i--) { if (reprStack.pyget(i) == obj) { - reprStack.delRange(i, reprStack.size(), 1); + reprStack.delRange(i, reprStack.size()); } } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |