Index: ReadlineConsole.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/util/ReadlineConsole.java,v retrieving revision 1.5 diff -c -r1.5 ReadlineConsole.java *** ReadlineConsole.java 2001/12/10 20:38:54 1.5 --- ReadlineConsole.java 2002/01/11 02:27:51 *************** *** 6,11 **** --- 6,16 ---- // Based on CPython-1.5.2's code module + /** + * This class wraps several possible libraries that supply Readline + * functionality to the console and to other console-like objects like + * cmd.Cmd + */ public class ReadlineConsole extends InteractiveConsole { public String filename; *************** *** 25,43 **** // Silently ignore errors during load of the native library. // Will use a pure java fallback. } - Readline.initReadline("jpython"); - } /** ! * Write a prompt and read a line. * ! * The returned line does not include the trailing newline. When the ! * user enters the EOF key sequence, EOFError is raised. ! * ! * This subclass implements the functionality using JavaReadline. ! **/ public String raw_input(PyObject prompt) { try { return Readline.readline(prompt==null ? "" : prompt.toString()); } catch (java.io.EOFException eofe) { --- 30,67 ---- // Silently ignore errors during load of the native library. // Will use a pure java fallback. } + // hook into the builtins table so that clients like cmd.Cmd can + // also use readline. + Py.getSystemState().builtins.__setitem__("raw_input", + new raw_inputFunction()); + + Readline.initReadline("jython"); + } /** ! * Override of base-class method used by the console itself works ! * through Readline. Note that we can't rely on the base class ! * call of the builtin method because that calls direct into builtins, ! * does not pay attention to the hook. * ! * @param prompt the prompt to be displayed at the beginning of line ! * @return the user input ! */ public String raw_input(PyObject prompt) { + return _raw_input(prompt); + } + + + /** + * Central point of dispatch to Readline library for all clients, + * whether the console itself or others like cmd.Cmd interpreters. + * Both of these uses come through here. + * + * @param prompt the prompt to be displayed at the beginning of line + * @return the user input + **/ + static String _raw_input(PyObject prompt) { try { return Readline.readline(prompt==null ? "" : prompt.toString()); } catch (java.io.EOFException eofe) { *************** *** 46,49 **** --- 70,104 ---- throw new PyException(Py.IOError); } } + } + /** + * This class puts a python wrapper around _raw_input() so that it can + * be placed in the __builtins__ dictionary for use by console-like + * interpreter shells like cmd.Cmd + */ + class raw_inputFunction extends PyObject { + public raw_inputFunction() {} + + public PyObject __call__(PyObject args[], String kws[]) { + ArgParser ap = new ArgParser("raw_input", args, kws, "prompt"); + PyObject prompt = ap.getPyObject(0, new PyString("")); + String input = ReadlineConsole._raw_input(prompt.__str__()); + PyString data = new PyString(null==input ? "" : input); + if (data.endswith("\n")) { + return data.__getslice__(Py.Zero, Py.newInteger(-1)); + } else { + if (data.__len__() == 0) { + throw Py.EOFError("raw_input()"); + } + } + return data; + } + } + + + + + + +