[Jython-checkins] jython: Enable setting the mode and buffersize when a PyFile wraps InputStream

jim.baker jython-checkins at python.org
Sun Jun 22 08:24:17 CEST 2014


http://hg.python.org/jython/rev/66800903dded
changeset:   7310:66800903dded
user:        Indra Talip <indra.talip at gmail.com>
date:        Sun Jun 22 00:24:07 2014 -0600
summary:
  Enable setting the mode and buffersize when a PyFile wraps InputStream
and OutputStream objects.

Merged from https://bitbucket.org/jython/jython/pull-request/7/additional-fileutilwrap-methods-that-allow/

files:
  Lib/test/test_java_integration.py      |  34 ++++++++++
  src/org/python/core/PyFile.java        |  23 +++++-
  src/org/python/core/util/FileUtil.java |  47 ++++++++++---
  3 files changed, 89 insertions(+), 15 deletions(-)


diff --git a/Lib/test/test_java_integration.py b/Lib/test/test_java_integration.py
--- a/Lib/test/test_java_integration.py
+++ b/Lib/test/test_java_integration.py
@@ -143,6 +143,40 @@
     def test_fileio_error(self):
         self.assertRaises(FileNotFoundException, FileInputStream, "garbage")
 
+    def fileutil_is_helper(self, mode, expected):
+        old_linesep = System.setProperty("line.separator", "\r\n")
+        try:
+            inputstream = ByteArrayInputStream(bytearray('1\r\n2\r\n3\r\n'))
+            inputfile = FileUtil.wrap(inputstream, mode)
+            actual = inputfile.readlines()
+            inputfile.close()
+            self.assertEquals(expected, actual)
+        finally:
+            System.setProperty("line.separator", old_linesep)
+
+    def test_fileutil_wrap_inputstream(self):
+        self.fileutil_is_helper('r', ['1\n', '2\n', '3\n'])
+
+    def test_fileutil_wrap_inputstream_binary(self):
+        self.fileutil_is_helper('rb', ['1\r\n', '2\r\n', '3\r\n'])
+
+    def fileutil_os_helper(self, mode, expected):
+        old_linesep = System.setProperty("line.separator", "\r\n")
+        try:
+            outputstream = ByteArrayOutputStream()
+            outputfile = FileUtil.wrap(outputstream, mode)
+            outputfile.writelines(["1\n", "2\n", "3\n"])
+            outputfile.close()
+            self.assertEquals(bytearray(outputstream.toByteArray()), expected)
+        finally:
+            System.setProperty("line.separator", old_linesep)
+
+    def test_fileutil_wrap_outputstream_default_textmode(self):
+        self.fileutil_os_helper("w", bytearray("1\r\n2\r\n3\r\n"))
+
+    def test_fileutil_wrap_outputstream_binary(self):
+        self.fileutil_os_helper("wb", bytearray("1\n2\n3\n"))
+
     def test_unsupported_tell(self):
         fp = FileUtil.wrap(System.out)
         self.assertRaises(IOError, fp.tell)
diff --git a/src/org/python/core/PyFile.java b/src/org/python/core/PyFile.java
--- a/src/org/python/core/PyFile.java
+++ b/src/org/python/core/PyFile.java
@@ -102,18 +102,28 @@
      * 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 org.python.core.util.FileUtil#wrap(InputStream, String, int)}
+     * {@link org.python.core.util.FileUtil#wrap(InputStream, String)}
      * {@link org.python.core.util.FileUtil#wrap(InputStream, int)}
      * {@link org.python.core.util.FileUtil#wrap(InputStream)}
      */
+    public PyFile(InputStream istream, String mode, int bufsize) {
+        this(istream, "<Java InputStream '" + istream + "' as file>", mode, bufsize, true);
+    }
+
+    public PyFile(InputStream istream, String mode) {
+        this(istream, mode, -1);
+    }
+
     public PyFile(InputStream istream, int bufsize) {
-        this(istream, "<Java InputStream '" + istream + "' as file>", "r", bufsize, true);
+        this(istream, "r", bufsize);
     }
 
     public PyFile(InputStream istream) {
         this(istream, -1);
     }
 
-    PyFile(OutputStream ostream, String name, String mode, int bufsize, boolean closefd) {
+    public PyFile(OutputStream ostream, String name, String mode, int bufsize, boolean closefd) {
         parseMode(mode);
         file___init__(new StreamIO(ostream, closefd), name, mode, bufsize);
     }
@@ -123,11 +133,18 @@
      * 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 org.python.core.util.FileUtil#wrap(OutputStream, String, int)}
+     * {@link org.python.core.util.FileUtil#wrap(OutputStream, String)}
      * {@link org.python.core.util.FileUtil#wrap(OutputStream, int)}
      * {@link org.python.core.util.FileUtil#wrap(OutputStream)}
      */
+
+    public PyFile(OutputStream ostream, String mode, int bufsize) {
+         this(ostream, "<Java OutputStream '" + ostream + "' as file>", mode, bufsize, true);
+    }
+
     public PyFile(OutputStream ostream, int bufsize) {
-        this(ostream, "<Java OutputStream '" + ostream + "' as file>", "w", bufsize, true);
+        this(ostream, "w", bufsize);
     }
 
     public PyFile(OutputStream ostream) {
diff --git a/src/org/python/core/util/FileUtil.java b/src/org/python/core/util/FileUtil.java
--- a/src/org/python/core/util/FileUtil.java
+++ b/src/org/python/core/util/FileUtil.java
@@ -12,37 +12,60 @@
  * Utility methods for Java file handling.
  */
 public class FileUtil {
-
     /**
-     * Creates a PyFile that reads from the given <code>InputStream</code> with mode.
+     * Creates a PyFile with mode that reads from the given <code>InputStream</code> using bufsize.
      */
-    public static PyFile wrap(InputStream is, String mode) {
-        return new PyFile(is, "<Java InputStream '" + is + "' as file>", mode, -1, true);    
+    public static PyFile wrap(InputStream is, String mode, int bufsize) {
+        return new PyFile(is, "<Java InputStream '" + is + "' as file>", mode, bufsize, true);
     }
 
     /**
-     * Creates a PyFile that reads from the given <code>InputStream</code> with bufsize.
+     * Creates a PyFile with mode that reads from the <code>InputStream</code>.
      */
-    public static PyFile wrap(InputStream is, int bufsize) {
-        return new PyFile(is, bufsize);
+    public static PyFile wrap(InputStream is, String mode) {
+        return wrap(is, mode, -1);
     }
 
     /**
-     * Creates a PyFile that reads from the given <code>InputStream</code>.
+     * Creates a PyFile in text mode that reads from the given <code>InputStream</code>
+     * using bufsize.
+     */
+    public static PyFile wrap(InputStream is, int bufsize) {
+        return wrap(is, "r", bufsize);
+    }
+
+    /**
+     * Creates a PyFile in text mode that reads from the given <code>InputStream</code>.
      */
     public static PyFile wrap(InputStream is) {
         return wrap(is, -1);
     }
 
     /**
-     * Creates a PyFile that writes to the given <code>OutputStream</code> with bufsize.
+     * Creates a PyFile with mode that writes to the given <code>OutputStream</code> with the
+     * given bufsize.
      */
-    public static PyFile wrap(OutputStream os, int bufsize) {
-        return new PyFile(os, bufsize);
+    public static PyFile wrap(OutputStream os, String mode, int bufsize) {
+        return new PyFile(os, mode, bufsize);
     }
 
     /**
-     * Creates a PyFile that writes to the given <code>OutputStream</code>.
+     * Creates a PyFile with mode that writes to the given <code>OutputStream</code>
+     */
+    public static PyFile wrap(OutputStream os, String mode) {
+        return wrap(os, mode, -1);
+    }
+
+    /**
+     * Creates a PyFile in text mode that writes to the given <code>OutputStream</code>
+     * with bufsize.
+     */
+    public static PyFile wrap(OutputStream os, int bufsize) {
+        return wrap(os, "w", bufsize);
+    }
+
+    /**
+     * Creates a PyFile in text mode that writes to the given <code>OutputStream</code>.
      */
     public static PyFile wrap(OutputStream os) {
         return wrap(os, -1);

-- 
Repository URL: http://hg.python.org/jython


More information about the Jython-checkins mailing list