Skip to content

Commit 5fb05ea

Browse files
authored
[3.13] gh-134210: handle signals in _curses.window.getch (GH-134326) (#134784)
(cherry picked from commit 51762b6)
1 parent a5f281f commit 5fb05ea

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
@@ -1385,7 +1385,7 @@ _curses_window_getbkgd_impl(PyCursesWindowObject *self)
13851385
}
13861386

13871387
/*[clinic input]
1388-
_curses.window.getch -> int
1388+
_curses.window.getch
13891389
13901390
[
13911391
y: int
@@ -1402,10 +1402,10 @@ keypad keys and so on return numbers higher than 256. In no-delay mode, -1
14021402
is returned if there is no input, else getch() waits until a key is pressed.
14031403
[clinic start generated code]*/
14041404

1405-
static int
1405+
static PyObject *
14061406
_curses_window_getch_impl(PyCursesWindowObject *self, int group_right_1,
14071407
int y, int x)
1408-
/*[clinic end generated code: output=980aa6af0c0ca387 input=bb24ebfb379f991f]*/
1408+
/*[clinic end generated code: output=e1639e87d545e676 input=73f350336b1ee8c8]*/
14091409
{
14101410
int rtn;
14111411

@@ -1418,7 +1418,17 @@ _curses_window_getch_impl(PyCursesWindowObject *self, int group_right_1,
14181418
}
14191419
Py_END_ALLOW_THREADS
14201420

1421-
return rtn;
1421+
if (rtn == ERR) {
1422+
// We suppress ERR returned by wgetch() in nodelay mode
1423+
// after we handled possible interruption signals.
1424+
if (PyErr_CheckSignals()) {
1425+
return NULL;
1426+
}
1427+
// ERR is an implementation detail, so to be on the safe side,
1428+
// we forcibly set the return value to -1 as documented above.
1429+
rtn = -1;
1430+
}
1431+
return PyLong_FromLong(rtn);
14221432
}
14231433

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