[Jython-checkins] jython: Get test_print passing!

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


http://hg.python.org/jython/rev/59d70d13e445
changeset:   6215:59d70d13e445
user:        Frank Wierzbicki <fwierzbicki at gmail.com>
date:        Mon May 16 15:43:16 2011 -0700
summary:
  Get test_print passing!

files:
  src/org/python/core/Py.java            |   4 +
  src/org/python/core/PySystemState.java |   2 +
  src/org/python/core/StdoutWrapper.java |  27 ++++++-
  src/org/python/core/__builtin__.java   |  51 +++++++++----
  4 files changed, 66 insertions(+), 18 deletions(-)


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 */
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
@@ -123,7 +123,7 @@
         return s;
     }
 
-    private void printToObject(PyObject file, PyObject o) {
+    private void printToFileObject(PyObject file, PyObject o) {
         if (!(o instanceof PyUnicode)) {
             o = o.__str__();
         }
@@ -147,8 +147,21 @@
             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);
         }
     }
 
@@ -220,7 +233,7 @@
                 out.__setattr__("softspace", space ? Py.One : Py.Zero);
             }
 
-            printToObject(out, o);
+            printToFileObject(out, o);
 
             if (newline) {
                 out.invoke("write", Py.Newline);
@@ -250,6 +263,10 @@
     }
 
     public void println() {
+        println(false);
+    }
+
+    public void println(boolean useUnicode) {
         PyObject out = myFile();
 
         if (out instanceof PyFile) {
@@ -258,7 +275,11 @@
             file.flush();
             file.softspace = false;
         } else {
-            out.invoke("write", Py.Newline);
+            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
@@ -1339,33 +1339,54 @@
 
     @Override
     public PyObject __call__(PyObject args[], String kwds[]) {
-        //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]);
+        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;
+            }
         }
-        PyObject values[] = new PyObject[args.length - kwlen];
-        System.arraycopy(args, 0, values, 0, args.length - kwlen);
-        return print(values, keyargs.get("sep"), keyargs.get("end"), keyargs.get("file"));
+        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[], PyObject sep, PyObject end, PyObject file) {
+    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();
+            out.println(useUnicode);
         } else {
-            out.print(values, sep, end);
+            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;
     }

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


More information about the Jython-checkins mailing list