01: // Copyright (c) Corporation for National Research Initiatives
02: package org.python.util;
03:
04: import org.gnu.readline.Readline;
05: import org.gnu.readline.ReadlineLibrary;
06: import org.python.core.Py;
07: import org.python.core.PyException;
08: import org.python.core.PyObject;
09: import org.python.core.PySystemState;
10:
11: /**
12: * Uses: <a href="http://java-readline.sourceforge.net/">Java Readline</a> <p/>
13: *
14: * Based on CPython-1.5.2's code module
15: *
16: */
17: public class ReadlineConsole extends InteractiveConsole {
18:
19: public String filename;
20:
21: public ReadlineConsole() {
22: this (null, CONSOLE_FILENAME);
23: }
24:
25: public ReadlineConsole(PyObject locals) {
26: this (locals, CONSOLE_FILENAME);
27: }
28:
29: public ReadlineConsole(PyObject locals, String filename) {
30: super (locals, filename, true);
31: String backingLib = PySystemState.registry.getProperty(
32: "python.console.readlinelib", "Editline");
33: try {
34: Readline.load(ReadlineLibrary.byName(backingLib));
35: } catch (RuntimeException e) {
36: // Silently ignore errors during load of the native library.
37: // Will use a pure java fallback.
38: }
39: Readline.initReadline("jython");
40: }
41:
42: /**
43: * Write a prompt and read a line.
44: *
45: * The returned line does not include the trailing newline. When the user
46: * enters the EOF key sequence, EOFError is raised.
47: *
48: * This subclass implements the functionality using JavaReadline.
49: */
50: public String raw_input(PyObject prompt) {
51: try {
52: String line = Readline.readline(prompt == null ? ""
53: : prompt.toString());
54: return (line == null ? "" : line);
55: } catch (java.io.EOFException eofe) {
56: throw new PyException(Py.EOFError);
57: } catch (java.io.IOException e) {
58: throw new PyException(Py.IOError);
59: }
60: }
61: }
|