[pypy-issue] [issue950] startup_hook in readline

hpaulj tracker at bugs.pypy.org
Sun Dec 4 03:30:44 CET 2011


hpaulj <hpj3 at myuw.net> added the comment:

>From PyMOTW (Py Module of the Week)
------------------------
import readline

def startup_hook():
    readline.insert_text('from startup_hook')

readline.set_startup_hook(startup_hook)

while True:
    line = raw_input('Prompt ("stop" to quit): ')
    if 'stop' in line:
        break
    print 'ENTERED: "%s"' % line
---------------------
Under regular python, the prompt will be:
Prompt ("stop" to quit): from startup_hook

under pypy 1.6 it first gives
AttributeError: 'ReadlineAlikeReader' object has no attribute 'pos'

and once that is corrected, the prompt is just:
Prompt ("stop" to quit):

that is, the 'from startup_hook' message is missing.

-----------------
my suggested change to 
pyrepl/readline.py is

    def raw_input(self, prompt=''):
        try:
            reader = self.get_reader()
        except _error:
            return _old_raw_input(prompt)
        reader.ps1 = prompt
        return reader.readline(reader, startup_hook = self.startup_hook)

That is, instead of executing self.startup_hook in raw_input, it is passed to 
reader.readline to be executed there

and to pyreply/reader.py

    def readline(self, returns_unicode=False, startup_hook=None):
        self.prepare()
        try:
            if startup_hook is not None:
                startup_hook()
            self.refresh()
            while not self.finished:
                self.handle1()
            if returns_unicode:
                return self.get_unicode()
            return self.get_buffer()
        finally:
            self.restore()

________________________________________
PyPy bug tracker <tracker at bugs.pypy.org>
<https://bugs.pypy.org/issue950>
________________________________________


More information about the pypy-issue mailing list