Skip to content

Commit 81056a3

Browse files
committed
handle input errors in getch, get_wch and getkey
1 parent 7d979cf commit 81056a3

File tree

2 files changed

+17
-27
lines changed

2 files changed

+17
-27
lines changed

Modules/_cursesmodule.c

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1673,7 +1673,7 @@ curses_check_signals_on_input_error(PyCursesWindowObject *self,
16731673
}
16741674

16751675
/*[clinic input]
1676-
_curses.window.getch -> int
1676+
_curses.window.getch
16771677
16781678
[
16791679
y: int
@@ -1690,10 +1690,10 @@ keypad keys and so on return numbers higher than 256. In no-delay mode, -1
16901690
is returned if there is no input, else getch() waits until a key is pressed.
16911691
[clinic start generated code]*/
16921692

1693-
static int
1693+
static PyObject *
16941694
_curses_window_getch_impl(PyCursesWindowObject *self, int group_right_1,
16951695
int y, int x)
1696-
/*[clinic end generated code: output=980aa6af0c0ca387 input=bb24ebfb379f991f]*/
1696+
/*[clinic end generated code: output=e1639e87d545e676 input=73f350336b1ee8c8]*/
16971697
{
16981698
int rtn;
16991699

@@ -1706,7 +1706,12 @@ _curses_window_getch_impl(PyCursesWindowObject *self, int group_right_1,
17061706
}
17071707
Py_END_ALLOW_THREADS
17081708

1709-
return rtn;
1709+
if (rtn == ERR) {
1710+
/* wgetch() returns ERR in nodelay mode */
1711+
const char *funcname = group_right_1 ? "mvwgetch" : "wgetch";
1712+
return curses_check_signals_on_input_error(self, funcname, "getch");
1713+
}
1714+
return PyLong_FromLong(rtn);
17101715
}
17111716

17121717
/*[clinic input]
@@ -1744,14 +1749,9 @@ _curses_window_getkey_impl(PyCursesWindowObject *self, int group_right_1,
17441749
Py_END_ALLOW_THREADS
17451750

17461751
if (rtn == ERR) {
1747-
/* getch() returns ERR in nodelay mode */
1748-
PyErr_CheckSignals();
1749-
if (!PyErr_Occurred()) {
1750-
cursesmodule_state *state = get_cursesmodule_state_by_win(self);
1751-
const char *funcname = group_right_1 ? "mvwgetch" : "wgetch";
1752-
PyErr_Format(state->error, "getkey(): %s(): no input", funcname);
1753-
}
1754-
return NULL;
1752+
/* wgetch() returns ERR in nodelay mode */
1753+
const char *funcname = group_right_1 ? "mvwgetch" : "wgetch";
1754+
return curses_check_signals_on_input_error(self, funcname, "getkey");
17551755
} else if (rtn <= 255) {
17561756
#ifdef NCURSES_VERSION_MAJOR
17571757
#if NCURSES_VERSION_MAJOR*100+NCURSES_VERSION_MINOR <= 507
@@ -1804,14 +1804,9 @@ _curses_window_get_wch_impl(PyCursesWindowObject *self, int group_right_1,
18041804
Py_END_ALLOW_THREADS
18051805

18061806
if (ct == ERR) {
1807-
if (PyErr_CheckSignals())
1808-
return NULL;
1809-
1810-
/* get_wch() returns ERR in nodelay mode */
1811-
cursesmodule_state *state = get_cursesmodule_state_by_win(self);
1807+
/* wget_wch() returns ERR in nodelay mode */
18121808
const char *funcname = group_right_1 ? "mvwget_wch" : "wget_wch";
1813-
PyErr_Format(state->error, "get_wch(): %s(): no input", funcname);
1814-
return NULL;
1809+
return curses_check_signals_on_input_error(self, funcname, "get_wch");
18151810
}
18161811
if (ct == KEY_CODE_YES)
18171812
return PyLong_FromLong(rtn);

Modules/clinic/_cursesmodule.c.h

Lines changed: 3 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)