[Jython-checkins] jython: Ensure embedded interpreter gets PlainConsole by default.

jeff.allen jython-checkins at python.org
Tue Sep 17 23:53:10 CEST 2013


http://hg.python.org/jython/rev/dbb3710ed6f4
changeset:   7125:dbb3710ed6f4
user:        Jeff Allen <ja.py at farowl.co.uk>
date:        Tue Sep 17 22:24:40 2013 +0100
summary:
  Ensure embedded interpreter gets PlainConsole by default.
When console selection moved into PySystemState, the JLineConsole was invoked in a
wider range of cases. This change ensures only org.python.util.jython invokes it.

files:
  registry                                        |  12 +++++--
  src/org/python/util/jython.java                 |  10 ++++++
  tests/java/org/python/util/InterpreterTest.java |  15 ++++++++++
  3 files changed, 33 insertions(+), 4 deletions(-)


diff --git a/registry b/registry
--- a/registry
+++ b/registry
@@ -30,10 +30,14 @@
 # this option is set from the command line.
 #python.verbose = message
 
-# Jython ships with a JLine console (http://jline.sourceforge.net/)
-# out of the box.
-python.console=org.python.util.JLineConsole
-# To activate explicitly the featureless Jython console, choose:
+# Jython ships with a JLine console (http://jline.sourceforge.net/) out of the
+# box. This is selected by default in the Jython command-line application
+# (org.python.util,jython) if you do not define python.console to be another
+# class on the command line. Alternatively, you could set python.console here,
+# but be aware that this will also affect the console in applications that
+# embed a PythonInterpreter, or use Jython as a JSR-223 script engine.
+#python.console=org.python.util.JLineConsole
+# To activate  the featureless Jython console explicitly, choose:
 #python.console=org.python.core.PlainConsole
 # By setting this to the name of a different console class,
 # new console features can be enabled. For example:
diff --git a/src/org/python/util/jython.java b/src/org/python/util/jython.java
--- a/src/org/python/util/jython.java
+++ b/src/org/python/util/jython.java
@@ -38,6 +38,9 @@
 
 public class jython {
 
+    // An instance of this class will provide the console (python.console) by default.
+    private static final String PYTHON_CONSOLE_CLASS = "org.python.util.JLineConsole";
+
     private static final String COPYRIGHT =
             "Type \"help\", \"copyright\", \"credits\" or \"license\" for more information.";
 
@@ -212,6 +215,13 @@
         if (!opts.fixInteractive || opts.interactive) {
             // The options suggest System.in is interactive: but only if isatty() agrees
             opts.interactive = Py.isInteractive();
+            if (opts.interactive) {
+                // Set the default console type if nothing else has
+                String consoleClassName = preProperties.getProperty("python.console");
+                if (consoleClassName==null) {
+                    preProperties.setProperty("python.console", PYTHON_CONSOLE_CLASS);
+                }
+            }
         }
 
         // Setup the basic python system state from these options
diff --git a/tests/java/org/python/util/InterpreterTest.java b/tests/java/org/python/util/InterpreterTest.java
--- a/tests/java/org/python/util/InterpreterTest.java
+++ b/tests/java/org/python/util/InterpreterTest.java
@@ -4,6 +4,8 @@
 
 import junit.framework.TestCase;
 
+import org.python.core.Console;
+import org.python.core.PlainConsole;
 import org.python.core.Py;
 import org.python.core.PyDictionary;
 import org.python.core.PyInteger;
@@ -63,4 +65,17 @@
             assertEquals(++base, blahInstance.invoke("incval").__tojava__(Integer.class));
         }
     }
+
+    /**
+     * Show that a PythonInterpreter comes by default with a PlainConsole (not JLine say).
+     */
+    public void testConsoleIsPlain() throws Exception {
+        PythonInterpreter interp = new PythonInterpreter();
+        interp.exec("import sys");
+        Console console = Py.tojava(interp.eval("sys._jy_console"), Console.class);
+        assertEquals(PlainConsole.class, console.getClass());
+        Console console2 = Py.getConsole();
+        assertEquals(PlainConsole.class, console2.getClass());
+    }
+
 }

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


More information about the Jython-checkins mailing list