From: <pj...@us...> - 2008-10-20 23:53:49
|
Revision: 5487 http://jython.svn.sourceforge.net/jython/?rev=5487&view=rev Author: pjenvey Date: 2008-10-20 23:53:47 +0000 (Mon, 20 Oct 2008) Log Message: ----------- __iter__ is just another file_self call 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 2008-10-20 21:37:06 UTC (rev 5486) +++ trunk/jython/src/org/python/core/PyFile.java 2008-10-20 23:53:47 UTC (rev 5487) @@ -349,16 +349,6 @@ return file_readlines(0); } - public PyObject __iter__() { - return file___iter__(); - } - - @ExposedMethod - final PyObject file___iter__() { - checkClosed(); - return this; - } - public PyObject __iternext__() { return file___iternext__(); } @@ -385,7 +375,7 @@ return file_next(); } - @ExposedMethod(names = {"__enter__", "xreadlines"}) + @ExposedMethod(names = {"__enter__", "__iter__", "xreadlines"}) final PyObject file_self() { checkClosed(); return this; @@ -395,6 +385,10 @@ return file_self(); } + public PyObject __iter__() { + return file_self(); + } + public PyObject xreadlines() { return file_self(); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2009-08-16 14:26:13
|
Revision: 6677 http://jython.svn.sourceforge.net/jython/?rev=6677&view=rev Author: fwierzbicki Date: 2009-08-16 14:26:06 +0000 (Sun, 16 Aug 2009) Log Message: ----------- Fix for http://bugs.jython.org/issue1439: Can't write() array.array 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-16 10:28:49 UTC (rev 6676) +++ trunk/jython/src/org/python/core/PyFile.java 2009-08-16 14:26:06 UTC (rev 6677) @@ -354,8 +354,12 @@ file_write(o.__str__().string); } else if (o instanceof PyString) { file_write(((PyString)o).string); + } else if (o instanceof PyArray) { + ((PyArray)o).tofile(this); } else { - throw Py.TypeError("write requires a string as its argument"); + //XXX: Match CPython's error message better + // "argument 1 must be string or read-only buffer, not {type}" + throw Py.TypeError("write requires a string or read-only buffer as its argument"); } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <pj...@us...> - 2009-08-16 23:14:40
|
Revision: 6683 http://jython.svn.sourceforge.net/jython/?rev=6683&view=rev Author: pjenvey Date: 2009-08-16 22:40:47 +0000 (Sun, 16 Aug 2009) Log Message: ----------- restrict array writes to binary mode 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-16 22:25:58 UTC (rev 6682) +++ trunk/jython/src/org/python/core/PyFile.java 2009-08-16 22:40:47 UTC (rev 6683) @@ -354,12 +354,12 @@ file_write(o.__str__().string); } else if (o instanceof PyString) { file_write(((PyString)o).string); - } else if (o instanceof PyArray) { - ((PyArray)o).tofile(this); + } else if (binary && o instanceof PyArray) { + file_write(((PyArray)o).tostring()); } else { - //XXX: Match CPython's error message better - // "argument 1 must be string or read-only buffer, not {type}" - throw Py.TypeError("write requires a string or read-only buffer as its argument"); + throw Py.TypeError(String.format("argument 1 must be string or %sbuffer, not %.200s", + binary ? "" : "read-only character ", + o.getType().fastGetName())); } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: Frank W. <fwi...@gm...> - 2009-08-17 00:23:29
|
Philip, Thanks for the improvements! -Frank On Sun, Aug 16, 2009 at 6:40 PM, <pj...@us...> wrote: > Revision: 6683 > http://jython.svn.sourceforge.net/jython/?rev=6683&view=rev > Author: pjenvey > Date: 2009-08-16 22:40:47 +0000 (Sun, 16 Aug 2009) > > Log Message: > ----------- > restrict array writes to binary mode > > 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-16 22:25:58 UTC (rev 6682) > +++ trunk/jython/src/org/python/core/PyFile.java 2009-08-16 22:40:47 UTC (rev 6683) > @@ -354,12 +354,12 @@ > file_write(o.__str__().string); > } else if (o instanceof PyString) { > file_write(((PyString)o).string); > - } else if (o instanceof PyArray) { > - ((PyArray)o).tofile(this); > + } else if (binary && o instanceof PyArray) { > + file_write(((PyArray)o).tostring()); > } else { > - //XXX: Match CPython's error message better > - // "argument 1 must be string or read-only buffer, not {type}" > - throw Py.TypeError("write requires a string or read-only buffer as its argument"); > + throw Py.TypeError(String.format("argument 1 must be string or %sbuffer, not %.200s", > + binary ? "" : "read-only character ", > + o.getType().fastGetName())); > } > } > > > > This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. > > ------------------------------------------------------------------------------ > Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day > trial. Simplify your report design, integration and deployment - and focus on > what you do best, core application coding. Discover what's new with > Crystal Reports now. http://p.sf.net/sfu/bobj-july > _______________________________________________ > Jython-checkins mailing list > Jyt...@li... > https://lists.sourceforge.net/lists/listinfo/jython-checkins > |
From: <pj...@us...> - 2009-08-16 23:39:21
|
Revision: 6684 http://jython.svn.sourceforge.net/jython/?rev=6684&view=rev Author: pjenvey Date: 2009-08-16 23:39:05 +0000 (Sun, 16 Aug 2009) Log Message: ----------- fix javadoc, small cleanup 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-16 22:40:47 UTC (rev 6683) +++ trunk/jython/src/org/python/core/PyFile.java 2009-08-16 23:39:05 UTC (rev 6684) @@ -1,4 +1,7 @@ -// Copyright (c) Corporation for National Research Initiatives +/* + * Copyright (c) Corporation for National Research Initiatives + * Copyright (c) Jython Developers + */ package org.python.core; import java.io.InputStream; @@ -27,7 +30,7 @@ import org.python.expose.ExposedType; /** - * A python file wrapper around a java stream, reader/writer or file. + * The Python file type. Wraps an {@link TextIOBase} object. */ @ExposedType(name = "file") public class PyFile extends PyObject { @@ -103,8 +106,8 @@ * method <code>file</code> doesn't expose this functionality (<code>open</code> does * albeit deprecated) as it isn't available to regular Python code. To wrap an * InputStream in a file from Python, use - * {@link util.FileUtil#wrap(InputStream, int)} - * {@link util.FileUtil#wrap(InputStream)} + * {@link org.python.core.util.FileUtil#wrap(InputStream, int)} + * {@link org.python.core.util.FileUtil#wrap(InputStream)} */ public PyFile(InputStream istream, int bufsize) { this(istream, "<Java InputStream '" + istream + "' as file>", "r", bufsize, true); @@ -124,8 +127,8 @@ * method <code>file</code> doesn't expose this functionality (<code>open</code> does * albeit deprecated) as it isn't available to regular Python code. To wrap an * OutputStream in a file from Python, use - * {@link util.FileUtil#wrap(OutputStream, int)} - * {@link util.FileUtil#wrap(OutputStream)} + * {@link org.python.core.util.FileUtil#wrap(OutputStream, int)} + * {@link org.python.core.util.FileUtil#wrap(OutputStream)} */ public PyFile(OutputStream ostream, int bufsize) { this(ostream, "<Java OutputStream '" + ostream + "' as file>", "w", bufsize, true); @@ -240,13 +243,13 @@ } @ExposedMethod(defaults = {"-1"}, doc = BuiltinDocs.file_read_doc) - final synchronized PyString file_read(int n) { + final synchronized PyString file_read(int size) { checkClosed(); - return new PyString(file.read(n)); + return new PyString(file.read(size)); } - public PyString read(int n) { - return file_read(n); + public PyString read(int size) { + return file_read(size); } public PyString read() { @@ -303,6 +306,7 @@ return file_readlines(0); } + @Override public PyObject __iternext__() { return file___iternext__(); } @@ -329,7 +333,8 @@ return file_next(); } - @ExposedMethod(names = {"__enter__", "__iter__", "xreadlines"}, doc = BuiltinDocs.file___iter___doc) + @ExposedMethod(names = {"__enter__", "__iter__", "xreadlines"}, + doc = BuiltinDocs.file___iter___doc) final PyObject file_self() { checkClosed(); return this; @@ -339,6 +344,7 @@ return file_self(); } + @Override public PyObject __iter__() { return file_self(); } @@ -348,35 +354,35 @@ } @ExposedMethod(doc = BuiltinDocs.file_write_doc) - final void file_write(PyObject o) { - if (o instanceof PyUnicode) { + final void file_write(PyObject obj) { + if (obj instanceof PyUnicode) { // Call __str__ on unicode objects to encode them before writing - file_write(o.__str__().string); - } else if (o instanceof PyString) { - file_write(((PyString)o).string); - } else if (binary && o instanceof PyArray) { - file_write(((PyArray)o).tostring()); + 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 ", - o.getType().fastGetName())); + obj.getType().fastGetName())); } } - final synchronized void file_write(String s) { + final synchronized void file_write(String string) { checkClosed(); softspace = false; - file.write(s); + file.write(string); } - public void write(String s) { - file_write(s); + public void write(String string) { + file_write(string); } @ExposedMethod(doc = BuiltinDocs.file_writelines_doc) - final synchronized void file_writelines(PyObject a) { + final synchronized void file_writelines(PyObject lines) { checkClosed(); - PyObject iter = Py.iter(a, "writelines() requires an iterable argument"); + PyObject iter = Py.iter(lines, "writelines() requires an iterable argument"); PyObject item = null; while ((item = iter.__iternext__()) != null) { @@ -387,8 +393,8 @@ } } - public void writelines(PyObject a) { - file_writelines(a); + public void writelines(PyObject lines) { + file_writelines(lines); } @ExposedMethod(doc = BuiltinDocs.file_tell_doc) @@ -506,6 +512,7 @@ return String.format("<%s file '%s', mode '%s' at %s>", state, name, mode, id); } + @Override public String toString() { return file_toString(); } @@ -540,19 +547,21 @@ throw Py.TypeError("can't delete numeric/char attribute"); } + @Override public Object __tojava__(Class<?> cls) { - Object o = null; + Object obj = null; if (InputStream.class.isAssignableFrom(cls)) { - o = file.asInputStream(); + obj = file.asInputStream(); } else if (OutputStream.class.isAssignableFrom(cls)) { - o = file.asOutputStream(); + obj = file.asOutputStream(); } - if (o == null) { - o = super.__tojava__(cls); + if (obj == null) { + obj = super.__tojava__(cls); } - return o; + return obj; } + @Override protected void finalize() throws Throwable { super.finalize(); if (closer != null) { @@ -569,15 +578,15 @@ } /** - * A mechanism to make sure PyFiles are closed on exit. On creation Closer adds itself to a list - * of Closers that will be run by PyFileCloser on JVM shutdown. When a PyFile's close or - * finalize methods are called, PyFile calls its Closer.close which clears Closer out of the - * shutdown queue. + * A mechanism to make sure PyFiles are closed on exit. On creation Closer adds itself + * to a list of Closers that will be run by PyFileCloser on JVM shutdown. When a + * PyFile's close or finalize methods are called, PyFile calls its Closer.close which + * clears Closer out of the shutdown queue. * - * We use a regular object here rather than WeakReferences and their ilk as they may be - * collected before the shutdown hook runs. There's no guarantee that finalize will be called - * during shutdown, so we can't use it. It's vital that this Closer has no reference to the - * PyFile it's closing so the PyFile remains garbage collectable. + * We use a regular object here rather than WeakReferences and their ilk as they may + * be collected before the shutdown hook runs. There's no guarantee that finalize will + * be called during shutdown, so we can't use it. It's vital that this Closer has no + * reference to the PyFile it's closing so the PyFile remains garbage collectable. */ private static class Closer { @@ -612,6 +621,7 @@ super("Jython Shutdown File Closer"); } + @Override public void run() { if (closers == null) { // closers can be null in some strange cases This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
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. |
From: <pj...@us...> - 2009-08-29 18:30:37
|
Revision: 6725 http://jython.svn.sourceforge.net/jython/?rev=6725&view=rev Author: pjenvey Date: 2009-08-29 18:30:16 +0000 (Sat, 29 Aug 2009) Log Message: ----------- treat line buffered read streams as the default buffered mode. CPython 2 line buffered read uses a different bufsize than the default depending on the platform (sometimes smaller, sometimes bigger) 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-29 17:54:06 UTC (rev 6724) +++ trunk/jython/src/org/python/core/PyFile.java 2009-08-29 18:30:16 UTC (rev 6725) @@ -196,7 +196,7 @@ buffer = lineBuffered ? new LineBufferedWriter(raw) : new BufferedWriter(raw, bufsize); } else if (reading) { // Line buffering is for output only - buffer = new BufferedReader(raw, lineBuffered ? 0 : bufsize); + buffer = new BufferedReader(raw, lineBuffered ? IOBase.DEFAULT_BUFFER_SIZE : bufsize); } else { // Should never happen throw Py.ValueError("unknown mode: '" + mode + "'"); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <pj...@us...> - 2011-02-12 01:50:44
|
Revision: 7197 http://jython.svn.sourceforge.net/jython/?rev=7197&view=rev Author: pjenvey Date: 2011-02-12 01:50:38 +0000 (Sat, 12 Feb 2011) Log Message: ----------- ensure files are closed on close() even after PySystemState.cleanup has been called. this is an evil situation to be calling python code in, but it doesn't hurt us to close the file anyway and this partly fixes #1701 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 2011-02-11 14:11:26 UTC (rev 7196) +++ trunk/jython/src/org/python/core/PyFile.java 2011-02-12 01:50:38 UTC (rev 7197) @@ -590,9 +590,8 @@ /** For closing directly */ public void close() { - if (sys.unregisterCloser(this)) { - file.close(); - } + sys.unregisterCloser(this); + file.close(); sys = null; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |