[Jython-checkins] jython: Add support for multiple objects passed to print.
frank.wierzbicki
jython-checkins at python.org
Wed May 18 02:53:30 CEST 2011
http://hg.python.org/jython/rev/67f89e017e31
changeset: 6212:67f89e017e31
user: Frank Wierzbicki <fwierzbicki at gmail.com>
date: Thu May 12 23:22:18 2011 -0700
summary:
Add support for multiple objects passed to print.
files:
src/org/python/core/Py.java | 2 +-
src/org/python/core/StdoutWrapper.java | 178 ++++++------
src/org/python/core/__builtin__.java | 21 +-
3 files changed, 115 insertions(+), 86 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
@@ -2092,7 +2092,7 @@
}
}
- class FixedFileWrapper extends StdoutWrapper {
+class FixedFileWrapper extends StdoutWrapper {
private PyObject file;
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,55 +76,61 @@
}
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);
}
}
- public void print(PyObject o, boolean space, boolean newline) {
- PyObject obj = myFile();
+ public void print(PyObject obj, boolean space, boolean newline) {
+ print(new PyObject[] {obj}, space, newline, null);
+ }
- if (obj instanceof PyFile) {
- PyFile file = (PyFile)obj;
+ public void print(PyObject[] objs, boolean space, boolean newline, PyObject sep) {
+ PyObject out = myFile();
+
+ 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);
+ 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);
- if (o instanceof PyString) {
- int len = s.length();
- if (len == 0 || !Character.isWhitespace(s.charAt(len - 1))
- || s.charAt(len - 1) == ' ') {
+ 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 {
file.softspace = space;
}
- } else {
- file.softspace = space;
}
if (newline) {
@@ -132,31 +138,33 @@
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);
+ 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 (o instanceof PyString) {
- int len = s.length();
- if (len == 0 || !Character.isWhitespace(s.charAt(len - 1))
- || s.charAt(len - 1) == ' ') {
+ 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 {
file.softspace = space;
}
- } else {
- file.softspace = space;
}
if (newline) {
@@ -165,31 +173,33 @@
}
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);
+ for (PyObject obj: objs) {
+ if (!(obj instanceof PyUnicode)) {
+ obj = obj.__str__();
+ }
+ out.invoke("write", obj);
- 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);
+ 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 {
+ out.__setattr__("softspace", space ? Py.One : Py.Zero);
}
- } else {
- obj.__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);
}
}
}
@@ -202,29 +212,29 @@
print(new PyString(s), false, true);
}
- public void print(PyObject o) {
- print(o, false, false);
+ public void print(PyObject obj) {
+ print(obj, false, false);
}
- public void printComma(PyObject o) {
- print(o, true, false);
+ public void printComma(PyObject obj) {
+ print(obj, true, false);
}
- public void println(PyObject o) {
- print(o, false, true);
+ public void println(PyObject obj) {
+ print(obj, false, true);
}
public void println() {
- PyObject obj = myFile();
+ PyObject out = myFile();
- if (obj instanceof PyFile) {
- PyFile file = (PyFile) obj;
+ 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);
+ 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,11 +1339,30 @@
@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>();
int kwlen = kwds.length;
- for (int i=kwlen;i>0;i--) {
+ for (int i=kwlen; i>0; i--) {
keyargs.put(kwds[kwlen - i], args[args.length - i]);
}
+ 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"));
+ }
+
+ private static PyObject print(PyObject values[], PyObject sep, PyObject end, PyObject file) {
+ StdoutWrapper out = Py.stdout;
+ if (file != null && file != Py.None) {
+ out = new FixedFileWrapper(file);
+ }
+
+ if (values.length == 0) {
+ out.println();
+ } else {
+ boolean newline = end == null || end.__nonzero__();
+ out.print(values, false, newline, sep);
+ }
return Py.None;
}
}
--
Repository URL: http://hg.python.org/jython
More information about the Jython-checkins
mailing list