From: <le...@us...> - 2008-10-14 03:20:21
|
Revision: 5390 http://jython.svn.sourceforge.net/jython/?rev=5390&view=rev Author: leosoto Date: 2008-10-14 03:20:10 +0000 (Tue, 14 Oct 2008) Log Message: ----------- Now String is mapped to PyUnicode on ClassicPyObjectAdapter (instead of mapping it to PyString). Thus, for APIs which really need to return bytestrings (such as PyFile) we now have to explicitely declare the return type as PyString (and on code written in Python, use StringUtil.asPyString(String)). Fixes #1128 Modified Paths: -------------- trunk/jython/Lib/javapath.py trunk/jython/Lib/os.py trunk/jython/Lib/zlib.py trunk/jython/src/com/ziclix/python/sql/DataHandler.java trunk/jython/src/com/ziclix/python/sql/JDBC20DataHandler.java trunk/jython/src/com/ziclix/python/sql/Jython22DataHandler.java trunk/jython/src/com/ziclix/python/sql/handler/MySQLDataHandler.java trunk/jython/src/com/ziclix/python/sql/handler/PostgresqlDataHandler.java trunk/jython/src/org/python/core/PyFile.java trunk/jython/src/org/python/core/PyFunction.java trunk/jython/src/org/python/core/PyStringMap.java trunk/jython/src/org/python/core/PySystemState.java trunk/jython/src/org/python/core/PyTraceback.java trunk/jython/src/org/python/core/__builtin__.java trunk/jython/src/org/python/core/adapter/ClassicPyObjectAdapter.java trunk/jython/src/org/python/core/util/StringUtil.java trunk/jython/src/org/python/modules/binascii.java trunk/jython/src/org/python/modules/cPickle.java trunk/jython/src/org/python/modules/cStringIO.java trunk/jython/src/org/python/modules/struct.java trunk/jython/src/org/python/modules/time/Time.java Modified: trunk/jython/Lib/javapath.py =================================================================== --- trunk/jython/Lib/javapath.py 2008-10-14 01:47:30 UTC (rev 5389) +++ trunk/jython/Lib/javapath.py 2008-10-14 03:20:10 UTC (rev 5390) @@ -20,7 +20,9 @@ from java.lang import System import os +from org.python.core.util.StringUtil import asPyString + def _tostr(s, method): if isinstance(s, basestring): return s @@ -40,7 +42,7 @@ def dirname(path): """Return the directory component of a pathname""" path = _tostr(path, "dirname") - result = File(path).getParent() + result = asPyString(File(path).getParent()) if not result: if isabs(path): result = path # Must be root @@ -51,7 +53,7 @@ def basename(path): """Return the final component of a pathname""" path = _tostr(path, "basename") - return File(path).getName() + return asPyString(File(path).getName()) def split(path): """Split a pathname. @@ -128,7 +130,7 @@ if a == "": a = os.sep f = File(f, a) - return f.getPath() + return asPyString(f.getPath()) def normcase(path): """Normalize case of pathname. @@ -137,7 +139,7 @@ """ path = _tostr(path, "normcase") - return File(path).getPath() + return asPyString(File(path).getPath()) def commonprefix(m): "Given a list of pathnames, return the longest common leading component" @@ -197,7 +199,7 @@ if not c: return gethome() if c == os.sep: - return File(gethome(), path[2:]).getPath() + return asPyString(File(gethome(), path[2:]).getPath()) return path def getuser(): @@ -252,7 +254,7 @@ def _abspath(path): # Must use normpath separately because getAbsolutePath doesn't normalize # and getCanonicalPath would eliminate symlinks. - return normpath(File(sys.getPath(path)).getAbsolutePath()) + return normpath(asPyString(File(sys.getPath(path)).getAbsolutePath())) def realpath(path): """Return an absolute path normalized and symbolic links eliminated""" @@ -261,7 +263,7 @@ def _realpath(path): try: - return File(sys.getPath(path)).getCanonicalPath() + return asPyString(File(sys.getPath(path)).getCanonicalPath()) except java.io.IOException: return _abspath(path) Modified: trunk/jython/Lib/os.py =================================================================== --- trunk/jython/Lib/os.py 2008-10-14 01:47:30 UTC (rev 5389) +++ trunk/jython/Lib/os.py 2008-10-14 03:20:10 UTC (rev 5390) @@ -48,6 +48,7 @@ from java.io import File from org.python.core import PyFile from org.python.core.io import FileDescriptors, FileIO, IOBase +from org.python.core.util.StringUtil import asPyString # Mapping of: os._name: [name list, shell command list] _os_map = dict(nt=[ @@ -264,7 +265,7 @@ l = File(sys.getPath(path)).list() if l is None: raise OSError(0, 'No such directory', path) - return list(l) + return [asPyString(entry) for entry in l] def chmod(path, mode): """chmod(path, mode) @@ -629,7 +630,7 @@ from org.python.core.util import StringUtil rawio = FileDescriptors.get(fd) buf = _handle_oserror(rawio.read, buffersize) - return str(StringUtil.fromBytes(buf)) + return asPyString(StringUtil.fromBytes(buf)) def write(fd, string): """write(fd, string) -> byteswritten Modified: trunk/jython/Lib/zlib.py =================================================================== --- trunk/jython/Lib/zlib.py 2008-10-14 01:47:30 UTC (rev 5389) +++ trunk/jython/Lib/zlib.py 2008-10-14 03:20:10 UTC (rev 5390) @@ -1,8 +1,10 @@ import jarray, binascii from java.util.zip import Adler32, Deflater, Inflater -from java.lang import Long, String, StringBuffer +from java.lang import Long, String +from cStringIO import StringIO + class error(Exception): pass @@ -132,18 +134,19 @@ def _get_deflate_data(deflater): buf = jarray.zeros(1024, 'b') - sb = StringBuffer() + s = StringIO() while not deflater.finished(): l = deflater.deflate(buf) if l == 0: break - sb.append(String(buf, 0, 0, l)) - return sb.toString() + s.write(String(buf, 0, 0, l)) + s.seek(0) + return s.read() def _get_inflate_data(inflater, max_length=0): buf = jarray.zeros(1024, 'b') - sb = StringBuffer() + s = StringIO() total = 0 while not inflater.finished(): if max_length: @@ -154,7 +157,8 @@ break total += l - sb.append(String(buf, 0, 0, l)) + s.write(String(buf, 0, 0, l)) if max_length and total == max_length: break - return sb.toString() + s.seek(0) + return s.read() Modified: trunk/jython/src/com/ziclix/python/sql/DataHandler.java =================================================================== --- trunk/jython/src/com/ziclix/python/sql/DataHandler.java 2008-10-14 01:47:30 UTC (rev 5389) +++ trunk/jython/src/com/ziclix/python/sql/DataHandler.java 2008-10-14 03:20:10 UTC (rev 5390) @@ -177,7 +177,7 @@ case Types.LONGVARCHAR: if (object instanceof PyFile) { - object = new PyString(((PyFile) object).read()); + object = ((PyFile) object).read(); } String varchar = (String) object.__tojava__(String.class); @@ -192,7 +192,7 @@ default : if (object instanceof PyFile) { - object = new PyString(((PyFile) object).read()); + object = ((PyFile) object).read(); } stmt.setObject(index, object.__tojava__(Object.class), type); Modified: trunk/jython/src/com/ziclix/python/sql/JDBC20DataHandler.java =================================================================== --- trunk/jython/src/com/ziclix/python/sql/JDBC20DataHandler.java 2008-10-14 01:47:30 UTC (rev 5389) +++ trunk/jython/src/com/ziclix/python/sql/JDBC20DataHandler.java 2008-10-14 03:20:10 UTC (rev 5390) @@ -62,7 +62,7 @@ case Types.CLOB: if (object instanceof PyFile) { - object = new PyString(((PyFile) object).read()); + object = ((PyFile) object).read(); } String clob = (String) object.__tojava__(String.class); Modified: trunk/jython/src/com/ziclix/python/sql/Jython22DataHandler.java =================================================================== --- trunk/jython/src/com/ziclix/python/sql/Jython22DataHandler.java 2008-10-14 01:47:30 UTC (rev 5389) +++ trunk/jython/src/com/ziclix/python/sql/Jython22DataHandler.java 2008-10-14 03:20:10 UTC (rev 5390) @@ -163,7 +163,7 @@ case Types.LONGVARCHAR: if (object instanceof PyFile) { - object = new PyString(((PyFile) object).read()); + object = ((PyFile) object).read(); } String varchar = (String) object.__tojava__(String.class); @@ -178,7 +178,7 @@ default : if (object instanceof PyFile) { - object = new PyString(((PyFile) object).read()); + object = ((PyFile) object).read(); } stmt.setObject(index, object.__tojava__(Object.class), type); Modified: trunk/jython/src/com/ziclix/python/sql/handler/MySQLDataHandler.java =================================================================== --- trunk/jython/src/com/ziclix/python/sql/handler/MySQLDataHandler.java 2008-10-14 01:47:30 UTC (rev 5389) +++ trunk/jython/src/com/ziclix/python/sql/handler/MySQLDataHandler.java 2008-10-14 03:20:10 UTC (rev 5390) @@ -54,17 +54,19 @@ switch (type) { case Types.LONGVARCHAR: - String varchar; + // XXX: Only works with ASCII data! + byte[] bytes; if (object instanceof PyFile) { - varchar = ((PyFile) object).read(); + bytes = ((PyFile) object).read().toBytes(); } else { - varchar = (String) object.__tojava__(String.class); + String varchar = (String) object.__tojava__(String.class); + bytes = StringUtil.toBytes(varchar); } - InputStream stream = new ByteArrayInputStream(StringUtil.toBytes(varchar)); + InputStream stream = new ByteArrayInputStream(bytes); stream = new BufferedInputStream(stream); - stmt.setAsciiStream(index, stream, varchar.length()); + stmt.setAsciiStream(index, stream, bytes.length); break; default : Modified: trunk/jython/src/com/ziclix/python/sql/handler/PostgresqlDataHandler.java =================================================================== --- trunk/jython/src/com/ziclix/python/sql/handler/PostgresqlDataHandler.java 2008-10-14 01:47:30 UTC (rev 5389) +++ trunk/jython/src/com/ziclix/python/sql/handler/PostgresqlDataHandler.java 2008-10-14 03:20:10 UTC (rev 5390) @@ -111,7 +111,7 @@ String varchar; // Postgresql driver can't handle the setCharacterStream() method so use setObject() instead if (object instanceof PyFile) { - varchar = ((PyFile) object).read(); + varchar = ((PyFile) object).read().asString(); } else { varchar = (String) object.__tojava__(String.class); } Modified: trunk/jython/src/org/python/core/PyFile.java =================================================================== --- trunk/jython/src/org/python/core/PyFile.java 2008-10-14 01:47:30 UTC (rev 5389) +++ trunk/jython/src/org/python/core/PyFile.java 2008-10-14 03:20:10 UTC (rev 5390) @@ -286,16 +286,16 @@ } @ExposedMethod(defaults = {"-1"}) - final synchronized String file_read(int n) { + final synchronized PyString file_read(int n) { checkClosed(); - return file.read(n); + return new PyString(file.read(n)); } - public String read(int n) { + public PyString read(int n) { return file_read(n); } - public String read() { + public PyString read() { return file_read(-1); } @@ -310,16 +310,16 @@ } @ExposedMethod(defaults = {"-1"}) - final synchronized String file_readline(int max) { + final synchronized PyString file_readline(int max) { checkClosed(); - return file.readline(max); + return new PyString(file.readline(max)); } - public String readline(int max) { + public PyString readline(int max) { return file_readline(max); } - public String readline() { + public PyString readline() { return file_readline(-1); } Modified: trunk/jython/src/org/python/core/PyFunction.java =================================================================== --- trunk/jython/src/org/python/core/PyFunction.java 2008-10-14 01:47:30 UTC (rev 5389) +++ trunk/jython/src/org/python/core/PyFunction.java 2008-10-14 03:20:10 UTC (rev 5390) @@ -143,13 +143,13 @@ } @ExposedGet(name = "func_name") - public String getFuncName() { - return __name__; + public PyString getFuncName() { + return new PyString(__name__); } @ExposedSet(name = "func_name") - public void setFuncName(String func_name) { - __name__ = func_name; + public void setFuncName(PyString func_name) { + __name__ = func_name.asString(); } @ExposedGet(name = "func_doc") Modified: trunk/jython/src/org/python/core/PyStringMap.java =================================================================== --- trunk/jython/src/org/python/core/PyStringMap.java 2008-10-14 01:47:30 UTC (rev 5389) +++ trunk/jython/src/org/python/core/PyStringMap.java 2008-10-14 03:20:10 UTC (rev 5390) @@ -141,7 +141,9 @@ for (Entry<Object, PyObject> entry : table.entrySet()) { Object key = entry.getKey(); if (key instanceof String) { - buf.append(Py.java2py(key).__repr__().toString()); + // This is a bit complicated, but prevents us to duplicate + // PyString#__repr__ logic here. + buf.append(new PyString((String)key).__repr__().toString()); } else { buf.append(((PyObject)key).__repr__().toString()); } Modified: trunk/jython/src/org/python/core/PySystemState.java =================================================================== --- trunk/jython/src/org/python/core/PySystemState.java 2008-10-14 01:47:30 UTC (rev 5389) +++ trunk/jython/src/org/python/core/PySystemState.java 2008-10-14 03:20:10 UTC (rev 5390) @@ -41,7 +41,7 @@ private static final String JAR_URL_PREFIX = "jar:file:"; private static final String JAR_SEPARATOR = "!"; - public static String version = Version.getVersion(); + public static PyString version = new PyString(Version.getVersion()); public static int hexversion = ((Version.PY_MAJOR_VERSION << 24) | (Version.PY_MINOR_VERSION << 16) | (Version.PY_MICRO_VERSION << 8) | @@ -101,8 +101,8 @@ public PyList path_hooks; public PyObject path_importer_cache; - public static String platform = "java"; - public static String byteorder = "big"; + public static PyString platform = new PyString("java"); + public static PyString byteorder = new PyString("big"); public PyObject ps1 = new PyString(">>> "); public PyObject ps2 = new PyString("... "); @@ -288,8 +288,8 @@ } } - public String getdefaultencoding() { - return codecs.getDefaultEncoding(); + public PyString getdefaultencoding() { + return new PyString(codecs.getDefaultEncoding()); } public void setdefaultencoding(String encoding) { @@ -431,7 +431,7 @@ if (version.equals("12")) { version = "1.2"; } - platform = "java" + version; + platform = new PyString("java" + version); } private static void initRegistry(Properties preProperties, Properties postProperties, Modified: trunk/jython/src/org/python/core/PyTraceback.java =================================================================== --- trunk/jython/src/org/python/core/PyTraceback.java 2008-10-14 01:47:30 UTC (rev 5389) +++ trunk/jython/src/org/python/core/PyTraceback.java 2008-10-14 03:20:10 UTC (rev 5390) @@ -62,11 +62,12 @@ return null; } + String line = null; int i = 0; try { for (i = 0; i < tb_lineno; i++) { - line = pyFile.readline(); + line = pyFile.readline().asString(); if (line.equals("")) { break; } Modified: trunk/jython/src/org/python/core/__builtin__.java =================================================================== --- trunk/jython/src/org/python/core/__builtin__.java 2008-10-14 01:47:30 UTC (rev 5389) +++ trunk/jython/src/org/python/core/__builtin__.java 2008-10-14 03:20:10 UTC (rev 5390) @@ -1028,7 +1028,7 @@ private static PyString readline(PyObject file) { if (file instanceof PyFile) { - return new PyString(((PyFile) file).readline()); + return ((PyFile) file).readline(); } else { PyObject ret = file.invoke("readline"); if (!(ret instanceof PyString)) { Modified: trunk/jython/src/org/python/core/adapter/ClassicPyObjectAdapter.java =================================================================== --- trunk/jython/src/org/python/core/adapter/ClassicPyObjectAdapter.java 2008-10-14 01:47:30 UTC (rev 5389) +++ trunk/jython/src/org/python/core/adapter/ClassicPyObjectAdapter.java 2008-10-14 03:20:10 UTC (rev 5390) @@ -9,8 +9,8 @@ import org.python.core.PyLong; import org.python.core.PyObject; import org.python.core.PyProxy; -import org.python.core.PyString; import org.python.core.PyType; +import org.python.core.PyUnicode; /** * Implements the algorithm originally used in {@link Py#java2py} to adapt objects. @@ -60,7 +60,7 @@ add(new ClassAdapter(String.class) { public PyObject adapt(Object o) { - return new PyString((String) o); + return new PyUnicode((String) o); } }); Modified: trunk/jython/src/org/python/core/util/StringUtil.java =================================================================== --- trunk/jython/src/org/python/core/util/StringUtil.java 2008-10-14 01:47:30 UTC (rev 5389) +++ trunk/jython/src/org/python/core/util/StringUtil.java 2008-10-14 03:20:10 UTC (rev 5390) @@ -5,6 +5,7 @@ import java.nio.ByteBuffer; import org.python.core.Py; +import org.python.core.PyString; /** * String Utility methods. @@ -83,4 +84,8 @@ chars[0] = Character.toLowerCase(c0); return new String(chars); } + + public static PyString asPyString(String string) { + return new PyString(string); } +} Modified: trunk/jython/src/org/python/modules/binascii.java =================================================================== --- trunk/jython/src/org/python/modules/binascii.java 2008-10-14 01:47:30 UTC (rev 5389) +++ trunk/jython/src/org/python/modules/binascii.java 2008-10-14 03:20:10 UTC (rev 5390) @@ -272,12 +272,12 @@ * binary data. Lines normally contain 45 (binary) bytes, except for the * last line. Line data may be followed by whitespace. */ - public static String a2b_uu(String ascii_data) { + public static PyString a2b_uu(String ascii_data) { int leftbits = 0; int leftchar = 0; if (ascii_data.length() == 0) - return ""; + return new PyString(""); StringBuffer bin_data = new StringBuffer(); @@ -331,7 +331,7 @@ for (; i<bin_len; i++) bin_data.append((char)0); - return bin_data.toString(); + return new PyString(bin_data.toString()); } @@ -346,7 +346,7 @@ * is the converted line, including a newline char. The length of * <i>data</i> should be at most 45. */ - public static String b2a_uu(String bin_data) { + public static PyString b2a_uu(String bin_data) { int leftbits = 0; char this_ch; int leftchar = 0; @@ -379,7 +379,7 @@ } ascii_data.append('\n'); // Append a courtesy newline - return ascii_data.toString(); + return new PyString(ascii_data.toString()); } @@ -417,7 +417,7 @@ * Convert a block of base64 data back to binary and return the * binary data. More than one line may be passed at a time. */ - public static String a2b_base64(String ascii_data) { + public static PyString a2b_base64(String ascii_data) { int leftbits = 0; char this_ch; int leftchar = 0; @@ -468,7 +468,7 @@ if (leftbits != 0) { throw new PyException(Error, "Incorrect padding"); } - return bin_data.toString(); + return new PyString(bin_data.toString()); } @@ -482,7 +482,7 @@ * Convert binary data to a line of ASCII characters in base64 coding. * The return value is the converted line, including a newline char. */ - public static String b2a_base64(String bin_data) { + public static PyString b2a_base64(String bin_data) { int leftbits = 0; char this_ch; int leftchar = 0; @@ -516,7 +516,7 @@ } ascii_data.append('\n'); // Append a courtesy newline - return ascii_data.toString(); + return new PyString(ascii_data.toString()); } @@ -625,7 +625,7 @@ * resulting string. The argument should already be RLE-coded, and have a * length divisible by 3 (except possibly the last fragment). */ - public static String b2a_hqx(String bin_data) { + public static PyString b2a_hqx(String bin_data) { int leftbits = 0; char this_ch; int leftchar = 0; @@ -649,7 +649,7 @@ leftchar <<= (6-leftbits); ascii_data.append((char) table_b2a_hqx[leftchar & 0x3f]); } - return ascii_data.toString(); + return new PyString(ascii_data.toString()); } @@ -834,7 +834,7 @@ "This function is also available as \"hexlify()\"." ); - public static String b2a_hex(String argbuf) { + public static PyString b2a_hex(String argbuf) { int arglen = argbuf.length(); StringBuffer retbuf = new StringBuffer(arglen*2); @@ -845,12 +845,12 @@ retbuf.append(hexdigit[(ch >>> 4) & 0xF]); retbuf.append(hexdigit[ch & 0xF]); } - return retbuf.toString(); + return new PyString(retbuf.toString()); } - public static String hexlify(String argbuf) { + public static PyString hexlify(String argbuf) { return b2a_hex(argbuf); } @@ -864,7 +864,7 @@ ); - public static String a2b_hex(String argbuf) { + public static PyString a2b_hex(String argbuf) { int arglen = argbuf.length(); /* XXX What should we do about strings with an odd length? Should @@ -883,11 +883,11 @@ throw Py.TypeError("Non-hexadecimal digit found"); retbuf.append((char) ((top << 4) + bot)); } - return retbuf.toString(); + return new PyString(retbuf.toString()); } - public static String unhexlify(String argbuf) { + public static PyString unhexlify(String argbuf) { return a2b_hex(argbuf); } @@ -917,7 +917,7 @@ return val; } - public static String a2b_qp(PyObject[] arg, String[] kws) + public static PyString a2b_qp(PyObject[] arg, String[] kws) { ArgParser ap = new ArgParser("a2b_qp", arg, kws, new String[] {"s", "header"}); String s = ap.getString(0); @@ -951,8 +951,7 @@ sb.append(c); } } - - return sb.toString(); + return new PyString(sb.toString()); } final private static Pattern RN_TO_N = Pattern.compile("\r\n"); @@ -964,7 +963,7 @@ + "space at end of lines is. When istext is not set, \r and \n (CR/LF) are\n" + "both encoded. When quotetabs is set, space and tabs are encoded."); - public static String b2a_qp(PyObject[] arg, String[] kws) { + public static PyString b2a_qp(PyObject[] arg, String[] kws) { ArgParser ap = new ArgParser("b2a_qp", arg, kws, new String[] {"s", "quotetabs", "istext", "header"}); String s = ap.getString(0); boolean quotetabs = getIntFlagAsBool(ap, 1, 0, "an integer is required"); @@ -1034,7 +1033,7 @@ qpEscape(sb, c); } } - return sb.toString(); + return new PyString(sb.toString()); } private static boolean endOfLine(String s, String lineEnd, int i) { Modified: trunk/jython/src/org/python/modules/cPickle.java =================================================================== --- trunk/jython/src/org/python/modules/cPickle.java 2008-10-14 01:47:30 UTC (rev 5389) +++ trunk/jython/src/org/python/modules/cPickle.java 2008-10-14 03:20:10 UTC (rev 5390) @@ -614,7 +614,7 @@ * @param object a data object which should be pickled. * @returns a string representing the pickled object. */ - public static String dumps(PyObject object) { + public static PyString dumps(PyObject object) { return dumps(object, 0); } @@ -625,7 +625,7 @@ * @param protocol pickle protocol version (0 - text, 1 - pre-2.3 binary, 2 - 2.3) * @returns a string representing the pickled object. */ - public static String dumps(PyObject object, int protocol) { + public static PyString dumps(PyObject object, int protocol) { cStringIO.StringIO file = cStringIO.StringIO(); dump(object, file, protocol); return file.getvalue(); @@ -705,11 +705,11 @@ public void flush() {} public String read(int len) { - return file.read(len); + return file.read(len).asString(); } public String readlineNoNl() { - return file.readlineNoNl(); + return file.readlineNoNl().asString(); } } Modified: trunk/jython/src/org/python/modules/cStringIO.java =================================================================== --- trunk/jython/src/org/python/modules/cStringIO.java 2008-10-14 01:47:30 UTC (rev 5389) +++ trunk/jython/src/org/python/modules/cStringIO.java 2008-10-14 03:20:10 UTC (rev 5390) @@ -99,10 +99,10 @@ public PyObject __iternext__() { _complain_ifclosed(); - String r = readline(); - if(r.equals("")) + PyString r = readline(); + if (r.__len__() == 0) return null; - return new PyString(r); + return r; } /** @@ -176,7 +176,7 @@ * An empty string is returned when EOF is encountered immediately. * @returns A string containing the data. */ - public String read() { + public PyString read() { return read(-1); } @@ -190,7 +190,7 @@ * @returns A string containing the data read. */ - public synchronized String read(long size) { + public synchronized PyString read(long size) { _complain_ifclosed(); int size_int = _convert_to_int(size); int len = buf.length(); @@ -204,10 +204,9 @@ substr = buf.substring(pos, newpos); pos = newpos; } - return substr; + return new PyString(substr); } - /** * Read one entire line from the file. A trailing newline character * is kept in the string (but may be absent when a file ends with @@ -215,7 +214,7 @@ * An empty string is returned when EOF is hit immediately. * @returns data from the file up to and including the newline. */ - public String readline() { + public PyString readline() { return readline(-1); } @@ -229,12 +228,12 @@ * returned. * @returns data from the file up to and including the newline. */ - public synchronized String readline(long size) { + public synchronized PyString readline(long size) { _complain_ifclosed(); int size_int = _convert_to_int(size); int len = buf.length(); if (pos == len) { - return ""; + return new PyString(""); } int i = buf.indexOf("\n", pos); int newpos = (i < 0) ? len : i + 1; @@ -243,7 +242,7 @@ } String r = buf.substring(pos, newpos); pos = newpos; - return r; + return new PyString(r); } @@ -251,7 +250,7 @@ * Read and return a line without the trailing newline. * Usind by cPickle as an optimization. */ - public synchronized String readlineNoNl() { + public synchronized PyString readlineNoNl() { _complain_ifclosed(); int len = buf.length(); int i = buf.indexOf("\n", pos); @@ -260,7 +259,7 @@ pos = newpos; if (pos < len) // Skip the newline pos++; - return r; + return new PyString(r); } @@ -286,10 +285,10 @@ int sizehint_int = (int)sizehint; int total = 0; PyList lines = new PyList(); - String line = readline(); - while (line.length() > 0) { - lines.append(new PyString(line)); - total += line.length(); + PyString line = readline(); + while (line.__len__() > 0) { + lines.append(line); + total += line.__len__(); if (0 < sizehint_int && sizehint_int <= total) break; line = readline(); @@ -367,8 +366,8 @@ * before the StringIO object's close() method is called. * @return the contents of the StringIO. */ - public synchronized String getvalue() { - return buf.toString(); + public synchronized PyString getvalue() { + return new PyString(buf.toString()); } } Modified: trunk/jython/src/org/python/modules/struct.java =================================================================== --- trunk/jython/src/org/python/modules/struct.java 2008-10-14 01:47:30 UTC (rev 5389) +++ trunk/jython/src/org/python/modules/struct.java 2008-10-14 03:20:10 UTC (rev 5390) @@ -978,7 +978,7 @@ * to the given format. The arguments must match the * values required by the format exactly. */ - static public String pack(PyObject[] args) { + static public PyString pack(PyObject[] args) { if (args.length < 1) Py.TypeError("illegal argument type for built-in operation"); @@ -987,7 +987,7 @@ FormatDef[] f = whichtable(format); int size = calcsize(format, f); - return pack(format, f, size, 1, args).toString(); + return new PyString(pack(format, f, size, 1, args).toString()); } // xxx - may need to consider doing a generic arg parser here Modified: trunk/jython/src/org/python/modules/time/Time.java =================================================================== --- trunk/jython/src/org/python/modules/time/Time.java 2008-10-14 01:47:30 UTC (rev 5389) +++ trunk/jython/src/org/python/modules/time/Time.java 2008-10-14 03:20:10 UTC (rev 5390) @@ -308,11 +308,11 @@ return _timefields(parseTimeDoubleArg(arg), TimeZone.getTimeZone("GMT")); } - public static String ctime() { + public static PyString ctime() { return ctime(Py.None); } - public static String ctime(PyObject secs) { + public static PyString ctime(PyObject secs) { return asctime(localtime(secs)); } @@ -405,11 +405,11 @@ return yearstr.substring(yearstr.length()-2, yearstr.length()); } - public static String asctime() { + public static PyString asctime() { return asctime(localtime()); } - public static String asctime(PyTuple tup) { + public static PyString asctime(PyTuple tup) { StringBuffer buf = new StringBuffer(25); buf.append(enshortdays[item(tup, 6)]).append(' '); buf.append(enshortmonths[item(tup, 1)]).append(' '); @@ -421,7 +421,7 @@ buf.append(_twodigit(item(tup, 3))).append(':'); buf.append(_twodigit(item(tup, 4))).append(':'); buf.append(_twodigit(item(tup, 5))).append(' '); - return buf.append(item(tup, 0)).toString(); + return new PyString(buf.append(item(tup, 0)).toString()); } public static String locale_asctime(PyTuple tup) { @@ -454,11 +454,11 @@ // writable but ignore its value? public static final int accept2dyear = 0; - public static String strftime(String format) { + public static PyString strftime(String format) { return strftime(format, localtime()); } - public static String strftime(String format, PyTuple tup) { + public static PyString strftime(String format, PyTuple tup) { checkLocale(); // Immediately validate the tuple @@ -651,7 +651,18 @@ lastc = i+1; i++; } - return s; + // FIXME: This have problems with localized data: + // $ LANG=es_ES.UTF-8 jythont -c "import time; print time.strftime('%A')" + // s?bado + // + // On the other hand, returning unicode would break some doctests + // and will give problems when the return value of strftime is + // mixed with other strings and then the final result is stored + // on places which only support str and not unicode (such as + // os.environ) + // + // TODO: Check how CPython deals with this problem. + return new PyString(s); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |