Skip to content

Commit 51762b6

Browse files
authored
gh-134210: handle signals in _curses.window.getch (#134326)
1 parent 579686d commit 51762b6

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:func:`curses.window.getch` now correctly handles signals. Patch by Bénédikt
2+
Tran.

Modules/_cursesmodule.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1656,7 +1656,7 @@ _curses_window_getbkgd_impl(PyCursesWindowObject *self)
16561656
}
16571657

16581658
/*[clinic input]
1659-
_curses.window.getch -> int
1659+
_curses.window.getch
16601660
16611661
[
16621662
y: int
@@ -1673,10 +1673,10 @@ keypad keys and so on return numbers higher than 256. In no-delay mode, -1
16731673
is returned if there is no input, else getch() waits until a key is pressed.
16741674
[clinic start generated code]*/
16751675

1676-
static int
1676+
static PyObject *
16771677
_curses_window_getch_impl(PyCursesWindowObject *self, int group_right_1,
16781678
int y, int x)
1679-
/*[clinic end generated code: output=980aa6af0c0ca387 input=bb24ebfb379f991f]*/
1679+
/*[clinic end generated code: output=e1639e87d545e676 input=73f350336b1ee8c8]*/
16801680
{
16811681
int rtn;
16821682

@@ -1689,7 +1689,17 @@ _curses_window_getch_impl(PyCursesWindowObject *self, int group_right_1,
16891689
}
16901690
Py_END_ALLOW_THREADS
16911691

1692-
return rtn;
1692+
if (rtn == ERR) {
1693+
// We suppress ERR returned by wgetch() in nodelay mode
1694+
// after we handled possible interruption signals.
1695+
if (PyErr_CheckSignals()) {
1696+
return NULL;
1697+
}
1698+
// ERR is an implementation detail, so to be on the safe side,
1699+
// we forcibly set the return value to -1 as documented above.
1700+
rtn = -1;
1701+
}
1702+
return PyLong_FromLong(rtn);
16931703
}
16941704

16951705
/*[clinic input]

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)