From: <pj...@us...> - 2009-05-10 06:43:50
|
Revision: 6326 http://jython.svn.sourceforge.net/jython/?rev=6326&view=rev Author: pjenvey Date: 2009-05-10 06:43:49 +0000 (Sun, 10 May 2009) Log Message: ----------- enable jline by default, with: o a custom build that fixes its term settings on BSD platforms o its unicode handling disabled since we expect raw bytes o our own keybindings disabling tab completion o it or any custom python.console disabled when not interactive refs #1293 Modified Paths: -------------- trunk/jython/build.xml trunk/jython/registry trunk/jython/src/org/python/util/JLineConsole.java trunk/jython/src/org/python/util/jline-keybindings.properties trunk/jython/src/org/python/util/jython.java Added Paths: ----------- trunk/jython/extlibs/jline-0.9.95-SNAPSHOT.jar Removed Paths: ------------- trunk/jython/extlibs/jline-0.9.94.jar Modified: trunk/jython/build.xml =================================================================== --- trunk/jython/build.xml 2009-05-10 06:42:35 UTC (rev 6325) +++ trunk/jython/build.xml 2009-05-10 06:43:49 UTC (rev 6326) @@ -145,7 +145,7 @@ <!-- classpaths --> <path id="main.classpath"> <pathelement path="${extlibs.dir}/libreadline-java-0.8.jar" /> - <pathelement path="${extlibs.dir}/jline-0.9.94.jar" /> + <pathelement path="${extlibs.dir}/jline-0.9.95-SNAPSHOT.jar" /> <pathelement path="${extlibs.dir}/servlet-api-2.5.jar" /> <pathelement path="${informix.jar}" /> <pathelement path="${oracle.jar}" /> @@ -503,8 +503,8 @@ todir="${compile.dir}/org/python/modules" preservelastmodified="true" /> - <copy todir="${compile.dir}/com" preservelastmodified="true"> - <fileset dir="${source.dir}/com"> + <copy todir="${compile.dir}" preservelastmodified="true"> + <fileset dir="${source.dir}"> <include name="**/*.properties" /> </fileset> </copy> @@ -562,6 +562,8 @@ <rule pattern="org.apache.xerces.**" result="org.python.apache.xerces.@1"/> <rule pattern="org.apache.wml.**" result="org.python.apache.wml.@1"/> <rule pattern="org.apache.html.**" result="org.python.apache.html.@1"/> + <zipfileset src="extlibs/jline-0.9.95-SNAPSHOT.jar"/> + <rule pattern="jline.**" result="org.python.jline.@1"/> <manifest> <attribute name="Main-Class" value="org.python.util.jython" /> <attribute name="Built-By" value="${user.name}" /> Deleted: trunk/jython/extlibs/jline-0.9.94.jar =================================================================== (Binary files differ) Added: trunk/jython/extlibs/jline-0.9.95-SNAPSHOT.jar =================================================================== (Binary files differ) Property changes on: trunk/jython/extlibs/jline-0.9.95-SNAPSHOT.jar ___________________________________________________________________ Added: svn:mime-type + application/octet-stream Modified: trunk/jython/registry =================================================================== --- trunk/jython/registry 2009-05-10 06:42:35 UTC (rev 6325) +++ trunk/jython/registry 2009-05-10 06:43:49 UTC (rev 6326) @@ -30,11 +30,14 @@ # this option is set from the command line. #python.verbose = message -# Setting this to the name of different console class, new console -# features can be enabled. Readline support is such an example -#python.console=org.python.util.JLineConsole +# Jython ships with a JLine console (http://jline.sourceforge.net/) +# out of the box. Setting this to the name of different console class, +# new console features can be enabled. Readline support is such an +# example: #python.console=org.python.util.ReadlineConsole #python.console.readlinelib=JavaReadline +# To activate the legacy Jython console: +#python.console=org.python.util.InteractiveConsole # Setting this to a valid codec name will cause the console to use a # different encoding when reading commands from the console. Modified: trunk/jython/src/org/python/util/JLineConsole.java =================================================================== --- trunk/jython/src/org/python/util/JLineConsole.java 2009-05-10 06:42:35 UTC (rev 6325) +++ trunk/jython/src/org/python/util/JLineConsole.java 2009-05-10 06:43:49 UTC (rev 6326) @@ -1,9 +1,18 @@ package org.python.util; import java.io.File; +import java.io.FileDescriptor; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStreamWriter; +import java.io.Writer; + import jline.ConsoleReader; import jline.Terminal; + import org.python.core.Py; import org.python.core.PyObject; @@ -14,6 +23,8 @@ */ public class JLineConsole extends InteractiveConsole { + protected ConsoleReader reader; + public JLineConsole() { this(null); } @@ -21,36 +32,71 @@ public JLineConsole(PyObject locals) { this(locals, CONSOLE_FILENAME); try { - File historyFile = new File(System.getProperty("user.home"), - ".jline-jython.history"); + File historyFile = new File(System.getProperty("user.home"), ".jline-jython.history"); reader.getHistory().setHistoryFile(historyFile); - } catch(IOException e) { - // oh well, no history from file + } catch (IOException e) { + // oh well, no history from file } } public JLineConsole(PyObject locals, String filename) { super(locals, filename, true); + + // Disable JLine's unicode handling so it yields raw bytes + System.setProperty("jline.UnixTerminal.input.encoding", "ISO-8859-1"); + System.setProperty("jline.WindowsTerminal.input.encoding", "ISO-8859-1"); + Terminal.setupTerminal(); try { - reader = new ConsoleReader(); - } catch(IOException e) { + InputStream input = new FileInputStream(FileDescriptor.in); + // Raw bytes in, so raw bytes out + Writer output = new OutputStreamWriter(new FileOutputStream(FileDescriptor.out), + "ISO-8859-1"); + reader = new ConsoleReader(input, output, getBindings()); + } catch (IOException e) { throw new RuntimeException(e); } } + /** + * Return the JLine bindings file. + * + * This handles loading the user's custom keybindings (normally JLine does) so it can + * fallback to Jython's (which disable tab completition) when the user's are not + * available. + * + * @return an InputStream of the JLine bindings file. + */ + protected InputStream getBindings() { + String userBindings = new File(System.getProperty("user.home"), + ".jlinebindings.properties").getAbsolutePath(); + File bindingsFile = new File(System.getProperty("jline.keybindings", userBindings)); + + try { + if (bindingsFile.isFile()) { + try { + return new FileInputStream(bindingsFile); + } catch (FileNotFoundException fnfe) { + // Shouldn't really ever happen + fnfe.printStackTrace(); + } + } + } catch (SecurityException se) { + // continue + } + return getClass().getResourceAsStream("jline-keybindings.properties"); + } + public String raw_input(PyObject prompt) { String line = null; try { line = reader.readLine(prompt.toString()); - } catch(IOException io) { + } catch (IOException io) { throw Py.IOError(io); } - if(line == null) { + if (line == null) { throw Py.EOFError("Ctrl-D exit"); } return line.endsWith("\n") ? line.substring(0, line.length() - 1) : line; } - - protected ConsoleReader reader; } Modified: trunk/jython/src/org/python/util/jline-keybindings.properties =================================================================== --- trunk/jython/src/org/python/util/jline-keybindings.properties 2009-05-10 06:42:35 UTC (rev 6325) +++ trunk/jython/src/org/python/util/jline-keybindings.properties 2009-05-10 06:43:49 UTC (rev 6326) @@ -24,8 +24,10 @@ # deleting the previous character 8: DELETE_PREV_CHAR -# TAB, CTRL-I: signal that console completion should be attempted -9: COMPLETE +## TAB, CTRL-I: signal that console completion should be attempted +#9: COMPLETE +# Jython needs a real TAB, disable completion +9: UNKNOWN # CTRL-J, CTRL-M: newline 10: NEWLINE Modified: trunk/jython/src/org/python/util/jython.java =================================================================== --- trunk/jython/src/org/python/util/jython.java 2009-05-10 06:42:35 UTC (rev 6325) +++ trunk/jython/src/org/python/util/jython.java 2009-05-10 06:43:49 UTC (rev 6326) @@ -137,9 +137,6 @@ // Setup the basic python system state from these options PySystemState.initialize(PySystemState.getBaseProperties(), opts.properties, opts.argv); - // Now create an interpreter - InteractiveConsole interp = newInterpreter(); - PyList warnoptions = new PyList(); for (String wopt : opts.warnoptions) { warnoptions.append(new PyString(wopt)); @@ -155,6 +152,9 @@ } } + // Now create an interpreter + InteractiveConsole interp = newInterpreter(opts.interactive); + // Print banner and copyright information (or not) if (opts.interactive && opts.notice && !opts.runModule) { System.err.println(InteractiveConsole.getDefaultBanner()); @@ -306,16 +306,25 @@ /** * Returns a new python interpreter using the InteractiveConsole subclass from the * <tt>python.console</tt> registry key. + * <p> + + * When stdin is interactive the default is {@link JLineConsole}. Otherwise the + * featureless {@link InteractiveConsole} is always used as alternative consoles cause + * unexpected behavior with the std file streams. */ - private static InteractiveConsole newInterpreter() { - try { - String interpClass = - PySystemState.registry.getProperty("python.console", - "org.python.util.InteractiveConsole"); - return (InteractiveConsole)Class.forName(interpClass).newInstance(); - } catch (Throwable t) { - return new InteractiveConsole(); + private static InteractiveConsole newInterpreter(boolean interactiveStdin) { + if (interactiveStdin) { + String interpClass = PySystemState.registry.getProperty("python.console", ""); + if (interpClass.length() > 0) { + try { + return (InteractiveConsole)Class.forName(interpClass).newInstance(); + } catch (Throwable t) { + // fall through + } + } + return new JLineConsole(); } + return new InteractiveConsole(); } /** This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |