[Jython-checkins] jython (merge default -> default): Merge + unbreak test_softspace.

frank.wierzbicki jython-checkins at python.org
Wed May 18 02:53:30 CEST 2011


http://hg.python.org/jython/rev/dace7d6d2a57
changeset:   6216:dace7d6d2a57
parent:      6210:1aed2fedd01b
parent:      6215:59d70d13e445
user:        Frank Wierzbicki <fwierzbicki at gmail.com>
date:        Mon May 16 21:01:45 2011 -0700
summary:
  Merge + unbreak test_softspace.

files:
  src/org/python/compiler/Future.java    |    2 +
  src/org/python/core/CodeFlag.java      |    7 +-
  src/org/python/core/FutureFeature.java |    4 +
  src/org/python/core/Py.java            |    6 +-
  src/org/python/core/PySystemState.java |    2 +
  src/org/python/core/StdoutWrapper.java |  176 ++++++++----
  src/org/python/core/__builtin__.java   |   69 +++++
  7 files changed, 204 insertions(+), 62 deletions(-)


diff --git a/src/org/python/compiler/Future.java b/src/org/python/compiler/Future.java
--- a/src/org/python/compiler/Future.java
+++ b/src/org/python/compiler/Future.java
@@ -60,6 +60,8 @@
                 FutureFeature.with_statement.addTo(features);
             if (cflags.isFlagSet(CodeFlag.CO_FUTURE_ABSOLUTE_IMPORT))
                 FutureFeature.absolute_import.addTo(features);
+            if (cflags.isFlagSet(CodeFlag.CO_FUTURE_PRINT_FUNCTION))
+                FutureFeature.print_function.addTo(features);
         }
         int beg = 0;
         List<stmt> suite = null;
diff --git a/src/org/python/core/CodeFlag.java b/src/org/python/core/CodeFlag.java
--- a/src/org/python/core/CodeFlag.java
+++ b/src/org/python/core/CodeFlag.java
@@ -50,7 +50,12 @@
     /**
      * With statement.
      */
-    CO_FUTURE_WITH_STATEMENT(0x8000);
+    CO_FUTURE_WITH_STATEMENT(0x8000),
+    /**
+     * print function.
+     */
+    CO_FUTURE_PRINT_FUNCTION(0x10000);
+
 
     public final int flag;
     private static Iterable<CodeFlag> allFlags = Collections.unmodifiableList(Arrays.asList(values()));
diff --git a/src/org/python/core/FutureFeature.java b/src/org/python/core/FutureFeature.java
--- a/src/org/python/core/FutureFeature.java
+++ b/src/org/python/core/FutureFeature.java
@@ -24,6 +24,10 @@
      */
     with_statement(CodeFlag.CO_FUTURE_WITH_STATEMENT),
     /**
+     * Enables the print function.
+     */
+    print_function(CodeFlag.CO_FUTURE_PRINT_FUNCTION),
+    /**
      * Use braces for block delimiters instead of indentation.
      */
     braces {
diff --git a/src/org/python/core/Py.java b/src/org/python/core/Py.java
--- a/src/org/python/core/Py.java
+++ b/src/org/python/core/Py.java
@@ -80,8 +80,12 @@
     public static PyString EmptyString;
     /** A Python string containing '\n' **/
     public static PyString Newline;
+    /** A Python unicode string containing '\n' **/
+    public static PyUnicode UnicodeNewline;
     /** A Python string containing ' ' **/
     public static PyString Space;
+    /** A Python unicode string containing ' ' **/
+    public static PyString UnicodeSpace;
     /** Set if the type object is dynamically allocated */
     public static long TPFLAGS_HEAPTYPE = 1L << 9;
     /** Set if the type allows subclassing */
@@ -2092,7 +2096,7 @@
     }
 }
 
- class FixedFileWrapper extends StdoutWrapper {
+class FixedFileWrapper extends StdoutWrapper {
 
     private PyObject file;
 
diff --git a/src/org/python/core/PySystemState.java b/src/org/python/core/PySystemState.java
--- a/src/org/python/core/PySystemState.java
+++ b/src/org/python/core/PySystemState.java
@@ -933,7 +933,9 @@
 
         Py.EmptyString = new PyString("");
         Py.Newline = new PyString("\n");
+        Py.UnicodeNewline = new PyUnicode("\n");
         Py.Space = new PyString(" ");
+        Py.UnicodeSpace = new PyUnicode(" ");
 
         // Setup standard wrappers for stdout and stderr...
         Py.stderr = new StderrWrapper();
diff --git a/src/org/python/core/StdoutWrapper.java b/src/org/python/core/StdoutWrapper.java
--- a/src/org/python/core/StdoutWrapper.java
+++ b/src/org/python/core/StdoutWrapper.java
@@ -16,20 +16,20 @@
         return ss.stdout;
     }
 
-    protected void setObject(PySystemState ss, PyObject obj) {
-        ss.stdout = obj;
+    protected void setObject(PySystemState ss, PyObject out) {
+        ss.stdout = out;
     }
 
     protected PyObject myFile() {
         PySystemState ss = Py.getSystemState();
-        PyObject obj = getObject(ss);
-        if (obj == null) {
+        PyObject out = getObject(ss);
+        if (out == null) {
             throw Py.AttributeError("missing sys." + this.name);
         }
-        if (obj.getJavaProxy() != null) {
+        if (out.getJavaProxy() != null) {
             PyFile f = null;
 
-            Object tojava = obj.__tojava__(OutputStream.class);
+            Object tojava = out.__tojava__(OutputStream.class);
             if (tojava != null && tojava != Py.NoConversion) {
                 f = new PyFile((OutputStream)tojava);
             }
@@ -38,17 +38,17 @@
                 return f;
             }
         }
-        return obj;
+        return out;
     }
 
     @Override
     public void flush() {
-        PyObject obj = myFile();
-        if (obj instanceof PyFile) {
-            ((PyFile) obj).flush();
+        PyObject out = myFile();
+        if (out instanceof PyFile) {
+            ((PyFile) out).flush();
         } else {
             try {
-                obj.invoke("flush");
+                out.invoke("flush");
             } catch (PyException pye) {
                 // ok
             }
@@ -56,12 +56,12 @@
     }
 
     public void write(String s) {
-        PyObject obj = myFile();
+        PyObject out = myFile();
 
-        if (obj instanceof PyFile) {
-            ((PyFile) obj).write(s);
+        if (out instanceof PyFile) {
+            ((PyFile) out).write(s);
         } else {
-            obj.invoke("write", new PyString(s));
+            out.invoke("write", new PyString(s));
         }
     }
 
@@ -76,46 +76,106 @@
     }
 
     public void flushLine() {
-        PyObject obj = myFile();
+        PyObject out = myFile();
 
-        if (obj instanceof PyFile) {
-            PyFile file = (PyFile) obj;
+        if (out instanceof PyFile) {
+            PyFile file = (PyFile) out;
             if (file.softspace) {
                 file.write("\n");
                 file.flush();
             }
             file.softspace = false;
         } else {
-            PyObject ss = obj.__findattr__("softspace");
+            PyObject ss = out.__findattr__("softspace");
             if (ss != null && ss.__nonzero__()) {
-                obj.invoke("write", Py.Newline);
+                out.invoke("write", Py.Newline);
             }
             try {
-                obj.invoke("flush");
+                out.invoke("flush");
             } catch (PyException pye) {
                 // ok
             }
-            obj.__setattr__("softspace", Py.Zero);
+            out.__setattr__("softspace", Py.Zero);
+        }
+    }
+
+    private String printToFile(PyFile file, PyObject o) {
+        String s;
+        if (o instanceof PyUnicode && file.encoding != null) {
+            s = ((PyUnicode)o).encode(file.encoding, "strict");
+        } else {
+            s = o.__str__().toString();
+        }
+        file.write(s);
+        return s;
+    }
+
+    private String printToFileWriter(PyFileWriter file, PyObject o) {
+        // since we are outputting directly to a character stream,
+        // avoid doing an encoding
+        String s;
+        if (o instanceof PyString) {
+            s = ((PyString) o).getString();
+        } else {
+            s = o.toString();
+        }
+        file.write(s);
+        return s;
+    }
+
+    private void printToFileObject(PyObject file, PyObject o) {
+        if (!(o instanceof PyUnicode)) {
+            o = o.__str__();
+        }
+        file.invoke("write", o);
+    }
+
+    /**
+     * For __future__ print_function.
+     */
+    public void print(PyObject[] args, PyObject sep, PyObject end) {
+        PyObject out = myFile();
+
+        if (out instanceof PyFile) {
+            PyFile file = (PyFile)out;
+            for (int i=0;i<args.length;i++) {
+                printToFile(file, args[i]);
+                if (i < args.length -1) {
+                    printToFile(file, sep);
+                }
+            }
+            printToFile(file, end);
+        } else if (out instanceof PyFileWriter) {
+            PyFileWriter file = (PyFileWriter)out;
+            for (int i=0;i<args.length;i++) {
+                printToFileWriter(file, args[i]);
+                if (i < args.length -1) {
+                    printToFileWriter(file, sep);
+                }
+            }
+            printToFileWriter(file, end);
+        } else {
+            for (int i=0;i<args.length;i++) {
+                printToFileObject(out, args[i]);
+                if (i < args.length -1) {
+                    printToFileObject(out, sep);
+                }
+            }
+            printToFileObject(out, end);
         }
     }
 
     public void print(PyObject o, boolean space, boolean newline) {
-        PyObject obj = myFile();
+        PyObject out = myFile();
 
-        if (obj instanceof PyFile) {
-            PyFile file = (PyFile)obj;
+        if (out instanceof PyFile) {
+            PyFile file = (PyFile)out;
             if (file.softspace) {
                 file.write(" ");
                 file.softspace = false;
             }
 
-            String s;
-            if (o instanceof PyUnicode && file.encoding != null) {
-                s = ((PyUnicode)o).encode(file.encoding, "strict");
-            } else {
-                s = o.__str__().toString();
-            }
-            file.write(s);
+            String s = printToFile(file, o);
 
             if (o instanceof PyString) {
                 int len = s.length();
@@ -132,22 +192,13 @@
                 file.softspace = false;
             }
             file.flush();
-        } else if (obj instanceof PyFileWriter) {
-            PyFileWriter file = (PyFileWriter)obj;
+        } else if (out instanceof PyFileWriter) {
+            PyFileWriter file = (PyFileWriter)out;
             if (file.softspace) {
                 file.write(" ");
                 file.softspace = false;
             }
-
-            // since we are outputting directly to a character stream,
-            // avoid doing an encoding
-            String s;
-            if (o instanceof PyString) {
-                s = ((PyString) o).getString();
-            } else {
-                s = o.toString();
-            }
-            file.write(s);
+            String s = printToFileWriter(file, o);
 
             if (o instanceof PyString) {
                 int len = s.length();
@@ -165,31 +216,28 @@
             }
             file.flush();
         } else {
-            PyObject ss = obj.__findattr__("softspace");
+            PyObject ss = out.__findattr__("softspace");
             if (ss != null && ss.__nonzero__()) {
-                obj.invoke("write", Py.Space);
-                obj.__setattr__("softspace", Py.Zero);
+                out.invoke("write", Py.Space);
+                out.__setattr__("softspace", Py.Zero);
             }
 
-            if (!(o instanceof PyUnicode)) {
-                o = o.__str__();
-            }
-            obj.invoke("write", o);
+            printToFileObject(out, o);
 
             if (o instanceof PyString) {
                 String s = o.toString();
                 int len = s.length();
                 if (len == 0 || !Character.isWhitespace(s.charAt(len - 1))
                     || s.charAt(len - 1) == ' ') {
-                    obj.__setattr__("softspace", space ? Py.One : Py.Zero);
+                    out.__setattr__("softspace", space ? Py.One : Py.Zero);
                 }
             } else {
-                obj.__setattr__("softspace", space ? Py.One : Py.Zero);
+                out.__setattr__("softspace", space ? Py.One : Py.Zero);
             }
 
             if (newline) {
-                obj.invoke("write", Py.Newline);
-                obj.__setattr__("softspace", Py.Zero);
+                out.invoke("write", Py.Newline);
+                out.__setattr__("softspace", Py.Zero);
             }
         }
     }
@@ -215,16 +263,24 @@
     }
 
     public void println() {
-        PyObject obj = myFile();
+        println(false);
+    }
 
-        if (obj instanceof PyFile) {
-            PyFile file = (PyFile) obj;
+    public void println(boolean useUnicode) {
+        PyObject out = myFile();
+
+        if (out instanceof PyFile) {
+            PyFile file = (PyFile) out;
             file.write("\n");
             file.flush();
             file.softspace = false;
         } else {
-            obj.invoke("write", Py.Newline);
-            obj.__setattr__("softspace", Py.Zero);
+            if (useUnicode) {
+                out.invoke("write", Py.UnicodeNewline);
+            } else {
+                out.invoke("write", Py.Newline);
+            }
+            out.__setattr__("softspace", Py.Zero);
         }
     }
 }
diff --git a/src/org/python/core/__builtin__.java b/src/org/python/core/__builtin__.java
--- a/src/org/python/core/__builtin__.java
+++ b/src/org/python/core/__builtin__.java
@@ -9,6 +9,7 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Map;
 
@@ -359,6 +360,7 @@
         dict.__setitem__("all", new AllFunction());
         dict.__setitem__("any", new AnyFunction());
         dict.__setitem__("format", new FormatFunction());
+        dict.__setitem__("print", new PrintFunction());
     }
 
     public static PyObject abs(PyObject o) {
@@ -1323,6 +1325,73 @@
     }
 }
 
+class PrintFunction extends PyBuiltinFunction {
+    PrintFunction() {
+
+        super("print",
+              "print(value, ..., sep=' ', end='\\n', file=sys.stdout)\n\n" +
+              "Prints the values to a stream, or to sys.stdout by default.\n" +
+              "Optional keyword arguments:\n" +
+              "file: a file-like object (stream); defaults to the current sys.stdout.\n" +
+              "sep:  string inserted between values, default a space.\n" +
+              "end:  string appended after the last value, default a newline.\n");
+    }
+
+    @Override
+    public PyObject __call__(PyObject args[], String kwds[]) {
+        int kwlen = kwds.length;
+        int argslen = args.length;
+        boolean useUnicode = false;
+        PyObject values[] = new PyObject[argslen - kwlen];
+        System.arraycopy(args, 0, values, 0, argslen - kwlen);
+        PyObject keyValues[] = new PyObject[kwlen];
+        System.arraycopy(args, argslen - kwlen, keyValues, 0, kwlen);
+        ArgParser ap = new ArgParser("print", keyValues, kwds, new String[] {"sep", "end", "file"});
+        for (PyObject keyValue: keyValues) {
+            if (keyValue instanceof PyUnicode) {
+                //If "file" is passed in as PyUnicode, that's OK as it will error later.
+                useUnicode = true;
+            }
+        }
+        String sep = ap.getString(0, null);
+        String end = ap.getString(1, null);
+        PyObject file = ap.getPyObject(2, null);
+        return print(values, sep, end, file, useUnicode);
+    }
+
+    private static PyObject print(PyObject values[], String sep, String end,
+                                  PyObject file, boolean useUnicode) {
+        StdoutWrapper out;
+        if (file != null && file != Py.None) {
+            out = new FixedFileWrapper(file);
+        } else {
+            out = Py.stdout;
+        }
+        if (values.length == 0) {
+            out.println(useUnicode);
+        } else {
+            if (sep == null) {
+                sep = " ";
+            }
+            if (end == null) {
+                end = "\n";
+            }
+            if (!useUnicode) {
+                for (PyObject value: values) {
+                    if (value instanceof PyUnicode) {
+                        useUnicode = true;
+                        break;
+                    }
+                }
+            }
+            out.print(values,
+                      useUnicode ? Py.newUnicode(sep) : Py.newString(sep),
+                      useUnicode ? Py.newUnicode(end) : Py.newString(end));
+        }
+        return Py.None;
+    }
+}
+
 class MaxFunction extends PyBuiltinFunction {
     MaxFunction() {
         super("max",

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


More information about the Jython-checkins mailing list