diff --git a/Lib/test/test_curses.py b/Lib/test/test_curses.py index cdb90dbf8b6..4439fcd13c5 100644 --- a/Lib/test/test_curses.py +++ b/Lib/test/test_curses.py @@ -261,6 +261,10 @@ def test_resize_term(stdscr): if curses.LINES != lines - 1 or curses.COLS != cols + 1: raise RuntimeError, "Expected resizeterm to update LINES and COLS" +def test_issue6243(stdscr): + curses.ungetch(1025) + stdscr.getkey() + def main(stdscr): curses.savetty() try: @@ -268,6 +272,7 @@ def main(stdscr): window_funcs(stdscr) test_userptr_without_set(stdscr) test_resize_term(stdscr) + test_issue6243(stdscr) finally: curses.resetty() diff --git a/Misc/NEWS b/Misc/NEWS index 589590e266f..9882fad4d75 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -66,6 +66,9 @@ Core and Builtins Library ------- +- Issue #6243: curses.getkey() can segfault when used with ungetch. + Fix by Trundle and Jerry Chen. + - Issue #7597: curses.use_env() can now be called before initscr(). Noted by Kan-Ru Chen. diff --git a/Modules/_cursesmodule.c b/Modules/_cursesmodule.c index 4a77b0552ad..e2c17e1c187 100644 --- a/Modules/_cursesmodule.c +++ b/Modules/_cursesmodule.c @@ -882,14 +882,17 @@ PyCursesWindow_GetKey(PyCursesWindowObject *self, PyObject *args) /* getch() returns ERR in nodelay mode */ PyErr_SetString(PyCursesError, "no input"); return NULL; - } else if (rtn<=255) + } else if (rtn<=255) { return Py_BuildValue("c", rtn); - else + } else { + const char *knp; #if defined(__NetBSD__) - return PyString_FromString(unctrl(rtn)); + knp = unctrl(rtn); #else - return PyString_FromString((char *)keyname(rtn)); + knp = keyname(rtn); #endif + return PyString_FromString((knp == NULL) ? "" : knp); + } } static PyObject *