Crash in curses stdscr.getkey?

David M. Cooke cookedm+news at physics.mcmaster.ca
Tue Nov 5 00:09:58 EST 2002


At some point, Erik Max Francis <max at alcyone.com> wrote:

> I'd receive a report from someone using CAGE (my cellular automata
> explorer in Python which uses curses) that he was getting crashes; he
> ran SuSE.  He fiddled around fond a workaround.  If he changed the line
> from
>
> 	char = self.stdscr.getkey()
>
> to
>
> 	char = self.stdscr.getch()
> 	if 1 <= char <= 255:
> 	    char = chr(char)
>
> he found that the crash went away.  I had presumed it was just an
> isolated incident (i.e., he had a corrupt curses library on his system
> or something similar), but just the other day another user reported the
> same crash, and when I sent them the workaround, it went away.
>
> Both users use Linux; the first used SuSE and the latter used Red Hat. 
> I use Slackware, and haven't seen any such crash (though both code
> fragments work fine).  The first was using Python 2.2.1; the latter was
> using Python 2.2.2.
>
> Is this a known problem, or should I try to get the endusers to research
> it further to submit bugs to the appropriate parties?  (I can't tell
> whether it's a curses problem or a Python glue problem.)

Looks like it's a problem with the wrapper for stdscr.getkey() -- in
no delay mode (according to the docs), it's supposed to throw an
exception, but doesn't. Digging further, it looks like _cursesmodule.c
has the wrong prototypes for the *getch functions. In particular, it
seems keyname(c) segfaults when c < 0.

Also looks like the doc's claims of throwing an exception from getch()
and getkey() when there is no input in nodelay mode is false.

I've gone ahead and submitted a patch (SF #633635). In the meantime, you can
get this functionality of getkey() with this:

def getkey(stdscr):
    c = stdscr.getch()
    if c == -1:
       raise curses.error, 'no input'
    return curses.keyname(c)

-- 
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke
|cookedm(at)physics(dot)mcmaster(dot)ca



More information about the Python-list mailing list