From: <pj...@us...> - 2008-11-23 01:57:14
|
Revision: 5606 http://jython.svn.sourceforge.net/jython/?rev=5606&view=rev Author: pjenvey Date: 2008-11-23 01:57:09 +0000 (Sun, 23 Nov 2008) Log Message: ----------- o allow open({In,Out}putStream) but with a deprecation warning. make open a function o add optional bufsize args to FileUtil.wrap fixes #1171 Modified Paths: -------------- trunk/jython/src/org/python/core/PyFile.java trunk/jython/src/org/python/core/__builtin__.java trunk/jython/src/org/python/core/util/FileUtil.java Modified: trunk/jython/src/org/python/core/PyFile.java =================================================================== --- trunk/jython/src/org/python/core/PyFile.java 2008-11-23 00:46:06 UTC (rev 5605) +++ trunk/jython/src/org/python/core/PyFile.java 2008-11-23 01:57:09 UTC (rev 5606) @@ -96,13 +96,19 @@ } /** - * Creates a file object wrapping the given <code>InputStream</code>. The builtin methods - * <code>file</code> and <code>open</code> don't expose this functionality as it isn't available - * to regular Python code. To wrap an InputStream in a file from Python, use + * Creates a file object wrapping the given <code>InputStream</code>. The builtin + * 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 FileUtil#wrap(InputStream, int)} * {@link FileUtil#wrap(InputStream)} */ + public PyFile(InputStream istream, int bufsize) { + this(istream, "<Java InputStream '" + istream + "' as file>", "r", bufsize, true); + } + public PyFile(InputStream istream) { - this(istream, "<Java InputStream '" + istream + "' as file>", "r", -1, true); + this(istream, -1); } PyFile(OutputStream ostream, String name, String mode, int bufsize, boolean closefd) { @@ -111,13 +117,19 @@ } /** - * Creates a file object wrapping the given <code>OutputStream</code>. The builtin methods - * <code>file</code> and <code>open</code> don't expose this functionality as it isn't available - * to regular Python code. To wrap an OutputStream in a file from Python, use + * Creates a file object wrapping the given <code>OutputStream</code>. The builtin + * 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 FileUtil#wrap(OutputStream, int)} * {@link FileUtil#wrap(OutputStream)} */ + public PyFile(OutputStream ostream, int bufsize) { + this(ostream, "<Java OutputStream '" + ostream + "' as file>", "w", bufsize, true); + } + public PyFile(OutputStream ostream) { - this(ostream, "<Java OutputStream '" + ostream + "' as file>", "w", -1, true); + this(ostream, -1); } public PyFile(String name, String mode, int bufsize) { Modified: trunk/jython/src/org/python/core/__builtin__.java =================================================================== --- trunk/jython/src/org/python/core/__builtin__.java 2008-11-23 00:46:06 UTC (rev 5605) +++ trunk/jython/src/org/python/core/__builtin__.java 2008-11-23 01:57:09 UTC (rev 5606) @@ -1,6 +1,8 @@ // Copyright (c) Corporation for National Research Initiatives package org.python.core; +import java.io.InputStream; +import java.io.OutputStream; import java.util.Iterator; import java.util.Map; @@ -305,7 +307,6 @@ dict.__setitem__("unicode", PyUnicode.TYPE); dict.__setitem__("basestring", PyBaseString.TYPE); dict.__setitem__("file", PyFile.TYPE); - dict.__setitem__("open", PyFile.TYPE); dict.__setitem__("slice", PySlice.TYPE); dict.__setitem__("xrange", PyXRange.TYPE); @@ -364,6 +365,7 @@ dict.__setitem__("vars", new BuiltinFunctions("vars", 41, 0, 1)); dict.__setitem__("zip", new BuiltinFunctions("zip", 43, 0, -1)); dict.__setitem__("compile", new CompileFunction()); + dict.__setitem__("open", new OpenFunction()); dict.__setitem__("reversed", new BuiltinFunctions("reversed", 45, 1)); dict.__setitem__("__import__", new ImportFunction()); dict.__setitem__("sorted", new SortedFunction()); @@ -1567,3 +1569,31 @@ return (modType)node; } } + +class OpenFunction extends PyBuiltinFunction { + OpenFunction() { + super("open", "Open a file using the file() type, returns a file object. This is the\n" + + "preferred way to open a file."); + } + + private static final String warning = + "Passing an Input/OutputStream to open is deprecated, use " + + "org.python.core.util.FileUtil.wrap(stream[, bufsize]) instead."; + + public PyObject __call__(PyObject args[], String kwds[]) { + ArgParser ap = new ArgParser("file", args, kwds, new String[] {"name", "mode", "bufsize"}, + 1); + PyObject obj = ap.getPyObject(0); + if (obj instanceof PyJavaInstance) { + int bufsize = ap.getInt(2, -1); + if (obj.javaProxy instanceof InputStream) { + Py.warning(Py.DeprecationWarning, warning); + return new PyFile((InputStream)obj.javaProxy, bufsize); + } else if (obj.javaProxy instanceof OutputStream) { + Py.warning(Py.DeprecationWarning, warning); + return new PyFile((OutputStream)obj.javaProxy, bufsize); + } + } + return PyFile.TYPE.__call__(args, kwds); + } +} Modified: trunk/jython/src/org/python/core/util/FileUtil.java =================================================================== --- trunk/jython/src/org/python/core/util/FileUtil.java 2008-11-23 00:46:06 UTC (rev 5605) +++ trunk/jython/src/org/python/core/util/FileUtil.java 2008-11-23 01:57:09 UTC (rev 5606) @@ -14,17 +14,31 @@ public class FileUtil { /** + * Creates a PyFile that reads from the given <code>InputStream</code> with bufsize. + */ + public static PyFile wrap(InputStream is, int bufsize) { + return new PyFile(is, bufsize); + } + + /** * Creates a PyFile that reads from the given <code>InputStream</code>. */ public static PyFile wrap(InputStream is) { - return new PyFile(is); + return wrap(is, -1); } /** + * Creates a PyFile that writes to the given <code>OutputStream</code> with bufsize. + */ + public static PyFile wrap(OutputStream os, int bufsize) { + return new PyFile(os, bufsize); + } + + /** * Creates a PyFile that writes to the given <code>OutputStream</code>. */ public static PyFile wrap(OutputStream os) { - return new PyFile(os); + return wrap(os, -1); } /** This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |