Skip to content

Commit 2e5843e

Browse files
gh-151744: Add curses.nofilter() (GH-151747)
Wrap the ncurses nofilter() function, which undoes the effect of filter(). Without it there is no way to restore normal screen sizing after a curses.filter() call in the same process. Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 7d4a0aa commit 2e5843e

9 files changed

Lines changed: 138 additions & 2 deletions

File tree

Doc/library/curses.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,17 @@ The module :mod:`!curses` defines the following functions:
204204
character-at-a-time line editing without touching the rest of the screen.
205205

206206

207+
.. function:: nofilter()
208+
209+
Undo the effect of a previous :func:`.filter` call.
210+
Like :func:`.filter`, it must be called before :func:`initscr` so that the
211+
next initialization uses the full screen again.
212+
213+
Availability: if the underlying curses library provides ``nofilter()``.
214+
215+
.. versionadded:: next
216+
217+
207218
.. function:: flash()
208219

209220
Flash the screen. That is, change it to reverse-video and then change it back

Doc/whatsnew/3.16.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ New modules
8686
Improved modules
8787
================
8888

89+
curses
90+
------
91+
92+
* Add :func:`curses.nofilter`, which undoes the effect of :func:`curses.filter`.
93+
(Contributed by Serhiy Storchaka in :gh:`151744`.)
94+
8995
gzip
9096
----
9197

Lib/test/test_curses.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,9 @@ def setUp(self):
120120
@requires_curses_func('filter')
121121
def test_filter(self):
122122
# TODO: Should be called before initscr() or newterm() are called.
123-
# TODO: nofilter()
124123
curses.filter()
124+
if hasattr(curses, 'nofilter'):
125+
curses.nofilter()
125126

126127
@requires_curses_func('use_env')
127128
def test_use_env(self):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add :func:`curses.nofilter`, which undoes the effect of :func:`curses.filter`.

Modules/_cursesmodule.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3220,6 +3220,28 @@ _curses_filter_impl(PyObject *module)
32203220
}
32213221
#endif
32223222

3223+
#ifdef HAVE_CURSES_NOFILTER
3224+
/*[clinic input]
3225+
_curses.nofilter
3226+
3227+
Undo the effect of a preceding filter() call.
3228+
3229+
Must be called before initscr(). It restores the normal behaviour
3230+
disabled by filter(), so that the next initscr() uses the full screen
3231+
rather than a single line.
3232+
[clinic start generated code]*/
3233+
3234+
static PyObject *
3235+
_curses_nofilter_impl(PyObject *module)
3236+
/*[clinic end generated code: output=d95ca4d48a6bdbdf input=58aea83b1a5c969f]*/
3237+
{
3238+
/* not checking for PyCursesInitialised here since nofilter() must
3239+
be called before initscr() */
3240+
nofilter();
3241+
Py_RETURN_NONE;
3242+
}
3243+
#endif
3244+
32233245
/*[clinic input]
32243246
_curses.baudrate
32253247
@@ -5321,6 +5343,7 @@ static PyMethodDef cursesmodule_methods[] = {
53215343
_CURSES_ENDWIN_METHODDEF
53225344
_CURSES_ERASECHAR_METHODDEF
53235345
_CURSES_FILTER_METHODDEF
5346+
_CURSES_NOFILTER_METHODDEF
53245347
_CURSES_FLASH_METHODDEF
53255348
_CURSES_FLUSHINP_METHODDEF
53265349
_CURSES_GETMOUSE_METHODDEF

Modules/clinic/_cursesmodule.c.h

Lines changed: 31 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

configure

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

configure.ac

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7192,6 +7192,7 @@ PY_CHECK_CURSES_FUNC([immedok])
71927192
PY_CHECK_CURSES_FUNC([syncok])
71937193
PY_CHECK_CURSES_FUNC([wchgat])
71947194
PY_CHECK_CURSES_FUNC([filter])
7195+
PY_CHECK_CURSES_FUNC([nofilter])
71957196
PY_CHECK_CURSES_FUNC([has_key])
71967197
PY_CHECK_CURSES_FUNC([typeahead])
71977198
PY_CHECK_CURSES_FUNC([use_env])

pyconfig.h.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,9 @@
218218
/* Define if you have the 'is_term_resized' function. */
219219
#undef HAVE_CURSES_IS_TERM_RESIZED
220220

221+
/* Define if you have the 'nofilter' function. */
222+
#undef HAVE_CURSES_NOFILTER
223+
221224
/* Define if you have the 'resizeterm' function. */
222225
#undef HAVE_CURSES_RESIZETERM
223226

0 commit comments

Comments
 (0)