Skip to content

Commit 860675f

Browse files
committed
support replacing sys.std* streams
most mutable fields currently primarily created in PySystemState should be moved to Py.getSystemState().sysdict, it is the dict used by the sys module. Thinking about ps1, ps2 etc
1 parent d2c7431 commit 860675f

File tree

8 files changed

+47
-66
lines changed

8 files changed

+47
-66
lines changed

src/org/python/core/PyBytecode.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,8 +1166,7 @@ protected PyObject interpret(PyFrame f, ThreadState ts) {
11661166
break;
11671167

11681168
default:
1169-
Py.print(Py.getSystemState().stderr,
1170-
Py.newString(
1169+
Py.stderr.print(Py.newUnicode(
11711170
String.format("XXX lineno: %d, opcode: %d\n",
11721171
f.f_lasti, opcode)));
11731172
throw Py.SystemError("unknown opcode");

src/org/python/core/PyGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ public void __del_builtin__() {
217217
}
218218
String msg = String.format("Exception %s: %s in %s", className, pye.value.__repr__(),
219219
__repr__());
220-
Py.println(Py.getSystemState().stderr, Py.newString(msg));
220+
Py.stdout.println(new PyUnicode(msg));
221221
} catch (Throwable t) {
222222
// but we currently ignore any Java exception completely. perhaps we
223223
// can also output something meaningful too?

src/org/python/core/PySystemState.java

Lines changed: 30 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,6 @@ public class PySystemState extends PyObject implements AutoCloseable, Closeable,
162162

163163
private ClassLoader classLoader = null;
164164

165-
public PyObject stdout, stderr, stdin;
166-
public PyObject __stdout__, __stderr__, __stdin__;
167-
168165
public PyObject __displayhook__, __excepthook__;
169166

170167
public PyObject last_value = Py.None;
@@ -288,9 +285,9 @@ private void initEncoding() {
288285
encoding = Py.getConsole().getEncoding();
289286
}
290287

291-
((PyFile)stdin).setEncoding(encoding, errors);
292-
((PyFile)stdout).setEncoding(encoding, errors);
293-
((PyFile)stderr).setEncoding(encoding, "backslashreplace");
288+
((PyFile)getStdin()).setEncoding(encoding, errors);
289+
((PyFile)getStdout()).setEncoding(encoding, errors);
290+
((PyFile)getStderr()).setEncoding(encoding, "backslashreplace");
294291
}
295292

296293
private void initImplementation() {
@@ -329,6 +326,26 @@ public PyObject getBuiltins() {
329326
return builtins;
330327
}
331328

329+
public PyObject getStdout() {
330+
return sysdict.__getitem__("stdout");
331+
}
332+
333+
public PyObject getStderr() {
334+
return sysdict.__getitem__("stderr");
335+
}
336+
337+
public PyObject getStdin() {
338+
return sysdict.__getitem__("stdin");
339+
}
340+
341+
public void setStdout(PyObject stdout) {
342+
sysdict.__setitem__("stdout", stdout);
343+
}
344+
345+
public void setStderr(PyObject stderr) {
346+
sysdict.__setitem__("stderr", stderr);
347+
}
348+
332349
public void setBuiltins(PyObject value) {
333350
builtins = value;
334351
modules.__setitem__("__builtin__", new PyModule("__builtin__", value));
@@ -703,7 +720,7 @@ public void callExitFunc() throws PyIgnoreMethodTag {
703720
exitfunc.__call__();
704721
} catch (PyException exc) {
705722
if (!exc.match(Py.SystemExit)) {
706-
Py.println(stderr, Py.newUnicode("Error in sys.exitfunc:"));
723+
Py.println(getStderr(), Py.newUnicode("Error in sys.exitfunc:"));
707724
}
708725
Py.printException(exc);
709726
}
@@ -1583,9 +1600,9 @@ public void cleanup() {
15831600
private void initstdio() {
15841601
String mode = Options.unbuffered ? "b" : "";
15851602
int buffering = Options.unbuffered ? 0 : 1;
1586-
stdin = __stdin__ = new PyFile(System.in, "<stdin>", "r" + mode, buffering, false);
1587-
stdout = __stdout__ = new PyFile(System.out, "<stdout>", "w" + mode, buffering, false);
1588-
stderr = __stderr__ = new PyFile(System.err, "<stderr>", "w" + mode, 0, false);
1603+
PyObject stdin = new PyFile(System.in, "<stdin>", "r" + mode, buffering, false);
1604+
PyObject stdout = new PyFile(System.out, "<stdout>", "w" + mode, buffering, false);
1605+
PyObject stderr = new PyFile(System.err, "<stderr>", "w" + mode, 0, false);
15891606

15901607
SysModule.setObject("stdin", stdin);
15911608
SysModule.setObject("__stdin__", stdin);
@@ -1819,42 +1836,7 @@ public int traverse(Visitproc visit, Object arg) {
18191836
return retVal;
18201837
}
18211838
}
1822-
if (stdout != null) {
1823-
retVal = visit.visit(stdout, arg);
1824-
if (retVal != 0) {
1825-
return retVal;
1826-
}
1827-
}
1828-
if (stderr != null) {
1829-
retVal = visit.visit(stderr, arg);
1830-
if (retVal != 0) {
1831-
return retVal;
1832-
}
1833-
}
1834-
if (stdin != null) {
1835-
retVal = visit.visit(stdin, arg);
1836-
if (retVal != 0) {
1837-
return retVal;
1838-
}
1839-
}
1840-
if (__stdout__ != null) {
1841-
retVal = visit.visit(__stdout__, arg);
1842-
if (retVal != 0) {
1843-
return retVal;
1844-
}
1845-
}
1846-
if (__stderr__ != null) {
1847-
retVal = visit.visit(__stderr__, arg);
1848-
if (retVal != 0) {
1849-
return retVal;
1850-
}
1851-
}
1852-
if (__stdin__ != null) {
1853-
retVal = visit.visit(__stdin__, arg);
1854-
if (retVal != 0) {
1855-
return retVal;
1856-
}
1857-
}
1839+
18581840
if (__displayhook__ != null) {
18591841
retVal = visit.visit(__displayhook__, arg);
18601842
if (retVal != 0) {
@@ -1899,9 +1881,8 @@ public boolean refersDirectlyTo(PyObject ob) {
18991881
return ob != null && (ob == argv || ob == modules || ob == path
19001882
|| ob == warnoptions || ob == builtins || ob == platform
19011883
|| ob == meta_path || ob == path_hooks || ob == path_importer_cache
1902-
|| ob == ps1 || ob == ps2 || ob == executable || ob == stdout
1903-
|| ob == stderr || ob == stdin || ob == __stdout__ || ob == __stderr__
1904-
|| ob == __stdin__ || ob == __displayhook__ || ob == __excepthook__
1884+
|| ob == ps1 || ob == ps2 || ob == executable
1885+
|| ob == __displayhook__ || ob == __excepthook__
19051886
|| ob == last_value || ob == last_type || ob == last_traceback
19061887
|| ob ==__name__ || ob == __dict__);
19071888
}

src/org/python/core/StderrWrapper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ public StderrWrapper() {
77
}
88

99
protected PyObject getObject(PySystemState ss) {
10-
return ss.stderr;
10+
return ss.getStderr();
1111
}
1212

1313
protected void setObject(PySystemState ss, PyObject obj) {
14-
ss.stderr = obj;
14+
ss.setStderr(obj);
1515
}
1616
}

src/org/python/core/StdoutWrapper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ public StdoutWrapper() {
1313
}
1414

1515
protected PyObject getObject(PySystemState ss) {
16-
return ss.stdout;
16+
return ss.getStdout();
1717
}
1818

1919
protected void setObject(PySystemState ss, PyObject out) {
20-
ss.stdout = out;
20+
ss.sysdict.__setitem__("stdout", out);
2121
}
2222

2323
protected PyObject myFile() {

src/org/python/core/__builtin__.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -992,7 +992,7 @@ private static PyBytes readline(PyObject file) {
992992
* @return line of text from the file (encoded as bytes values compatible with PyBytes)
993993
*/
994994
public static String raw_input(PyObject prompt, PyObject file) {
995-
PyObject stdout = Py.getSystemState().stdout;
995+
PyObject stdout = Py.getSystemState().getStdout();
996996
if (stdout instanceof PyAttributeDeleted) {
997997
throw Py.RuntimeError("[raw_]input: lost sys.stdout");
998998
}
@@ -1016,7 +1016,7 @@ public static String raw_input(PyObject prompt, PyObject file) {
10161016
* @return line of text from console (encoded as bytes values compatible with PyBytes)
10171017
*/
10181018
public static String raw_input(PyObject prompt) {
1019-
PyObject stdin = Py.getSystemState().stdin;
1019+
PyObject stdin = Py.getSystemState().getStdin();
10201020
if (stdin instanceof PyAttributeDeleted) {
10211021
throw Py.RuntimeError("[raw_]input: lost sys.stdin");
10221022
}

src/org/python/util/PythonInterpreter.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.python.core.PyObject;
1616
import org.python.core.PySystemState;
1717
import org.python.core.__builtin__;
18+
import org.python.modules.sys.SysModule;
1819

1920
import java.io.Closeable;
2021
import java.io.Reader;
@@ -129,7 +130,7 @@ protected void setSystemState() {
129130
* @param inStream a Python file-like object to use as the input stream
130131
*/
131132
public void setIn(PyObject inStream) {
132-
getSystemState().stdin = inStream;
133+
SysModule.setObject("stdin", inStream);
133134
}
134135

135136
/**
@@ -191,7 +192,7 @@ public void setIn(java.io.InputStream inStream) {
191192
* @param outStream Python file-like object to use as the output stream
192193
*/
193194
public void setOut(PyObject outStream) {
194-
getSystemState().stdout = outStream;
195+
SysModule.setObject("stdout", outStream);
195196
}
196197

197198
/**
@@ -222,7 +223,7 @@ public void setOut(java.io.OutputStream outStream) {
222223
* @param outStream Python file-like object to use as the error output stream
223224
*/
224225
public void setErr(PyObject outStream) {
225-
getSystemState().stderr = outStream;
226+
SysModule.setObject("stderr", outStream);
226227
}
227228

228229
/**
@@ -393,12 +394,12 @@ public void cleanup() {
393394
PySystemState sys = Py.getSystemState();
394395
sys.callExitFunc();
395396
try {
396-
sys.stdout.invoke("flush");
397+
sys.getStdout().invoke("flush");
397398
} catch (PyException pye) {
398399
// fall through
399400
}
400401
try {
401-
sys.stderr.invoke("flush");
402+
sys.getStderr().invoke("flush");
402403
} catch (PyException pye) {
403404
// fall through
404405
}

tests/java/org/python/modules/_io/_ioTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,21 +127,21 @@ public void openPyFileOStreamByFileno() throws IOException {
127127
@Test
128128
public void openStdinByFileno() throws IOException {
129129
PySystemState sys = Py.getSystemState();
130-
openByFilenoTest(sys.stdin, "rb");
130+
openByFilenoTest(sys.getStdin(), "rb");
131131
}
132132

133133
/** Check <code>sys.stdout.fileno()</code> is acceptable to <code>_io.open()</code> */
134134
@Test
135135
public void openStdoutByFileno() throws IOException {
136136
PySystemState sys = Py.getSystemState();
137-
openByFilenoTest(sys.stdout, "wb");
137+
openByFilenoTest(sys.getStdout(), "wb");
138138
}
139139

140140
/** Check <code>sys.stderr.fileno()</code> is acceptable to <code>_io.open()</code> */
141141
@Test
142142
public void openStderrByFileno() throws IOException {
143143
PySystemState sys = Py.getSystemState();
144-
openByFilenoTest(sys.stderr, "wb");
144+
openByFilenoTest(sys.getStderr(), "wb");
145145
}
146146

147147
/**

0 commit comments

Comments
 (0)