Skip to content

Commit 683b515

Browse files
[3.15] gh-156234: Fix and rewrite the curses documentation on reading (GH-156235)
Fix wrong types: instr() and getstr() return a bytes object, not a str, and their n limits the number of bytes; getkey() returns a str; unctrl() returns a bytes object. Make clear whether an integer standing for a character is an encoded byte or a character code. Rewrite the documentation of getch(), get_wch(), getkey(), getstr() and instr(), following X/Open Curses. (cherry picked from commit be87bfa)
1 parent e839679 commit 683b515

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.
@@ -490,8 +492,8 @@ The module :mod:`!curses` defines the following functions:
490492
.. function:: putp(str)
491493

492494
Equivalent to ``tputs(str, 1, putchar)``; emit the value of a specified
493-
terminfo capability for the current terminal. Note that the output of :func:`putp`
494-
always goes to standard output.
495+
terminfo capability, a bytes object, for the current terminal.
496+
Note that the output of :func:`putp` always goes to standard output.
495497

496498
:func:`setupterm` (or :func:`initscr`) must be called first.
497499

@@ -662,7 +664,7 @@ The module :mod:`!curses` defines the following functions:
662664
.. function:: tparm(str[, ...])
663665

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

@@ -683,7 +685,8 @@ The module :mod:`!curses` defines the following functions:
683685

684686
.. function:: unctrl(ch)
685687

686-
Return a bytes object which is a printable representation of the character *ch*.
688+
Return a bytes object which is a printable representation of the character *ch*;
689+
any attributes and color pair are ignored.
687690
Control characters are represented as a caret followed by the character, for
688691
example as ``b'^C'``. Printing characters are left as they are.
689692

@@ -692,6 +695,9 @@ The module :mod:`!curses` defines the following functions:
692695

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

698+
*ch* may be an integer (a key code or the code of an encoded byte), a byte,
699+
or a string of length 1 which encodes to a single byte.
700+
695701
.. note::
696702

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

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

718+
*ch* may be an integer (a character code, not a key code) or a string of
719+
length 1.
720+
712721
.. note::
713722

714723
Only one *ch* can be pushed before :meth:`!get_wch` is called.
@@ -989,27 +998,58 @@ Window objects
989998

990999
.. method:: window.getch([y, x])
9911000

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

9971024

9981025
.. method:: window.get_wch([y, x])
9991026

1000-
Get a wide character. Return a character for most keys, or an integer for
1001-
function keys, keypad keys, and other special keys.
1002-
In no-delay mode, raise an exception if there is no input.
1027+
Read a key press, after moving the cursor to *y*, *x* if specified,
1028+
and return it as a one-character :class:`str`.
1029+
The window is refreshed first if it is not a pad and was modified since
1030+
the last refresh.
1031+
Wait until a key is pressed, or raise :exc:`error` if the read is
1032+
non-blocking or times out (see :meth:`nodelay` and :meth:`timeout`).
1033+
1034+
In keypad mode (see :meth:`keypad`) function keys and other special keys
1035+
are returned as one of the :ref:`KEY_* constants <curses-key-constants>`,
1036+
an integer.
1037+
Otherwise, or if their escape sequence does not arrive in time
1038+
(see :meth:`notimeout` and :func:`set_escdelay`),
1039+
their characters are returned one at a time.
1040+
1041+
In echo mode (see :func:`echo`) the key is added to the window as by
1042+
:meth:`addch`; special keys are not echoed.
10031043

10041044
.. versionadded:: 3.3
10051045

10061046

10071047
.. method:: window.getkey([y, x])
10081048

1009-
Get a character, returning a string instead of an integer, as :meth:`getch`
1010-
does. Function keys, keypad keys and other special keys return a multibyte
1011-
string containing the key name. In no-delay mode, raise an exception if
1012-
there is no input.
1049+
Read a key press as :meth:`getch` does, but return it as a :class:`str`:
1050+
an ordinary key as a one-character string, the byte decoded as Latin-1,
1051+
and a special key as its name, such as ``'KEY_UP'`` (see :func:`keyname`).
1052+
Raise :exc:`error` instead of returning ``-1`` if there is no input.
10131053

10141054

10151055
.. method:: window.getmaxyx()
@@ -1029,8 +1069,11 @@ Window objects
10291069
window.getstr(y, x)
10301070
window.getstr(y, x, n)
10311071

1032-
Read a bytes object from the user, with primitive line editing capacity.
1033-
At most *n* characters are read;
1072+
Read a line of input from the user, with primitive line editing capacity,
1073+
after moving the cursor to *y*, *x* if specified.
1074+
Return it as a bytes object, in the encoding of the current locale
1075+
and without the terminating newline.
1076+
At most *n* bytes are read;
10341077
*n* defaults to and cannot exceed 2047.
10351078

10361079
.. versionchanged:: 3.14
@@ -1132,12 +1175,11 @@ Window objects
11321175
.. method:: window.instr([n])
11331176
window.instr(y, x[, n])
11341177

1135-
Return a bytes object of characters, extracted from the window starting at the
1136-
current cursor position, or at *y*, *x* if specified, and stopping at the end
1137-
of the line. Attributes and color information are stripped
1138-
from the characters. If *n* is specified, :meth:`instr` returns a string
1139-
at most *n* characters long (exclusive of the trailing NUL).
1140-
The maximum value for *n* is 2047.
1178+
Read the text of the window from the current cursor position,
1179+
or from *y*, *x* if specified, to the end of the line,
1180+
and return it as a bytes object, in the encoding of the current locale.
1181+
Attributes and color pairs are stripped.
1182+
At most *n* bytes are read; *n* defaults to and cannot exceed 2047.
11411183

11421184
.. versionchanged:: 3.14
11431185
The maximum value for *n* was increased from 1023 to 2047.
@@ -1161,6 +1203,8 @@ Window objects
11611203
If *flag* is ``True``, escape sequences generated by some keys (keypad, function keys)
11621204
will be interpreted by :mod:`!curses`. If *flag* is ``False``, escape sequences will be
11631205
left as is in the input stream.
1206+
Keypad mode is disabled by default, but :func:`wrapper` enables it for the
1207+
main window.
11641208

11651209

11661210
.. method:: window.leaveok(flag)
@@ -1513,6 +1557,8 @@ by some methods.
15131557
| | color-pair field information |
15141558
+-------------------------+-------------------------------+
15151559

1560+
.. _curses-key-constants:
1561+
15161562
Keys are referred to by integer constants with names starting with ``KEY_``.
15171563
The exact keycaps available are system dependent.
15181564

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)