1 package org.python.util; 3 4 import org.gnu.readline.Readline; 5 import org.gnu.readline.ReadlineLibrary; 6 import org.python.core.ArgParser; 7 import org.python.core.Py; 8 import org.python.core.PyException; 9 import org.python.core.PyObject; 10 import org.python.core.PyString; 11 import org.python.core.PySystemState; 12 13 21 public class ReadlineConsole extends InteractiveConsole { 22 public String filename; 23 24 public ReadlineConsole() { 25 this(null, "<console>"); 26 } 27 public ReadlineConsole(PyObject locals) { 28 this(locals, "<console>"); 29 } 30 public ReadlineConsole(PyObject locals, String filename) { 31 super(locals,filename); 32 String backingLib = PySystemState.registry.getProperty( 33 "python.console.readlinelib", "Editline"); 34 try { 35 Readline.load(ReadlineLibrary.byName(backingLib)); 36 } catch (RuntimeException e) { 37 } 40 41 Py.getSystemState().builtins.__setitem__("raw_input", 44 Py.newJavaFunc(this.getClass(), "_raw_input")); 45 46 Readline.initReadline("jython"); 47 } 48 49 50 58 public String raw_input(PyObject prompt) { 59 return _raw_input(new PyObject[] { prompt }, new String [0]); 60 } 61 62 71 public static String _raw_input(PyObject args[], String kws[]) { 72 ArgParser ap = new ArgParser("raw_input", args, kws, "prompt"); 73 PyObject prompt = ap.getPyObject(0, new PyString("")); 74 try { 75 String line = Readline.readline( 76 prompt==null ? "" : prompt.toString()); 77 return (line == null ? "" : line); 78 } catch (java.io.EOFException eofe) { 79 throw new PyException(Py.EOFError); 80 } catch (java.io.IOException e) { 81 throw new PyException(Py.IOError); 82 } 83 } 84 } 85 | Popular Tags |