From: <fwi...@us...> - 2008-08-22 23:02:37
|
Revision: 5235 http://jython.svn.sourceforge.net/jython/?rev=5235&view=rev Author: fwierzbicki Date: 2008-08-22 23:02:32 +0000 (Fri, 22 Aug 2008) Log Message: ----------- Merged revisions 5227,5229,5231-5234 via svnmerge from https://jython.svn.sourceforge.net/svnroot/jython/trunk/jython ........ r5227 | pjenvey | 2008-08-20 19:01:42 -0400 (Wed, 20 Aug 2008) | 3 lines rewrite the subx joiner yet again: this time exactly as CPython does it, with __getslice__ and join. fixes a string subclass corner case ........ r5229 | leosoto | 2008-08-20 23:23:28 -0400 (Wed, 20 Aug 2008) | 1 line Fixing #1095 for old-style classes. Thanks to Anselm Kruls for the patch. ........ r5231 | pjenvey | 2008-08-21 15:44:29 -0400 (Thu, 21 Aug 2008) | 3 lines make cStringIO thread safe as unfortunately the CPython GIL guarantees it, as does the pure python version. add cStringIO.In/OutputType ........ r5232 | pjenvey | 2008-08-21 19:59:46 -0400 (Thu, 21 Aug 2008) | 1 line document TypeError vs AttributeError here ........ r5233 | fwierzbicki | 2008-08-22 11:31:56 -0400 (Fri, 22 Aug 2008) | 2 lines Switch "type"->"kind" for the compile parameter value which specifies the kind of parse/compile "exec", "eval", or "single". CPython's source uses "kind" and other parts of Jython use this term as well. ........ r5234 | pjenvey | 2008-08-22 16:16:59 -0400 (Fri, 22 Aug 2008) | 3 lines fix unicode __add__ and repeat always forcing the basic plane on the result -- can't optimize for strs when unicode also uses these methods ........ Modified Paths: -------------- branches/nowalker/Lib/test/test_descr_jy.py branches/nowalker/Lib/test/test_re_jy.py branches/nowalker/src/org/python/core/Py.java branches/nowalker/src/org/python/core/PyInstance.java branches/nowalker/src/org/python/core/PyObject.java branches/nowalker/src/org/python/core/PyString.java branches/nowalker/src/org/python/core/__builtin__.java branches/nowalker/src/org/python/modules/cStringIO.java branches/nowalker/src/org/python/modules/sre/PatternObject.java Added Paths: ----------- branches/nowalker/Lib/test/test_unicode_jy.py Property Changed: ---------------- branches/nowalker/ Property changes on: branches/nowalker ___________________________________________________________________ Modified: svnmerge-integrated - /trunk/jython:1-5221 + /trunk/jython:1-5234 Modified: branches/nowalker/Lib/test/test_descr_jy.py =================================================================== --- branches/nowalker/Lib/test/test_descr_jy.py 2008-08-22 20:16:59 UTC (rev 5234) +++ branches/nowalker/Lib/test/test_descr_jy.py 2008-08-22 23:02:32 UTC (rev 5235) @@ -326,16 +326,34 @@ def __getattr__(self, name): raise BarAttributeError + class BarClassic: + def __getattr__(self, name): + raise BarAttributeError + class Foo(object): def __getattr__(self, name): raise AttributeError("Custom message") + + class FooClassic: + def __getattr__(self, name): + raise AttributeError("Custom message") + self.assertRaises(BarAttributeError, lambda: Bar().x) + self.assertRaises(BarAttributeError, lambda: BarClassic().x) + try: Foo().x self.assert_(False) # Previous line should raise AttributteError except AttributeError, e: self.assertEquals("Custom message", str(e)) + try: + FooClassic().x + self.assert_(False) # Previous line should raise AttributteError + except AttributeError, e: + self.assertEquals("Custom message", str(e)) + + def test_main(): test_support.run_unittest(TestDescrTestCase, SubclassDescrTestCase, Modified: branches/nowalker/Lib/test/test_re_jy.py =================================================================== --- branches/nowalker/Lib/test/test_re_jy.py 2008-08-22 20:16:59 UTC (rev 5234) +++ branches/nowalker/Lib/test/test_re_jy.py 2008-08-22 23:02:32 UTC (rev 5235) @@ -8,6 +8,23 @@ result = re.sub('', lambda match : None, 'foo') self.assertEqual(result, 'foo') self.assert_(isinstance(result, str)) + + def test_sub_with_subclasses(self): + class Foo(unicode): + def join(self, items): + return Foo(unicode.join(self, items)) + result = re.sub('bar', 'baz', Foo('bar')) + self.assertEqual(result, u'baz') + self.assertEqual(type(result), unicode) + + class Foo2(unicode): + def join(self, items): + return Foo2(unicode.join(self, items)) + def __getslice__(self, start, stop): + return Foo2(unicode.__getslice__(self, start, stop)) + result = re.sub('bar', 'baz', Foo2('bar')) + self.assertEqual(result, Foo2('baz')) + self.assert_(isinstance(result, Foo2)) def test_unkown_groupname(self): self.assertRaises(IndexError, Copied: branches/nowalker/Lib/test/test_unicode_jy.py (from rev 5234, trunk/jython/Lib/test/test_unicode_jy.py) =================================================================== --- branches/nowalker/Lib/test/test_unicode_jy.py (rev 0) +++ branches/nowalker/Lib/test/test_unicode_jy.py 2008-08-22 23:02:32 UTC (rev 5235) @@ -0,0 +1,28 @@ +"""Misc unicode tests + +Made for Jython. +""" +import re +import unittest +from test import test_support + +class UnicodeTestCase(unittest.TestCase): + + def test_simplejson_plane_bug(self): + # a bug exposed by simplejson: unicode __add__ was always + # forcing the basic plane + chunker = re.compile(r'(.*?)(["\\\x00-\x1f])', re.VERBOSE | re.MULTILINE | re.DOTALL) + orig = u'z\U0001d120x' + quoted1 = u'"z\U0001d120x"' + quoted2 = '"' + orig + '"' + # chunker re gives different results depending on the plane + self.assertEqual(chunker.match(quoted1, 1).groups(), (orig, u'"')) + self.assertEqual(chunker.match(quoted2, 1).groups(), (orig, u'"')) + + +def test_main(): + test_support.run_unittest(UnicodeTestCase) + + +if __name__ == "__main__": + test_main() Modified: branches/nowalker/src/org/python/core/Py.java =================================================================== --- branches/nowalker/src/org/python/core/Py.java 2008-08-22 20:16:59 UTC (rev 5234) +++ branches/nowalker/src/org/python/core/Py.java 2008-08-22 23:02:32 UTC (rev 5235) @@ -1641,8 +1641,8 @@ } public static PyObject compile(InputStream istream, String filename, - String type) { - return compile_flags(istream, filename, type, null); + String kind) { + return compile_flags(istream, filename, kind, null); } // with compiler-flags @@ -1664,15 +1664,15 @@ } public static PyObject compile_flags(InputStream istream, String filename, - String type,CompilerFlags cflags) + String kind,CompilerFlags cflags) { - modType node = ParserFacade.parse(istream, type, filename, cflags); + modType node = ParserFacade.parse(istream, kind, filename, cflags); if (cflags != null && cflags.only_ast) { return Py.java2py(node); } boolean printResults = false; - if (type.equals("single")) { + if (kind.equals("single")) { printResults = true; } return Py.compile_flags(node, getName(), filename, true, printResults, cflags); @@ -1680,7 +1680,7 @@ public static PyObject compile_flags(String data, String filename, - String type, + String kind, CompilerFlags cflags) { if (data.contains("\0")) { @@ -1695,7 +1695,7 @@ } return Py.compile_flags(new ByteArrayInputStream(bytes), filename, - type, + kind, cflags); } Modified: branches/nowalker/src/org/python/core/PyInstance.java =================================================================== --- branches/nowalker/src/org/python/core/PyInstance.java 2008-08-22 20:16:59 UTC (rev 5234) +++ branches/nowalker/src/org/python/core/PyInstance.java 2008-08-22 23:02:32 UTC (rev 5235) @@ -201,10 +201,19 @@ } public PyObject __findattr_ex__(String name) { - return __findattr__(name, false); + return __findattr_ex__(name, false); } public PyObject __findattr__(String name, boolean stopAtJava) { + try { + return __findattr_ex__(name, stopAtJava); + } catch (PyException exc) { + if (Py.matchException(exc, Py.AttributeError)) return null; + throw exc; + } + } + + protected PyObject __findattr_ex__(String name, boolean stopAtJava) { PyObject result = ifindlocal(name); if (result != null) return result; @@ -234,12 +243,7 @@ if (getter == null) return null; - try { - return getter.__call__(this, new PyString(name)); - } catch (PyException exc) { - if (Py.matchException(exc, Py.AttributeError)) return null; - throw exc; - } + return getter.__call__(this, new PyString(name)); } public boolean isCallable() { Modified: branches/nowalker/src/org/python/core/PyObject.java =================================================================== --- branches/nowalker/src/org/python/core/PyObject.java 2008-08-22 20:16:59 UTC (rev 5234) +++ branches/nowalker/src/org/python/core/PyObject.java 2008-08-22 23:02:32 UTC (rev 5235) @@ -829,6 +829,9 @@ } public void readonlyAttributeError(String name) { + // XXX: Should be an AttributeError but CPython throws TypeError for read only + // member descriptors (in structmember.c::PyMember_SetOne), which is expected by a + // few tests. fixed in py3k: http://bugs.python.org/issue1687163 throw Py.TypeError("readonly attribute"); } Modified: branches/nowalker/src/org/python/core/PyString.java =================================================================== --- branches/nowalker/src/org/python/core/PyString.java 2008-08-22 20:16:59 UTC (rev 5234) +++ branches/nowalker/src/org/python/core/PyString.java 2008-08-22 23:02:32 UTC (rev 5235) @@ -670,7 +670,7 @@ for(int i = 0; i < count; i++) { string.getChars(0, s, new_chars, i * s); } - return createInstance(new String(new_chars), true); + return createInstance(new String(new_chars)); } @Override @@ -711,7 +711,7 @@ if (generic_other instanceof PyUnicode) { return new PyUnicode(result); } - return createInstance(result, true); + return createInstance(result); } else return null; } Modified: branches/nowalker/src/org/python/core/__builtin__.java =================================================================== --- branches/nowalker/src/org/python/core/__builtin__.java 2008-08-22 20:16:59 UTC (rev 5234) +++ branches/nowalker/src/org/python/core/__builtin__.java 2008-08-22 23:02:32 UTC (rev 5235) @@ -455,15 +455,15 @@ throw Py.TypeError("number coercion failed"); } - public static PyObject compile(String data, String filename, String type) { - return Py.compile_flags(data, filename, type, Py.getCompilerFlags()); + public static PyObject compile(String data, String filename, String kind) { + return Py.compile_flags(data, filename, kind, Py.getCompilerFlags()); } - public static PyObject compile(String data, String filename, String type, int flags, boolean dont_inherit) { + public static PyObject compile(String data, String filename, String kind, int flags, boolean dont_inherit) { if ((flags & ~PyTableCode.CO_ALL_FEATURES) != 0) { throw Py.ValueError("compile(): unrecognised flags"); } - return Py.compile_flags(data, filename, type, Py.getCompilerFlags(flags, dont_inherit)); + return Py.compile_flags(data, filename, kind, Py.getCompilerFlags(flags, dont_inherit)); } public static void delattr(PyObject o, String n) { Modified: branches/nowalker/src/org/python/modules/cStringIO.java =================================================================== --- branches/nowalker/src/org/python/modules/cStringIO.java 2008-08-22 20:16:59 UTC (rev 5234) +++ branches/nowalker/src/org/python/modules/cStringIO.java 2008-08-22 23:02:32 UTC (rev 5235) @@ -16,6 +16,7 @@ import org.python.core.PyList; import org.python.core.PyObject; import org.python.core.PyString; +import org.python.core.PyType; /** * This module implements a file-like class, StringIO, that reads and @@ -37,6 +38,9 @@ public static final int SEEK_CUR = 1; public static final int SEEK_END = 2; } + + public static PyType InputType = PyType.fromClass(StringIO.class); + public static PyType OutputType = PyType.fromClass(StringIO.class); public static StringIO StringIO() { return new StringIO(); @@ -133,7 +137,7 @@ * @param pos the position in the file. * @param mode; 0=from the start, 1=relative, 2=from the end. */ - public void seek(long pos, int mode) { + public synchronized void seek(long pos, int mode) { _complain_ifclosed(); switch (mode) { case os.SEEK_CUR: @@ -152,7 +156,7 @@ /** * Reset the file position to the beginning of the file. */ - public void reset() { + public synchronized void reset() { pos = 0; } @@ -160,7 +164,7 @@ * Return the file position. * @returns the position in the file. */ - public int tell() { + public synchronized int tell() { _complain_ifclosed(); return pos; } @@ -186,7 +190,7 @@ * @returns A string containing the data read. */ - public String read(long size) { + public synchronized String read(long size) { _complain_ifclosed(); int size_int = _convert_to_int(size); int len = buf.length(); @@ -225,7 +229,7 @@ * returned. * @returns data from the file up to and including the newline. */ - public String readline(long size) { + public synchronized String readline(long size) { _complain_ifclosed(); int size_int = _convert_to_int(size); int len = buf.length(); @@ -247,7 +251,7 @@ * Read and return a line without the trailing newline. * Usind by cPickle as an optimization. */ - public String readlineNoNl() { + public synchronized String readlineNoNl() { _complain_ifclosed(); int len = buf.length(); int i = buf.indexOf("\n", pos); @@ -303,7 +307,7 @@ /** * truncate the file at the position pos. */ - public void truncate(long pos) { + public synchronized void truncate(long pos) { int pos_int = _convert_to_int(pos); if (pos_int < 0) pos_int = this.pos; @@ -320,7 +324,7 @@ write(obj.toString()); } - public void write(String s) { + public synchronized void write(String s) { _complain_ifclosed(); buf.setLength(pos); int newpos = pos + s.length(); @@ -332,7 +336,7 @@ * Write a char to the file. Used by cPickle as an optimization. * @param ch The data to write. */ - public void writeChar(char ch) { + public synchronized void writeChar(char ch) { int len = buf.length(); if (len <= pos) buf.setLength(pos + 1); @@ -363,7 +367,7 @@ * before the StringIO object's close() method is called. * @return the contents of the StringIO. */ - public String getvalue() { + public synchronized String getvalue() { return buf.toString(); } Modified: branches/nowalker/src/org/python/modules/sre/PatternObject.java =================================================================== --- branches/nowalker/src/org/python/modules/sre/PatternObject.java 2008-08-22 20:16:59 UTC (rev 5234) +++ branches/nowalker/src/org/python/modules/sre/PatternObject.java 2008-08-22 23:02:32 UTC (rev 5235) @@ -118,7 +118,7 @@ SRE_STATE state = new SRE_STATE(string, 0, Integer.MAX_VALUE, flags); - StringBuilder buf = new StringBuilder(); + PyList list = new PyList(); int n = 0; int i = 0; @@ -137,7 +137,7 @@ if (i < b) { /* get segment before this match */ - buf.append(string.substring(i, b)); + list.append(string.__getslice__(Py.newInteger(i), Py.newInteger(b))); } if (! (i == b && i == e && n > 0)) { PyObject item; @@ -150,7 +150,7 @@ } if (item != Py.None) { - buf.append(item.toString()); + list.append(item); } i = e; n++; @@ -163,25 +163,23 @@ state.start = state.ptr; } if (i < state.endpos) { - buf.append(string.substring(i, state.endpos)); + list.append(string.__getslice__(Py.newInteger(i), Py.newInteger(state.endpos))); } - // Follows rules enumerated in test_re.test_bug_1140 - PyString outstring; - if (buf.length() == 0) { - outstring = instring.createInstance(buf.toString()); - } else if (template instanceof PyUnicode || instring instanceof PyUnicode) { - outstring = Py.newUnicode(buf.toString()); - } else { - outstring = Py.newString(buf.toString()); - } - + PyObject outstring = join_list(list, string); if (subn) { return new PyTuple(outstring, Py.newInteger(n)); } return outstring; } + private PyObject join_list(PyList list, PyString string) { + PyObject joiner = string.__getslice__(Py.Zero, Py.Zero); + if (list.size() == 0) { + return joiner; + } + return joiner.__getattr__("join").__call__(list); + } public PyObject split(PyObject[] args, String[] kws) { ArgParser ap = new ArgParser("split", args, kws, This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |