Where to insert Python readline?

Chris Gonnerman chris.gonnerman at newcenturycomputers.net
Sun May 18 18:44:21 EDT 2003


----- Original Message ----- 
From: "Gary Bishop" <gb at cs.unc.edu>


> I'd like to implement a GNU Readline work-a-like in Python. I'm
> encouraged by how easy it was to convert Fredrik Lundh's "Console"
> extension to Python using ctypes (great stuff!).

I hate to say this, but I've been there and done that
(well, mostly anyway).  Check out:

    http://newcenturycomputers.net/projects/readline.html

> Suppose I implement a Python function that works somewhat like GNU
> Readline. My question is, how/where should I inject it into the
> existing system to get the effect of using GNU Readline as it works
> now?
> 
> It seems to me there are several possibilities:
> 
> 1) I could replace __builtins__.raw_input. That would be really
> easy. Would that cover all the cases? I don't think so. It looks to me
> like the C code makes direct calls to PyOS_Readline in several
> places. What would I miss with this approach?

The command line itself, as far as I remember.
 
> 2) I could use code.interact and pass in my function. This would get
> me a command prompt that uses my function. Would that cover all
> the cases? Seems it would handle the command prompt nicely but I don't
> know if it would handle various calls to input.

Probably would work fine, but might be convoluted to write.

> 3) I could make a C-callable wrapper function (still within ctypes!)
> and put a pointer to it in PyOS_ReadlineFunctionPointer. This would
> be a bit strange but it should work.

The code in C (regardless of ctypes, which I know nothing about)
is really simple.  REALLY simple.  I borrowed a chunk of code
from /F (with credit given) and created a very small module in
C that allows registering a Python function as the readline
procedure.

Take a look at the _rlsetup.c source in my source archive.  It
really is quite simple.

My Alternative Readline for Windows lacks completion, which I never
use so I don't miss it.  Many have complained of this lack but
nobody has implemented the missing functionality as far as I know.

Chris Gonnerman -- chris.gonnerman at newcenturycomputers.net
http://newcenturycomputers.net







More information about the Python-list mailing list