Index: src/org/python/core/PySequenceIter.java =================================================================== --- src/org/python/core/PySequenceIter.java (revision 3455) +++ src/org/python/core/PySequenceIter.java (working copy) @@ -10,10 +10,12 @@ } public PyObject __iternext__() { - try { - return seq.__finditem__(idx++); + try { + return seq.__getitem__(new PyInteger(idx++)); } catch (PyException exc) { - if (Py.matchException(exc, Py.StopIteration)) + if (Py.matchException(exc, Py.StopIteration) || + Py.matchException(exc, Py.IndexError) || + Py.matchException(exc, Py.KeyError)) return null; throw exc; } Index: src/org/python/core/PyString.java =================================================================== --- src/org/python/core/PyString.java (revision 3455) +++ src/org/python/core/PyString.java (working copy) @@ -321,7 +321,7 @@ } public PyObject __call__() { - return new PyString(((PyString)self).str_toString()); + return ((PyString)self).str_toString(); } } @@ -1624,11 +1624,11 @@ } public String toString() { - return str_toString(); + return string; } - final String str_toString() { - return string; + final PyString str_toString() { + return new PyString(encode_UnicodeEscape(string, true)); } public String internedString() { @@ -1642,7 +1642,7 @@ } public PyString __repr__() { - return new PyString(encode_UnicodeEscape(string, true)); + return str_toString(); } private static char[] hexdigit = "0123456789ABCDEF".toCharArray(); @@ -2028,48 +2028,48 @@ } public PyObject __lt__(PyObject other) { - String s = coerce(other); + return str___lt__(other); + } + + final PyObject str___lt__(PyObject other){ + String s = coerce(other); if (s == null) return null; return string.compareTo(s) < 0 ? Py.True : Py.False; } - - final PyObject str___lt__(PyObject other){ - return __lt__(other); - } public PyObject __le__(PyObject other) { - String s = coerce(other); + return str___le__(other); + } + + final PyObject str___le__(PyObject other){ + String s = coerce(other); if (s == null) return null; return string.compareTo(s) <= 0 ? Py.True : Py.False; } - - final PyObject str___le__(PyObject other){ - return __le__(other); - } public PyObject __gt__(PyObject other) { - String s = coerce(other); + return str___gt__(other); + } + + final PyObject str___gt__(PyObject other){ + String s = coerce(other); if (s == null) return null; return string.compareTo(s) > 0 ? Py.True : Py.False; } - - final PyObject str___gt__(PyObject other){ - return __gt__(other); - } public PyObject __ge__(PyObject other) { - String s = coerce(other); + return str___ge__(other); + } + + final PyObject str___ge__(PyObject other) { + String s = coerce(other); if (s == null) return null; return string.compareTo(s) >= 0 ? Py.True : Py.False; } - - final PyObject str___ge__(PyObject other){ - return __ge__(other); - } private static String coerce(PyObject o) { if (o instanceof PyString) Index: src/org/python/core/PyUnicode.java =================================================================== --- src/org/python/core/PyUnicode.java (revision 3455) +++ src/org/python/core/PyUnicode.java (working copy) @@ -232,7 +232,7 @@ } public PyObject __call__() { - return new PyString(((PyUnicode)self).unicode_toString()); + return ((PyUnicode)self).unicode_toString(); } } @@ -1526,11 +1526,14 @@ } public PyString __repr__() { - return new PyUnicode("u" + encode_UnicodeEscape(string, true)); + return unicode_toString(); } - public String unicode_toString() { - return "u" + str_toString(); + public PyUnicode unicode_toString() { + if (string.indexOf("u'") != -1 || string.indexOf("u\\") != -1) + return new PyUnicode(encode_UnicodeEscape(string, true)); + else + return new PyUnicode("u" + encode_UnicodeEscape(string, true)); } final int unicode___cmp__(PyObject other) { Index: src/org/python/core/PyStringDerived.java =================================================================== --- src/org/python/core/PyStringDerived.java (revision 3455) +++ src/org/python/core/PyStringDerived.java (working copy) @@ -689,18 +689,6 @@ return super.__int__(); } - public String toString() { - PyType self_type=getType(); - PyObject impl=self_type.lookup("__repr__"); - if (impl!=null) { - PyObject res=impl.__get__(this,self_type).__call__(); - if (!(res instanceof PyString)) - throw Py.TypeError("__repr__ should return a string"); - return((PyString)res).toString(); - } - return super.toString(); - } - public int hashCode() { PyType self_type=getType(); PyObject impl=self_type.lookup("__hash__"); Index: src/org/python/core/PyUnicodeDerived.java =================================================================== --- src/org/python/core/PyUnicodeDerived.java (revision 3455) +++ src/org/python/core/PyUnicodeDerived.java (working copy) @@ -689,18 +689,6 @@ return super.__int__(); } - public String toString() { - PyType self_type=getType(); - PyObject impl=self_type.lookup("__repr__"); - if (impl!=null) { - PyObject res=impl.__get__(this,self_type).__call__(); - if (!(res instanceof PyString)) - throw Py.TypeError("__repr__ should return a string"); - return((PyString)res).toString(); - } - return super.toString(); - } - public int hashCode() { PyType self_type=getType(); PyObject impl=self_type.lookup("__hash__"); Index: src/org/python/core/__builtin__.java =================================================================== --- src/org/python/core/__builtin__.java (revision 3455) +++ src/org/python/core/__builtin__.java (working copy) @@ -555,26 +555,35 @@ public static void execfile(String name) { execfile(name, null, null); } - + public static PyObject filter(PyObject f, PyString s) { if (f == Py.None) { - return s; + if (!(s instanceof PyStringDerived || s instanceof PyUnicodeDerived) ) + return s; } - PyObject[] args = new PyObject[1]; - char[] chars = s.toString().toCharArray(); - int i; - int j; - int n = chars.length; - for (i = 0, j = 0; i < n; i++) { - args[0] = Py.makeCharacter(chars[i]); - if (!f.__call__(args).__nonzero__()) { - continue; - } - chars[j++] = chars[i]; + + PyObject iter = s.__iter__(); + StringBuffer buffer = new StringBuffer(); + + for (PyObject item = null; (item = iter.__iternext__()) != null;) { + if (!(item instanceof PyString)) + throw Py.TypeError("__getitem__ returned different type"); + if (f == Py.None) { + if (!item.__nonzero__()) { + continue; + } + } else if (!f.__call__(item).__nonzero__()) { + continue; + } + buffer.append(item); } - return new PyString(new String(chars, 0, j)); + + if (s instanceof PyUnicode) + return new PyUnicode(buffer.toString()); + + return new PyString(buffer.toString()); } - + public static PyObject filter(PyObject f, PyObject l) { if (l instanceof PyString) { return filter(f, (PyString) l); Index: Lib/test/test_builtin.py =================================================================== --- Lib/test/test_builtin.py (revision 3455) +++ Lib/test/test_builtin.py (working copy) @@ -323,11 +323,7 @@ self.assertEqual(filter(None, "12"), "12") self.assertEqual(filter(lambda x: x>="3", "1234"), "34") self.assertRaises(TypeError, filter, 42, "12") - # Jython transition 2.3 - # Jython uses special pyget method instead of __getitem__ for filter, __iter__ - # http://jython.org/1768970 - if test.test_support.is_jython: - return + class badstr(str): def __getitem__(self, index): raise ValueError