Skip to content

Commit 58549cf

Browse files
[3.14] gh-134210: handle signals in _curses.window.getch (GH-134326) (#134783)
gh-134210: handle signals in `_curses.window.getch` (GH-134326) (cherry picked from commit 51762b6) Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
1 parent 452d098 commit 58549cf

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
@@ -1529,7 +1529,7 @@ _curses_window_getbkgd_impl(PyCursesWindowObject *self)
15291529
}
15301530

15311531
/*[clinic input]
1532-
_curses.window.getch -> int
1532+
_curses.window.getch
15331533
15341534
[
15351535
y: int
@@ -1546,10 +1546,10 @@ keypad keys and so on return numbers higher than 256. In no-delay mode, -1
15461546
is returned if there is no input, else getch() waits until a key is pressed.
15471547
[clinic start generated code]*/
15481548

1549-
static int
1549+
static PyObject *
15501550
_curses_window_getch_impl(PyCursesWindowObject *self, int group_right_1,
15511551
int y, int x)
1552-
/*[clinic end generated code: output=980aa6af0c0ca387 input=bb24ebfb379f991f]*/
1552+
/*[clinic end generated code: output=e1639e87d545e676 input=73f350336b1ee8c8]*/
15531553
{
15541554
int rtn;
15551555

@@ -1562,7 +1562,17 @@ _curses_window_getch_impl(PyCursesWindowObject *self, int group_right_1,
15621562
}
15631563
Py_END_ALLOW_THREADS
15641564

1565-
return rtn;
1565+
if (rtn == ERR) {
1566+
// We suppress ERR returned by wgetch() in nodelay mode
1567+
// after we handled possible interruption signals.
1568+
if (PyErr_CheckSignals()) {
1569+
return NULL;
1570+
}
1571+
// ERR is an implementation detail, so to be on the safe side,
1572+
// we forcibly set the return value to -1 as documented above.
1573+
rtn = -1;
1574+
}
1575+
return PyLong_FromLong(rtn);
15661576
}
15671577

15681578
/*[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)