[Python-Dev] readline history read/write

Skip Montanaro skip@mojam.com (Skip Montanaro)
Tue, 27 Jun 2000 21:51:03 -0500 (CDT)


--lhrBnbs8QA
Content-Type: text/plain; charset=us-ascii
Content-Description: message body and .signature
Content-Transfer-Encoding: 7bit


The original thing that motivated me to propose what became atexit.py was my 
desire to read and write readline history files.  The two attached context
diffs patch Modules/readline.c and Lib/rlcompleter.py to do that.  Two new
functions are added to the readline module and rlcompleter is tweaked to
read the history file referenced by the PYTHONHISTORY environment variable
(if it's defined) and automatically save it when the interpreter exits.

I'm looking for a little feedback.  Here are a few things that jumped to my
mind:

    1. Is it okay to add the read and register/write code to rlcompleter.py?
       Reading and writing readline history files doesn't have anything
       directly to do with identifier completion, but they both have to do
       with readline.

    2. Assuming that history file read/write is desired, should it be
       predicated on the presence of an environment variable or (as in bash
       and other readline-aware programs) should it just happen?

    3. Am I correct in assuming that this capability will only be available
       to Unix/Linux environments, or is readline generally available, thus
       forcing me to think about cross-platform issues?

    4. Is there some mechanism already present in IDLE and/or PythonWin I
       should be aware of and try to emulate?

The two context diffs are both short.  You should be able to follow
everything just by reading the bits of code.  Offering your thoughts on the
above questions shouldn't require actually modifying your interpreter.

Thx,

-- 
Skip Montanaro, skip@mojam.com, http://www.mojam.com/, http://www.musi-cal.com/
On Phil Jackson's ability to manage multiple Lakers superstars, Shaquille
O'Neal said: "He's got the rings.  I listen to the man with the rings."


--lhrBnbs8QA
Content-Type: text/plain
Content-Description: read/write history files
Content-Disposition: inline;
	filename="readline.dif"
Content-Transfer-Encoding: 7bit

*** /tmp/readline.c.~2.17~SlD5BD	Tue Jun 27 21:38:08 2000
--- /tmp/readline.cSlDGMJ	Tue Jun 27 21:38:08 2000
***************
*** 94,99 ****
--- 94,147 ----
  ";
  
  
+ /* Exported function to load a readline history file */
+ 
+ static PyObject *
+ read_history_file(self, args)
+ 	PyObject *self;
+ 	PyObject *args;
+ {
+ 	char *s = NULL;
+ 	if (!PyArg_ParseTuple(args, "|z:read_history_file", &s))
+ 		return NULL;
+ 	errno = read_history(s);
+ 	if (errno)
+ 		return PyErr_SetFromErrno(PyExc_IOError);
+ 	Py_INCREF(Py_None);
+ 	return Py_None;
+ }
+ 
+ static char doc_read_history_file[] = "\
+ read_history_file([filename]) -> None\n\
+ Load a readline history file.\n\
+ The default filename is ~/.history.\
+ ";
+ 
+ 
+ /* Exported function to save a readline history file */
+ 
+ static PyObject *
+ write_history_file(self, args)
+ 	PyObject *self;
+ 	PyObject *args;
+ {
+ 	char *s = NULL;
+ 	if (!PyArg_ParseTuple(args, "|z:write_history_file", &s))
+ 		return NULL;
+ 	errno = write_history(s);
+ 	if (errno)
+ 		return PyErr_SetFromErrno(PyExc_IOError);
+ 	Py_INCREF(Py_None);
+ 	return Py_None;
+ }
+ 
+ static char doc_write_history_file[] = "\
+ write_history_file([filename]) -> None\n\
+ Save a readline history file.\n\
+ The default filename is ~/.history.\
+ ";
+ 
+ 
  /* Exported function to specify a word completer in Python */
  
  static PyObject *completer = NULL;
***************
*** 261,266 ****
--- 309,316 ----
  	{"get_line_buffer", get_line_buffer, 0, doc_get_line_buffer},
  	{"insert_text", insert_text, 1, doc_insert_text},
  	{"read_init_file", read_init_file, 1, doc_read_init_file},
+ 	{"read_history_file", read_history_file, 1, doc_read_history_file},
+ 	{"write_history_file", write_history_file, 1, doc_write_history_file},
  	{"set_completer", set_completer, 1, doc_set_completer},
  	{"get_begidx", get_begidx, 0, doc_get_begidx},
  	{"get_endidx", get_endidx, 0, doc_get_endidx},

--lhrBnbs8QA
Content-Type: text/plain
Content-Description: load and save readline history files automatically
Content-Disposition: inline;
	filename="rlcompleter.dif"
Content-Transfer-Encoding: 7bit

*** /tmp/rlcompleter.py.~1.7~SlDswI	Tue Jun 27 21:36:32 2000
--- /tmp/rlcompleter.pySlD56O	Tue Jun 27 21:36:32 2000
***************
*** 118,120 ****
--- 118,131 ----
      return ret
  
  readline.set_completer(Completer().complete)
+ 
+ import os
+ histfile = os.getenv("PYTHONHISTORY")
+ if histfile is not None:
+     try:
+         readline.read_history_file(histfile)
+     except IOError:
+         pass
+     import atexit
+     atexit.register(readline.write_history_file, histfile)
+ del os, histfile

--lhrBnbs8QA--