curses & disable readline & replace stdin

  • Thread starter Brano Zarnovican
  • Start date
B

Brano Zarnovican

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top