From: <pj...@us...> - 2009-08-17 01:43:26
|
Revision: 6686 http://jython.svn.sourceforge.net/jython/?rev=6686&view=rev Author: pjenvey Date: 2009-08-17 01:43:09 +0000 (Mon, 17 Aug 2009) Log Message: ----------- fix writelines' handling of non-strs and not resetting softspace Modified Paths: -------------- trunk/jython/src/org/python/core/PyFile.java Modified: trunk/jython/src/org/python/core/PyFile.java =================================================================== --- trunk/jython/src/org/python/core/PyFile.java 2009-08-17 00:17:36 UTC (rev 6685) +++ trunk/jython/src/org/python/core/PyFile.java 2009-08-17 01:43:09 UTC (rev 6686) @@ -355,18 +355,7 @@ @ExposedMethod(doc = BuiltinDocs.file_write_doc) final void file_write(PyObject obj) { - if (obj instanceof PyUnicode) { - // Call __str__ on unicode objects to encode them before writing - file_write(obj.__str__().string); - } else if (obj instanceof PyString) { - file_write(((PyString)obj).string); - } else if (binary && obj instanceof PyArray) { - file_write(((PyArray)obj).tostring()); - } else { - throw Py.TypeError(String.format("argument 1 must be string or %sbuffer, not %.200s", - binary ? "" : "read-only character ", - obj.getType().fastGetName())); - } + file_write(asWritable(obj, null)); } final synchronized void file_write(String string) { @@ -383,13 +372,9 @@ final synchronized void file_writelines(PyObject lines) { checkClosed(); PyObject iter = Py.iter(lines, "writelines() requires an iterable argument"); - - PyObject item = null; - while ((item = iter.__iternext__()) != null) { - if (!(item instanceof PyString)) { - throw Py.TypeError("writelines() argument must be a sequence of strings"); - } - file.write(item.toString()); + for (PyObject item = null; (item = iter.__iternext__()) != null;) { + softspace = false; + file.write(asWritable(item, "writelines() argument must be a sequence of strings")); } } @@ -397,6 +382,25 @@ file_writelines(lines); } + /** + * Return a String for writing to the underlying file from obj. + */ + private String asWritable(PyObject obj, String message) { + if (obj instanceof PyUnicode) { + return ((PyUnicode)obj).encode(); + } else if (obj instanceof PyString) { + return ((PyString)obj).string; + } else if (binary && obj instanceof PyArray) { + return ((PyArray)obj).tostring(); + } + if (message == null) { + message = String.format("argument 1 must be string or %sbuffer, not %.200s", + binary ? "" : "read-only character ", + obj.getType().fastGetName()); + } + throw Py.TypeError(message); + } + @ExposedMethod(doc = BuiltinDocs.file_tell_doc) final synchronized long file_tell() { checkClosed(); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |