Skip to content

Commit 6a9dbff

Browse files
[3.15] gh-156234: Fix and rewrite the curses documentation on reading (GH-156235) (#156279)
1 parent 4951a11 commit 6a9dbff

3 files changed

Lines changed: 138 additions & 70 deletions

File tree

Doc/library/curses.rst

Lines changed: 69 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ Linux and the BSD variants of Unix.
2727

2828
Whenever the documentation mentions a *character* it can be specified
2929
as an integer, a one-character Unicode string or a one-byte byte string.
30+
An integer is the code of a single encoded byte, optionally combined with
31+
attributes and a color pair, as returned by :meth:`window.inch`.
3032

3133
Whenever the documentation mentions a *character string* it can be specified
3234
as a Unicode string or a byte string.
@@ -502,8 +504,8 @@ The module :mod:`!curses` defines the following functions:
502504
.. function:: putp(str)
503505

504506
Equivalent to ``tputs(str, 1, putchar)``; emit the value of a specified
505-
terminfo capability for the current terminal. Note that the output of :func:`putp`
506-
always goes to standard output.
507+
terminfo capability, a bytes object, for the current terminal.
508+
Note that the output of :func:`putp` always goes to standard output.
507509

508510
:func:`setupterm` (or :func:`initscr`) must be called first.
509511

@@ -674,7 +676,7 @@ The module :mod:`!curses` defines the following functions:
674676
.. function:: tparm(str[, ...])
675677

676678
Instantiate the bytes object *str* with the supplied parameters, where *str* should
677-
be a parameterized string obtained from the terminfo database. For example,
679+
be a parameterized byte string obtained from the terminfo database. For example,
678680
``tparm(tigetstr("cup"), 5, 3)`` could result in ``b'\033[6;4H'``, the exact
679681
result depending on terminal type. Up to nine integer parameters may be supplied.
680682

@@ -695,7 +697,8 @@ The module :mod:`!curses` defines the following functions:
695697

696698
.. function:: unctrl(ch)
697699

698-
Return a bytes object which is a printable representation of the character *ch*.
700+
Return a bytes object which is a printable representation of the character *ch*;
701+
any attributes and color pair are ignored.
699702
Control characters are represented as a caret followed by the character, for
700703
example as ``b'^C'``. Printing characters are left as they are.
701704

@@ -704,6 +707,9 @@ The module :mod:`!curses` defines the following functions:
704707

705708
Push *ch* so the next :meth:`~window.getch` will return it.
706709

710+
*ch* may be an integer (a key code or the code of an encoded byte), a byte,
711+
or a string of length 1 which encodes to a single byte.
712+
707713
.. note::
708714

709715
Only one *ch* can be pushed before :meth:`!getch` is called.
@@ -721,6 +727,9 @@ The module :mod:`!curses` defines the following functions:
721727

722728
Push *ch* so the next :meth:`~window.get_wch` will return it.
723729

730+
*ch* may be an integer (a character code, not a key code) or a string of
731+
length 1.
732+
724733
.. note::
725734

726735
Only one *ch* can be pushed before :meth:`!get_wch` is called.
@@ -1001,27 +1010,58 @@ Window objects
10011010

10021011
.. method:: window.getch([y, x])
10031012

1004-
Get a character. Note that the integer returned does *not* have to be in ASCII
1005-
range: function keys, keypad keys and so on are represented by numbers higher
1006-
than 255. In no-delay mode, return ``-1`` if there is no input, otherwise
1007-
wait until a key is pressed.
1013+
Read a key press, after moving the cursor to *y*, *x* if specified,
1014+
and return it as an integer.
1015+
The window is refreshed first if it is not a pad and was modified since
1016+
the last refresh.
1017+
Wait until a key is pressed, or return ``-1`` if the read is non-blocking
1018+
or times out (see :meth:`nodelay` and :meth:`timeout`).
1019+
1020+
An ordinary key is returned as the code of a single byte of its encoding
1021+
in the current locale,
1022+
so a character encoded with several bytes takes several calls.
1023+
For example, in a UTF-8 locale ``'é'`` is read as ``195``, then ``169``.
1024+
Use :meth:`get_wch` to read it as a single character.
1025+
1026+
In keypad mode (see :meth:`keypad`) function keys and other special keys
1027+
are returned as one of the :ref:`KEY_* constants <curses-key-constants>`,
1028+
which cannot be mistaken for an ordinary key.
1029+
Otherwise, or if their escape sequence does not arrive in time
1030+
(see :meth:`notimeout` and :func:`set_escdelay`),
1031+
their bytes are returned one at a time.
1032+
1033+
In echo mode (see :func:`echo`) the key is added to the window as by
1034+
:meth:`addch`; special keys are not echoed.
10081035

10091036

10101037
.. method:: window.get_wch([y, x])
10111038

1012-
Get a wide character. Return a character for most keys, or an integer for
1013-
function keys, keypad keys, and other special keys.
1014-
In no-delay mode, raise an exception if there is no input.
1039+
Read a key press, after moving the cursor to *y*, *x* if specified,
1040+
and return it as a one-character :class:`str`.
1041+
The window is refreshed first if it is not a pad and was modified since
1042+
the last refresh.
1043+
Wait until a key is pressed, or raise :exc:`error` if the read is
1044+
non-blocking or times out (see :meth:`nodelay` and :meth:`timeout`).
1045+
1046+
In keypad mode (see :meth:`keypad`) function keys and other special keys
1047+
are returned as one of the :ref:`KEY_* constants <curses-key-constants>`,
1048+
an integer.
1049+
Otherwise, or if their escape sequence does not arrive in time
1050+
(see :meth:`notimeout` and :func:`set_escdelay`),
1051+
their characters are returned one at a time.
1052+
1053+
In echo mode (see :func:`echo`) the key is added to the window as by
1054+
:meth:`addch`; special keys are not echoed.
10151055

10161056
.. versionadded:: 3.3
10171057

10181058

10191059
.. method:: window.getkey([y, x])
10201060

1021-
Get a character, returning a string instead of an integer, as :meth:`getch`
1022-
does. Function keys, keypad keys and other special keys return a multibyte
1023-
string containing the key name. In no-delay mode, raise an exception if
1024-
there is no input.
1061+
Read a key press as :meth:`getch` does, but return it as a :class:`str`:
1062+
an ordinary key as a one-character string, the byte decoded as Latin-1,
1063+
and a special key as its name, such as ``'KEY_UP'`` (see :func:`keyname`).
1064+
Raise :exc:`error` instead of returning ``-1`` if there is no input.
10251065

10261066

10271067
.. method:: window.getmaxyx()
@@ -1041,8 +1081,11 @@ Window objects
10411081
window.getstr(y, x)
10421082
window.getstr(y, x, n)
10431083

1044-
Read a bytes object from the user, with primitive line editing capacity.
1045-
At most *n* characters are read;
1084+
Read a line of input from the user, with primitive line editing capacity,
1085+
after moving the cursor to *y*, *x* if specified.
1086+
Return it as a bytes object, in the encoding of the current locale
1087+
and without the terminating newline.
1088+
At most *n* bytes are read;
10461089
*n* defaults to and cannot exceed 2047.
10471090

10481091
.. versionchanged:: 3.14
@@ -1144,12 +1187,11 @@ Window objects
11441187
.. method:: window.instr([n])
11451188
window.instr(y, x[, n])
11461189

1147-
Return a bytes object of characters, extracted from the window starting at the
1148-
current cursor position, or at *y*, *x* if specified, and stopping at the end
1149-
of the line. Attributes and color information are stripped
1150-
from the characters. If *n* is specified, :meth:`instr` returns a string
1151-
at most *n* characters long (exclusive of the trailing NUL).
1152-
The maximum value for *n* is 2047.
1190+
Read the text of the window from the current cursor position,
1191+
or from *y*, *x* if specified, to the end of the line,
1192+
and return it as a bytes object, in the encoding of the current locale.
1193+
Attributes and color pairs are stripped.
1194+
At most *n* bytes are read; *n* defaults to and cannot exceed 2047.
11531195

11541196
.. versionchanged:: 3.14
11551197
The maximum value for *n* was increased from 1023 to 2047.
@@ -1173,6 +1215,8 @@ Window objects
11731215
If *flag* is ``True``, escape sequences generated by some keys (keypad, function keys)
11741216
will be interpreted by :mod:`!curses`. If *flag* is ``False``, escape sequences will be
11751217
left as is in the input stream.
1218+
Keypad mode is disabled by default, but :func:`wrapper` enables it for the
1219+
main window.
11761220

11771221

11781222
.. method:: window.leaveok(flag)
@@ -1525,6 +1569,8 @@ by some methods.
15251569
| | color-pair field information |
15261570
+-------------------------+-------------------------------+
15271571

1572+
.. _curses-key-constants:
1573+
15281574
Keys are referred to by integer constants with names starting with ``KEY_``.
15291575
The exact keycaps available are system dependent.
15301576

Modules/_cursesmodule.c

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1747,18 +1747,25 @@ _curses.window.getch
17471747
]
17481748
/
17491749
1750-
Get a character code from terminal keyboard.
1750+
Read a key press and return it as an integer.
17511751
1752-
The integer returned does not have to be in ASCII range: function
1753-
keys, keypad keys and so on return numbers higher than 256. In
1754-
no-delay mode, -1 is returned if there is no input, else getch()
1755-
waits until a key is pressed.
1752+
Wait until a key is pressed, or return -1 if the read is
1753+
non-blocking or times out.
1754+
1755+
An ordinary key is returned as the code of a single byte of its
1756+
encoding in the current locale, so a character encoded with several
1757+
bytes takes several calls. Use get_wch() to read it as a single
1758+
character.
1759+
1760+
In keypad mode function keys and other special keys are returned as
1761+
one of the KEY_* constants, which cannot be mistaken for an ordinary
1762+
key. Otherwise their bytes are returned one at a time.
17561763
[clinic start generated code]*/
17571764

17581765
static PyObject *
17591766
_curses_window_getch_impl(PyCursesWindowObject *self, int group_right_1,
17601767
int y, int x)
1761-
/*[clinic end generated code: output=e1639e87d545e676 input=0dc5ff40e079787a]*/
1768+
/*[clinic end generated code: output=e1639e87d545e676 input=882ddab9b41afbbd]*/
17621769
{
17631770
int rtn;
17641771

@@ -1795,18 +1802,18 @@ _curses.window.getkey
17951802
]
17961803
/
17971804
1798-
Get a character (string) from terminal keyboard.
1805+
Read a key press and return it as a str.
17991806
1800-
Returning a string instead of an integer, as getch() does. Function
1801-
keys, keypad keys and other special keys return a multibyte string
1802-
containing the key name. In no-delay mode, an exception is raised
1803-
if there is no input.
1807+
Read as getch() does, but return an ordinary key as a one-character
1808+
string, the byte decoded as Latin-1, and a special key as its name,
1809+
such as 'KEY_UP'. Raise curses.error instead of returning -1 if
1810+
there is no input.
18041811
[clinic start generated code]*/
18051812

18061813
static PyObject *
18071814
_curses_window_getkey_impl(PyCursesWindowObject *self, int group_right_1,
18081815
int y, int x)
1809-
/*[clinic end generated code: output=8490a182db46b10f input=bd24a7da1ed9c73b]*/
1816+
/*[clinic end generated code: output=8490a182db46b10f input=f054cf034c69e879]*/
18101817
{
18111818
int rtn;
18121819

@@ -1851,16 +1858,20 @@ _curses.window.get_wch
18511858
]
18521859
/
18531860
1854-
Get a wide character from terminal keyboard.
1861+
Read a key press and return it as a one-character str.
1862+
1863+
Wait until a key is pressed, or raise curses.error if the read is
1864+
non-blocking or times out.
18551865
1856-
Return a character for most keys, or an integer for function keys,
1857-
keypad keys, and other special keys.
1866+
In keypad mode function keys and other special keys are returned as
1867+
one of the KEY_* constants, an integer. Otherwise their characters
1868+
are returned one at a time.
18581869
[clinic start generated code]*/
18591870

18601871
static PyObject *
18611872
_curses_window_get_wch_impl(PyCursesWindowObject *self, int group_right_1,
18621873
int y, int x)
1863-
/*[clinic end generated code: output=9f4f86e91fe50ef3 input=dd7e5367fb49dc48]*/
1874+
/*[clinic end generated code: output=9f4f86e91fe50ef3 input=77eb2da426ebe71f]*/
18641875
{
18651876
int ct;
18661877
wint_t rtn;
@@ -1928,14 +1939,14 @@ curses_clinic_parse_optional_xy_n(PyObject *args,
19281939

19291940
PyDoc_STRVAR(_curses_window_getstr__doc__,
19301941
"getstr([[y, x,] n=2047])\n"
1931-
"Read a string from the user, with primitive line editing capacity.\n"
1942+
"Read a line of input and return it as a bytes object.\n"
19321943
"\n"
19331944
" y\n"
19341945
" Y-coordinate.\n"
19351946
" x\n"
19361947
" X-coordinate.\n"
19371948
" n\n"
1938-
" Maximal number of characters.");
1949+
" Maximal number of bytes.");
19391950

19401951
static PyObject *
19411952
PyCursesWindow_getstr(PyObject *op, PyObject *args)
@@ -2180,21 +2191,19 @@ _curses_window_inch_impl(PyCursesWindowObject *self, int group_right_1,
21802191

21812192
PyDoc_STRVAR(_curses_window_instr__doc__,
21822193
"instr([y, x,] n=2047)\n"
2183-
"Return a string of characters, extracted from the window.\n"
2194+
"Return the text of the window as a bytes object.\n"
21842195
"\n"
21852196
" y\n"
21862197
" Y-coordinate.\n"
21872198
" x\n"
21882199
" X-coordinate.\n"
21892200
" n\n"
2190-
" Maximal number of characters.\n"
2201+
" Maximal number of bytes.\n"
21912202
"\n"
2192-
"Return a string of characters, extracted from the window starting\n"
2193-
"at the current cursor position, or at y, x if specified, and\n"
2194-
"stopping at the end of the line. Attributes and color\n"
2195-
"information are stripped from the characters. If n is specified,\n"
2196-
"instr() returns a string at most n characters long (exclusive of\n"
2197-
"the trailing NUL).");
2203+
"Read from the current cursor position, or from y, x if specified, to\n"
2204+
"the end of the line, and return the text in the encoding of the\n"
2205+
"current locale, with attributes and color pairs stripped. At most n\n"
2206+
"bytes are read.");
21982207

21992208
static PyObject *
22002209
PyCursesWindow_instr(PyObject *op, PyObject *args)
@@ -5109,15 +5118,16 @@ _curses.unctrl
51095118
ch: object
51105119
/
51115120
5112-
Return a string which is a printable representation of the character ch.
5121+
Return a bytes object which is a printable representation of ch.
51135122
5114-
Control characters are displayed as a caret followed by the character,
5115-
for example as ^C. Printing characters are left as they are.
5123+
Control characters are displayed as a caret followed by the
5124+
character, for example as ^C. Printing characters are left as they
5125+
are. Any attributes and color pair are ignored.
51165126
[clinic start generated code]*/
51175127

51185128
static PyObject *
51195129
_curses_unctrl(PyObject *module, PyObject *ch)
5120-
/*[clinic end generated code: output=8e07fafc430c9434 input=cd1e35e16cd1ace4]*/
5130+
/*[clinic end generated code: output=8e07fafc430c9434 input=6732d59733d3ed5b]*/
51215131
{
51225132
chtype ch_;
51235133

0 commit comments

Comments
 (0)