From: <pj...@us...> - 2008-07-28 19:46:42
|
Revision: 5012 http://jython.svn.sourceforge.net/jython/?rev=5012&view=rev Author: pjenvey Date: 2008-07-28 19:46:39 +0000 (Mon, 28 Jul 2008) Log Message: ----------- fix stock UserList/test_userlist: o handle __get/set/delslice__ start/stop as null or None in PySlice.indices2 o fix list slice assignments of the same underlying list (always copy first as CPython does) Modified Paths: -------------- branches/asm/src/org/python/core/PyList.java branches/asm/src/org/python/core/PySlice.java Removed Paths: ------------- branches/asm/Lib/UserList.py branches/asm/Lib/test/test_userlist.py Deleted: branches/asm/Lib/UserList.py =================================================================== --- branches/asm/Lib/UserList.py 2008-07-28 13:33:50 UTC (rev 5011) +++ branches/asm/Lib/UserList.py 2008-07-28 19:46:39 UTC (rev 5012) @@ -1,94 +0,0 @@ -"""A more or less complete user-defined wrapper around list objects.""" - -#Imported from Python 2.3.5 and added _fixindex -class UserList: - def __init__(self, initlist=None): - self.data = [] - if initlist is not None: - # XXX should this accept an arbitrary sequence? - if type(initlist) == type(self.data): - self.data[:] = initlist - elif isinstance(initlist, UserList): - self.data[:] = initlist.data[:] - else: - self.data = list(initlist) - def __repr__(self): return repr(self.data) - def __lt__(self, other): return self.data < self.__cast(other) - def __le__(self, other): return self.data <= self.__cast(other) - def __eq__(self, other): return self.data == self.__cast(other) - def __ne__(self, other): return self.data != self.__cast(other) - def __gt__(self, other): return self.data > self.__cast(other) - def __ge__(self, other): return self.data >= self.__cast(other) - def __cast(self, other): - if isinstance(other, UserList): return other.data - else: return other - def __cmp__(self, other): - return cmp(self.data, self.__cast(other)) - def __contains__(self, item): return item in self.data - def __len__(self): return len(self.data) - def __getitem__(self, i): return self.data[i] - def __setitem__(self, i, item): self.data[i] = item - def __delitem__(self, i): del self.data[i] - def __getslice__(self, i, j): - i = self._fixindex(i); j = self._fixindex(j) - return self.__class__(self.data[i:j]) - def __setslice__(self, i, j, other): - i = self._fixindex(i); j = self._fixindex(j) - if isinstance(other, UserList): - self.data[i:j] = other.data - elif isinstance(other, type(self.data)): - self.data[i:j] = other - else: - self.data[i:j] = list(other) - def __delslice__(self, i, j): - i = self._fixindex(i); j = self._fixindex(j) - del self.data[i:j] - def __add__(self, other): - if isinstance(other, UserList): - return self.__class__(self.data + other.data) - elif isinstance(other, type(self.data)): - return self.__class__(self.data + other) - else: - return self.__class__(self.data + list(other)) - def __radd__(self, other): - if isinstance(other, UserList): - return self.__class__(other.data + self.data) - elif isinstance(other, type(self.data)): - return self.__class__(other + self.data) - else: - return self.__class__(list(other) + self.data) - def __iadd__(self, other): - if isinstance(other, UserList): - self.data += other.data - elif isinstance(other, type(self.data)): - self.data += other - else: - self.data += list(other) - return self - def __mul__(self, n): - return self.__class__(self.data*n) - __rmul__ = __mul__ - def __imul__(self, n): - self.data *= n - return self - def append(self, item): self.data.append(item) - def insert(self, i, item): self.data.insert(i, item) - def pop(self, i=-1): return self.data.pop(i) - def remove(self, item): self.data.remove(item) - def count(self, item): return self.data.count(item) - def index(self, item, *args): return self.data.index(item, *args) - def reverse(self): self.data.reverse() - def sort(self, *args): self.data.sort(*args) - def extend(self, other): - if isinstance(other, UserList): - self.data.extend(other.data) - else: - self.data.extend(other) - def _fixindex(self, index): - if index < 0: - index += len(self.data) - elif index > len(self.data): - index = len(self.data) - index = max(index, 0) - return index - Deleted: branches/asm/Lib/test/test_userlist.py =================================================================== --- branches/asm/Lib/test/test_userlist.py 2008-07-28 13:33:50 UTC (rev 5011) +++ branches/asm/Lib/test/test_userlist.py 2008-07-28 19:46:39 UTC (rev 5012) @@ -1,269 +0,0 @@ -# Check every path through every method of UserList - -#imported from Python 2.3.5 - -from UserList import UserList -import unittest, test.test_support - -class UserListTest(unittest.TestCase): - - def test_constructors(self): - l0 = [] - l1 = [0] - l2 = [0, 1] - - u = UserList() - u0 = UserList(l0) - u1 = UserList(l1) - u2 = UserList(l2) - - uu = UserList(u) - uu0 = UserList(u0) - uu1 = UserList(u1) - uu2 = UserList(u2) - - v = UserList(tuple(u)) - class OtherList: - def __init__(self, initlist): - self.__data = initlist - def __len__(self): - return len(self.__data) - def __getitem__(self, i): - return self.__data[i] - v0 = UserList(OtherList(u0)) - vv = UserList("this is also a sequence") - - def test_repr(self): - l0 = [] - l2 = [0, 1, 2] - u0 = UserList(l0) - u2 = UserList(l2) - - self.assertEqual(str(u0), str(l0)) - self.assertEqual(repr(u0), repr(l0)) - self.assertEqual(`u2`, `l2`) - - def test_cmplen(self): - l0 = [] - l1 = [0] - l2 = [0, 1] - - # Test constructors - - u = UserList() - u0 = UserList(l0) - u1 = UserList(l1) - u2 = UserList(l2) - - uu = UserList(u) - uu0 = UserList(u0) - uu1 = UserList(u1) - uu2 = UserList(u2) - - def mycmp(x, y): - r = cmp(x, y) - if r < 0: return -1 - if r > 0: return 1 - return r - - all = [l0, l1, l2, u, u0, u1, u2, uu, uu0, uu1, uu2] - for a in all: - for b in all: - self.assertEqual(mycmp(a, b), mycmp(len(a), len(b))) - - self.assert_(u0 <= u2) - self.assert_(u2 >= u0) - - def test_getitem(self): - u = UserList([0, 1, 2]) - for i in xrange(len(u)): - self.assertEqual(u[i], i) - - def test_setitem(self): - u = UserList([0, 1]) - u[0] = 0 - u[1] = 100 - self.assertEqual(u, [0, 100]) - self.assertRaises(IndexError, u.__setitem__, 2, 200) - - def test_delitem(self): - u = UserList([0, 1]) - del u[1] - del u[0] - self.assertRaises(IndexError, u.__delitem__, 0) - - def test_getslice(self): - l = [0, 1] - u = UserList(l) - for i in xrange(-3, 4): - self.assertEqual(u[:i], l[:i]) - self.assertEqual(u[i:], l[i:]) - for j in xrange(-3, 4): - self.assertEqual(u[i:j], l[i:j]) - - def test_setslice(self): - l = [0, 1] - u = UserList(l) - - # Test __setslice__ - for i in range(-3, 4): - u[:i] = l[:i] - self.assertEqual(u, l) - u2 = u[:] - u2[:i] = u[:i] - self.assertEqual(u2, u) - u[i:] = l[i:] - self.assertEqual(u, l) - u2 = u[:] - u2[i:] = u[i:] - self.assertEqual(u2, u) - for j in range(-3, 4): - u[i:j] = l[i:j] - self.assertEqual(u, l) - u2 = u[:] - u2[i:j] = u[i:j] - self.assertEqual(u2, u) - - uu2 = u2[:] - uu2[:0] = [-2, -1] - self.assertEqual(uu2, [-2, -1, 0, 1]) - uu2[0:] = [] - self.assertEqual(uu2, []) - - def test_contains(self): - u = UserList([0, 1, 2]) - for i in u: - self.assert_(i in u) - for i in min(u)-1, max(u)+1: - self.assert_(i not in u) - - def test_delslice(self): - u = UserList([0, 1]) - del u[1:2] - del u[0:1] - self.assertEqual(u, []) - - u = UserList([0, 1]) - del u[1:] - del u[:1] - self.assertEqual(u, []) - - def test_addmul(self): - u1 = UserList([0]) - u2 = UserList([0, 1]) - self.assertEqual(u1, u1 + []) - self.assertEqual(u1, [] + u1) - self.assertEqual(u1 + [1], u2) - self.assertEqual([-1] + u1, [-1, 0]) - self.assertEqual(u2, u2*1) - self.assertEqual(u2, 1*u2) - self.assertEqual(u2+u2, u2*2) - self.assertEqual(u2+u2, 2*u2) - self.assertEqual(u2+u2+u2, u2*3) - self.assertEqual(u2+u2+u2, 3*u2) - - def test_add_specials(self): - u = UserList("spam") - u2 = u + "eggs" - self.assertEqual(u2, list("spameggs")) - - def test_radd_specials(self): - u = UserList("eggs") - u2 = "spam" + u - self.assertEqual(u2, list("spameggs")) - u2 = u.__radd__(UserList("spam")) - self.assertEqual(u2, list("spameggs")) - - def test_append(self): - u = UserList((0, )) - u.append(1) - self.assertEqual(u, [0, 1]) - - def test_insert(self): - u = UserList((0, 1)) - u.insert(0, -1) - self.assertEqual(u, [-1, 0, 1]) - - def test_pop(self): - u = UserList((-1, 0, 1)) - u.pop() - self.assertEqual(u, [-1, 0]) - u.pop(0) - self.assertEqual(u, [0]) - - def test_remove(self): - u = UserList((0, 1)) - u.remove(1) - self.assertEqual(u, [0]) - - def test_count(self): - u = UserList((0, 1))*3 - self.assertEqual(u.count(0), 3) - self.assertEqual(u.count(1), 3) - self.assertEqual(u.count(2), 0) - - def test_index(self): - u = UserList((0, 1)) - self.assertEqual(u.index(0), 0) - self.assertEqual(u.index(1), 1) - self.assertRaises(ValueError, u.index, 2) - - u = UserList([-2,-1,0,0,1,2]) - self.assertEqual(u.count(0), 2) - self.assertEqual(u.index(0), 2) - self.assertEqual(u.index(0,2), 2) - self.assertEqual(u.index(-2,-10), 0) - self.assertEqual(u.index(0,3), 3) - self.assertEqual(u.index(0,3,4), 3) - self.assertRaises(ValueError, u.index, 2,0,-10) - - def test_reverse(self): - u = UserList((0, 1)) - u2 = u[:] - u.reverse() - self.assertEqual(u, [1, 0]) - u.reverse() - self.assertEqual(u, u2) - - def test_sort(self): - u = UserList([1, 0]) - u.sort() - self.assertEqual(u, [0, 1]) - - def test_slice(self): - u = UserList("spam") - u[:2] = "h" - self.assertEqual(u, list("ham")) - - def test_iadd(self): - u = UserList((0, 1)) - u += [0, 1] - self.assertEqual(u, [0, 1, 0, 1]) - u += UserList([0, 1]) - self.assertEqual(u, [0, 1, 0, 1, 0, 1]) - - u = UserList("spam") - u += "eggs" - self.assertEqual(u, list("spameggs")) - - def test_extend(self): - u1 = UserList((0, )) - u2 = UserList((0, 1)) - u = u1[:] - u.extend(u2) - self.assertEqual(u, u1 + u2) - - u = UserList("spam") - u.extend("eggs") - self.assertEqual(u, list("spameggs")) - - def test_imul(self): - u = UserList((0, 1)) - u *= 3 - self.assertEqual(u, [0, 1, 0, 1, 0, 1]) - -def test_main(): - test.test_support.run_unittest(UserListTest) - -if __name__ == "__main__": - test_main() Modified: branches/asm/src/org/python/core/PyList.java =================================================================== --- branches/asm/src/org/python/core/PyList.java 2008-07-28 13:33:50 UTC (rev 5011) +++ branches/asm/src/org/python/core/PyList.java 2008-07-28 19:46:39 UTC (rev 5012) @@ -190,7 +190,7 @@ protected void setsliceList(int start, int stop, int step, List value) { if(step != 1) { - throw Py.TypeError("setslice with java.util.List and step != 1 not " + "supported yet"); + throw Py.TypeError("setslice with java.util.List and step != 1 not supported yet"); } int n = value.size(); list.ensureCapacity(start + n); @@ -200,23 +200,16 @@ } protected void setsliceIterable(int start, int stop, int step, PyObject value) { - PyObject iter; + PyObject[] seq; try { - iter = value.__iter__(); - } catch(PyException pye) { - if(Py.matchException(pye, Py.TypeError)) { + seq = Py.make_array(value); + } catch (PyException pye) { + if (Py.matchException(pye, Py.TypeError)) { throw Py.TypeError("can only assign an iterable"); } throw pye; } - PyObject next; - for(int j = 0; (next = iter.__iternext__()) != null; j += step) { - if(step < 0) { - list.pyset(start + j, next); - } else { - list.add(start + j, next); - } - } + setslicePySequence(start, stop, step, new PyList(seq)); } protected PyObject repeat(int count) { Modified: branches/asm/src/org/python/core/PySlice.java =================================================================== --- branches/asm/src/org/python/core/PySlice.java 2008-07-28 13:33:50 UTC (rev 5011) +++ branches/asm/src/org/python/core/PySlice.java 2008-07-28 19:46:39 UTC (rev 5012) @@ -188,8 +188,9 @@ */ public static PyObject[] indices2(PyObject obj, PyObject start, PyObject stop) { PyObject[] indices = new PyObject[2]; - int istart = start == null ? 0 : calculateSliceIndex(start); - int istop = stop == null ? PySystemState.maxint : calculateSliceIndex(stop); + int istart = (start == null || start == Py.None) ? 0 : calculateSliceIndex(start); + int istop = (stop == null || stop == Py.None) + ? PySystemState.maxint : calculateSliceIndex(stop); if (istart < 0 || istop < 0) { try { int len = obj.__len__(); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |