11// Copyright (c) Corporation for National Research Initiatives
22package org .python .util ;
3- import org .python .core .*;
43
5- // Based on CPython-1.5.2's code module
4+ import org .python .core .Py ;
5+ import org .python .core .PyBuiltinFunctionSet ;
6+ import org .python .core .PyException ;
7+ import org .python .core .PyObject ;
8+ import org .python .core .PySystemState ;
9+ import org .python .core .__builtin__ ;
610
11+ // Based on CPython-1.5.2's code module
712public class InteractiveConsole extends InteractiveInterpreter {
13+
14+ public static final String CONSOLE_FILENAME = "<console>" ;
15+
816 public String filename ;
917
1018 public InteractiveConsole () {
11- this (null , "<console>" );
19+ this (null , CONSOLE_FILENAME );
1220 }
21+
1322 public InteractiveConsole (PyObject locals ) {
14- this (locals , "<console>" );
23+ this (locals , CONSOLE_FILENAME );
1524 }
25+
1626 public InteractiveConsole (PyObject locals , String filename ) {
27+ this (locals , filename , false );
28+ }
29+
30+ /**
31+ * @param replaceRawInput -
32+ * if true, we hook this Class's raw_input into the builtins
33+ * table so that clients like cmd.Cmd use it.
34+ */
35+ public InteractiveConsole (PyObject locals , String filename , boolean replaceRawInput ) {
1736 super (locals );
1837 this .filename = filename ;
38+ if (replaceRawInput ) {
39+ PyObject newRawInput = new PyBuiltinFunctionSet ("raw_input" , 0 , 0 , 1 ) {
40+
41+ public PyObject __call__ () {
42+ return __call__ (Py .EmptyString );
43+ }
44+
45+ public PyObject __call__ (PyObject prompt ) {
46+ return Py .newString (raw_input (prompt ));
47+ }
48+ };
49+ Py .getSystemState ().builtins .__setitem__ ("raw_input" , newRawInput );
50+ }
1951 }
2052
2153 /**
2254 * Closely emulate the interactive Python console.
23- *
24- * The optional banner argument specifies the banner to print before
25- * the first interaction; by default it prints a banner similar to the
26- * one printed by the real Python interpreter, followed by the current
27- * class name in parentheses (so as not to confuse this with the real
28- * interpreter -- since it's so close!).
29- **/
55+ *
56+ * The optional banner argument specifies the banner to print before the
57+ * first interaction; by default it prints "Jython <version> on <platform>".
58+ */
3059 public void interact () {
3160 interact (getDefaultBanner ());
3261 }
3362
3463 public static String getDefaultBanner () {
35- return "Jython " + PySystemState .version + " on "
36- + PySystemState .platform ;
64+ return "Jython " + PySystemState .version + " on " + PySystemState .platform ;
3765 }
3866
3967 public void interact (String banner ) {
40- if (banner != null ) {
68+ if (banner != null ) {
4169 write (banner );
4270 write ("\n " );
4371 }
4472 // Dummy exec in order to speed up response on first command
4573 exec ("2" );
46- //System.err.println("interp2");
74+ // System.err.println("interp2");
4775 boolean more = false ;
48- while (true ) {
76+ while (true ) {
4977 PyObject prompt = more ? systemState .ps2 : systemState .ps1 ;
5078 String line ;
5179 try {
5280 line = raw_input (prompt );
53- } catch (PyException exc ) {
54- if (!Py .matchException (exc , Py .EOFError ))
81+ } catch (PyException exc ) {
82+ if (!Py .matchException (exc , Py .EOFError ))
5583 throw exc ;
5684 write ("\n " );
5785 break ;
@@ -62,36 +90,35 @@ public void interact(String banner) {
6290
6391 /**
6492 * Push a line to the interpreter.
65- *
93+ *
6694 * The line should not have a trailing newline; it may have internal
67- * newlines. The line is appended to a buffer and the interpreter's
68- * runsource() method is called with the concatenated contents of the
69- * buffer as source. If this indicates that the command was executed
70- * or invalid, the buffer is reset; otherwise, the command is
71- * incomplete, and the buffer is left as it was after the line was
72- * appended. The return value is 1 if more input is required, 0 if the
73- * line was dealt with in some way (this is the same as runsource()).
74- **/
75-
95+ * newlines. The line is appended to a buffer and the interpreter's
96+ * runsource() method is called with the concatenated contents of the buffer
97+ * as source. If this indicates that the command was executed or invalid,
98+ * the buffer is reset; otherwise, the command is incomplete, and the buffer
99+ * is left as it was after the line was appended. The return value is 1 if
100+ * more input is required, 0 if the line was dealt with in some way (this is
101+ * the same as runsource()).
102+ */
76103 public boolean push (String line ) {
77- if (buffer .length () > 0 )
104+ if (buffer .length () > 0 )
78105 buffer .append ("\n " );
79106 buffer .append (line );
80107 boolean more = runsource (buffer .toString (), filename );
81- if (!more )
108+ if (!more )
82109 resetbuffer ();
83110 return more ;
84111 }
85112
86113 /**
87114 * Write a prompt and read a line.
88- *
89- * The returned line does not include the trailing newline. When the
90- * user enters the EOF key sequence, EOFError is raised.
91- *
115+ *
116+ * The returned line does not include the trailing newline. When the user
117+ * enters the EOF key sequence, EOFError is raised.
118+ *
92119 * The base implementation uses the built-in function raw_input(); a
93120 * subclass may replace this with a different implementation.
94- ** /
121+ */
95122 public String raw_input (PyObject prompt ) {
96123 return __builtin__ .raw_input (prompt );
97124 }
0 commit comments