Contents

The Console

Left to itself, a Java program simply reads and writes characters at the console through JVM-provided streams. This plain behaviour may be augmented by the command window on your system with some line-editing capability (such as using the cursor keys to revisit a typo back up the line), or the ability to recall previously entered lines. On Unix-like systems it is common for a shell or console application to use a library that provides sophisticated editing and line recall, while the console itself provides little more than a line buffer with delete. On Windows, the command window provides basic editing and recall, from which applications benefit without further effort.

In order to privide a common behaviour across platforms, the Jython interpreter (the one that gives you the Python >>>  prompt) will access the console through a library called JLine, when it detects that the console is interactive. You may choose otherwise.

The choice of console affects interactions with the user through sys,stdin, sys.stdout and raw_input(). raw_input() always uses the other two streams to issue its prompt and read your response. The Python prompt >>>  is issued through raw_input().

Choosing a Console (jython command)

The console is chosen by naming a class in the Jython setting python.console (see Settings). The sources of this setting, in descending order of precedence are:

  1. Java System properties (including a command-line -Dpython.console= option).

  2. The Jython registry.
  3. The default value org.python.util.JLineConsole set by the jython program.

The default Jython registry makes no setting of python.console so if you do not set python.console on the command line you will get a JLineConsole. If you specify a class that cannot be found, or some other error occurs while loading and initialising the alleged console, you will get a non-fatal error and a PlainConsole.

Choosing a Console (embedded interpreter)

The console is chosen by naming a class in the Jython setting python.console (see Settings). The sources of this setting, in descending order of precedence are:

  1. The Jython registry.
  2. Java System properties (including a command-line -Dpython.console= option).

If python.console remains undefined the console class is org.python.core.PlainConsole.

The default Jython registry makes no setting of python.console so if you do not set python.console on the command line you will get a PlainConsole. If you specify a class that cannot be found, or some other error occurs while loading and initialising the alleged console, you will get a non-fatal error and a PlainConsole.

When using the embedded interpreter, PlainConsole is the recommended choice as installing the line-editing consoles will have a permanent and perhaps confusing effect on the application's use of the standard streams. If you use a line-diting console, do so by setting python.console and getting the interpreter before taking any references to System.in or System.out. When using the JSR-223 script engine, the engine directs the interpreter to use its own streams, with which it wraps System.in and System.out before initialising Jython. The ScriptEngine does not then benefit from the installed console.

Properties of the Available Consoles

JLineConsole

JLineConsole (-Dpython.console=org.python.util.JLineConsole) provides line-editing cross-platform. The behaviour of the system terminal is radically altered, and System.in and System.out are both replaced by specialised objects. sys.stdin and sys.stdout wrap these objects instead of the original JVM ones. In the event of an uncontrolled exit of the JVM you may need to type blindly stty sane (Unix) to get a console that echoes correctly.

In version 2.7 of Jython, the JLine console has been tested to work with:

ReadlineConsole

ReadlineConsole: (-Dpython.console=org.python.util.ReadlineConsole) is an alternative (Unix) line-editing console that uses the library libjavareadline. The behaviour of the system terminal is altered, and System.in and System.out are both replaced by specialised objects. sys.stdin and sys.stdout wrap these objects instead of the original JVM ones. The readline console takes an extra setting python.console.readlinelib with values of GnuReadline or Editline.

In version 2.7 of Jython, the Readline console has been tested using -Dpython.console.readlinelib=GnuReadline to work with:

The Readline console has been tested using -Dpython.console.readlinelib=Editline to work with

PlainConsole

PlainConsole: (-Dpython.console=org.python.core.PlainConsole or simply -Dpython.console= ) provides no (additional) line-editing. System.inand System.out are unmolested.

PlainConsole is inconvenient on Linux because there is no line recall or and not facility to correct typing mistakes other than to delete backwards to the error. It is acceptable on Windows since the system itself provides some line recall and editing.

Notes on using the Consoles

Encoding is a tricky subject.

Linux

This is based on Linux Mint 14 (a Ubuntu derivative), the bash shell and Gnome Terminal. By default, the environment uses UTF-8 and you can expect the JLineConsole and ReadlineConsole (with python.console.readlinelib=GnuReadline) to work. The ReadlineConsole with python.console.readlinelib=Editline appears only to accept US-ASCII.

These notes do not cover how to type characters outside the US-ASCII set. You can install a range of keyboard mappings, and locales that support the encodings you need. This author uses the UK Extended WinKeys keyboard layout to access a range of accented characters and for these tests also installed a Greek keyboard layout and locale.

To obtain an environment with ISO-8859-1 encoding, launch as

$ export LC_ALL=en_GB.iso88591
$ jython

or en_US.iso88591 if that's appropriate. You must set the encoding in the terminal window menu as well as through the shell environment.

The JVM should then pick up the encoding in use and configure the console object, and sys.stdin and sys.stdout to use it. To check this, use:

>>> import sys
>>> sys.stdin.encoding
'ISO-8859-1'

Windows

This is based on Windows 7 command shell. When using the command window with non-ascii characters, in order to get characters represented correctly on screen, you must set a "TrueType" font (from the properties menu of the window). You can expect the JLineConsole to work with any single-byte encoding, but it does not work with codepage 65001, the close approximation to UTF-8. The root of this problem is that Windows uses a different "wide character" API for this, while JLine only calls the byte-oriented one.

These notes do not cover how to install support for alternate languages and keyboard mappings. This author uses the UK Extended keyboard layout to access a range of accented characters and for these tests also installed a Greek keyboard layout.

To obtain an environment with ISO-8859-1 encoding, launch as

> chcp 1252
> jython

(You may find cp1252 is your default.) The JVM should then pick up the encoding in use and configure the console object, and sys.stdin and sys.stdout to use it. To check this, use:

>>> import sys
>>> sys.stdin.encoding
'Cp1252'

ConsoleChoices (last edited 2014-06-15 07:23:09 by JeffAllen)