curses & disable readline & replace stdin

Brano Zarnovican zarnovican at pobox.sk
Fri Mar 4 04:49:52 EST 2005


Hi !

I'd like to init curses and still have working Python interactive
command line. I found that you can replace stdin/stdout/stderr
like this:

#!/usr/bin/python -i

import curses
import sys
import atexit

# this doesn't help, anyway
#del sys.modules['readline']

# global variable (curses window)
stdscr = None

class FakeStdIO(object):

  def write(self, str):
    global stdscr
    stdscr.addstr(str)
    stdscr.refresh()

  def readline(self):
    print "$ ",
    return stdscr.getstr()

stdscr = curses.initscr()
atexit.register(curses.endwin)
curses.echo()

f = FakeStdIO()
sys.stdin  = f
sys.stdout = f
sys.stderr = f

print 'Hello, curses !'        # <-- this works

l = sys.stdin.readline()       # <-- this also works fine

# <-- interactive Python prompt doesn't echo imput

It looks that '>>>' prompt is using the readline module, which
is not using sys.stdin at all.

Q: Can you prevent python interpreter to use readline module
  (even when it is available), *without* recompiling python ?

I think it should be possible to write a module in C to access
Python internals. But can you do it "from python" ?

The other option is to make readline somehow work with curses.
This is however not what I intended.

Platform: Linux Mandrake, Python 2.3.5

Thanks,

BranoZ




More information about the Python-list mailing list