[Jython-checkins] jython: Reverted some name changes, better print_function support.

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


http://hg.python.org/jython/rev/1219ca492d71
changeset:   6214:1219ca492d71
user:        Frank Wierzbicki <fwierzbicki at gmail.com>
date:        Fri May 13 22:28:07 2011 -0700
summary:
  Reverted some name changes, better print_function support.

files:
  src/org/python/core/StdoutWrapper.java |  137 +++++++-----
  src/org/python/core/__builtin__.java   |   10 +-
  2 files changed, 88 insertions(+), 59 deletions(-)


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
@@ -99,11 +99,60 @@
         }
     }
 
-    public void print(PyObject obj, boolean space, boolean newline) {
-        print(new PyObject[] {obj}, space, newline, null);
+    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;
     }
 
-    public void print(PyObject[] objs, boolean space, boolean newline, PyObject sep) {
+    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 printToObject(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;
+            printToFileWriter(file, end);
+        } else {
+        }
+    }
+
+    public void print(PyObject o, boolean space, boolean newline) {
         PyObject out = myFile();
 
         if (out instanceof PyFile) {
@@ -113,24 +162,16 @@
                 file.softspace = false;
             }
 
-            for (PyObject obj: objs) {
-                String s;
-                if (obj instanceof PyUnicode && file.encoding != null) {
-                    s = ((PyUnicode)obj).encode(file.encoding, "strict");
-                } else {
-                    s = obj.__str__().toString();
-                }
-                file.write(s);
+            String s = printToFile(file, o);
 
-                if (obj instanceof PyString) {
-                    int len = s.length();
-                    if (len == 0 || !Character.isWhitespace(s.charAt(len - 1))
-                        || s.charAt(len - 1) == ' ') {
-                        file.softspace = space;
-                    }
-                } else {
+            if (o instanceof PyString) {
+                int len = s.length();
+                if (len == 0 || !Character.isWhitespace(s.charAt(len - 1))
+                    || s.charAt(len - 1) == ' ') {
                     file.softspace = space;
                 }
+            } else {
+                file.softspace = space;
             }
 
             if (newline) {
@@ -144,27 +185,16 @@
                 file.write(" ");
                 file.softspace = false;
             }
+            String s = printToFileWriter(file, o);
 
-            for (PyObject obj: objs) {
-                // since we are outputting directly to a character stream,
-                // avoid doing an encoding
-                String s;
-                if (obj instanceof PyString) {
-                    s = ((PyString) obj).getString();
-                } else {
-                    s = obj.toString();
-                }
-                file.write(s);
-
-                if (obj instanceof PyString) {
-                    int len = s.length();
-                    if (len == 0 || !Character.isWhitespace(s.charAt(len - 1))
-                        || s.charAt(len - 1) == ' ') {
-                        file.softspace = space;
-                    }
-                } else {
+            if (o instanceof PyString) {
+                int len = s.length();
+                if (len == 0 || !Character.isWhitespace(s.charAt(len - 1))
+                    || s.charAt(len - 1) == ' ') {
                     file.softspace = space;
                 }
+            } else {
+                file.softspace = space;
             }
 
             if (newline) {
@@ -179,24 +209,19 @@
                 out.__setattr__("softspace", Py.Zero);
             }
 
-            for (PyObject obj: objs) {
-                if (!(obj instanceof PyUnicode)) {
-                    obj = obj.__str__();
-                }
-                out.invoke("write", obj);
-
-                if (obj instanceof PyString) {
-                    String s = obj.toString();
-                    int len = s.length();
-                    if (len == 0 || !Character.isWhitespace(s.charAt(len - 1))
-                        || s.charAt(len - 1) == ' ') {
-                        out.__setattr__("softspace", space ? Py.One : Py.Zero);
-                    }
-                } else {
+            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) == ' ') {
                     out.__setattr__("softspace", space ? Py.One : Py.Zero);
                 }
+            } else {
+                out.__setattr__("softspace", space ? Py.One : Py.Zero);
             }
 
+            printToObject(out, o);
+
             if (newline) {
                 out.invoke("write", Py.Newline);
                 out.__setattr__("softspace", Py.Zero);
@@ -212,16 +237,16 @@
         print(new PyString(s), false, true);
     }
 
-    public void print(PyObject obj) {
-        print(obj, false, false);
+    public void print(PyObject o) {
+        print(o, false, false);
     }
 
-    public void printComma(PyObject obj) {
-        print(obj, true, false);
+    public void printComma(PyObject o) {
+        print(o, true, false);
     }
 
-    public void println(PyObject obj) {
-        print(obj, false, true);
+    public void println(PyObject o) {
+        print(o, false, true);
     }
 
     public void println() {
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
@@ -1342,6 +1342,9 @@
         //XXX: integrate into ArgParser - need key checks etc.
         //     ArgParser will need to be extended to take keyword-only args.
         Map<String, PyObject> keyargs = new HashMap<String, PyObject>();
+        //XXX: preloading defaults for now.
+        keyargs.put("sep", Py.newString(" "));
+        keyargs.put("end", Py.newString("\n"));
         int kwlen = kwds.length;
         for (int i=kwlen; i>0; i--) {
             keyargs.put(kwds[kwlen - i], args[args.length - i]);
@@ -1352,16 +1355,17 @@
     }
 
     private static PyObject print(PyObject values[], PyObject sep, PyObject end, PyObject file) {
-        StdoutWrapper out = Py.stdout;
+        StdoutWrapper out;
         if (file != null && file != Py.None) {
             out = new FixedFileWrapper(file);
+        } else {
+            out = Py.stdout;
         }
 
         if (values.length == 0) {
             out.println();
         } else {
-            boolean newline = end == null || end.__nonzero__();
-            out.print(values, false, newline, sep);
+            out.print(values, sep, end);
         }
         return Py.None;
     }

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


More information about the Jython-checkins mailing list