Skip to content

Commit 9b7eb47

Browse files
[3.14] gh-156234: Fix and rewrite the curses documentation on reading (GH-156235) (GH-156280)
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 f2b4f3f commit 9b7eb47

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
@@ -30,6 +30,8 @@ Linux and the BSD variants of Unix.
3030

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

3436
Whenever the documentation mentions a *character string* it can be specified
3537
as a Unicode string or a byte string.
@@ -505,8 +507,8 @@ The module :mod:`!curses` defines the following functions:
505507
.. function:: putp(str)
506508

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

511513
:func:`setupterm` (or :func:`initscr`) must be called first.
512514

@@ -677,7 +679,7 @@ The module :mod:`!curses` defines the following functions:
677679
.. function:: tparm(str[, ...])
678680

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

@@ -698,7 +700,8 @@ The module :mod:`!curses` defines the following functions:
698700

699701
.. function:: unctrl(ch)
700702

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

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

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

713+
*ch* may be an integer (a key code or the code of an encoded byte), a byte,
714+
or a string of length 1 which encodes to a single byte.
715+
710716
.. note::
711717

712718
Only one *ch* can be pushed before :meth:`!getch` is called.
@@ -724,6 +730,9 @@ The module :mod:`!curses` defines the following functions:
724730

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

733+
*ch* may be an integer (a character code, not a key code) or a string of
734+
length 1.
735+
727736
.. note::
728737

729738
Only one *ch* can be pushed before :meth:`!get_wch` is called.
@@ -1004,27 +1013,58 @@ Window objects
10041013

10051014
.. method:: window.getch([y, x])
10061015

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

10121039

10131040
.. method:: window.get_wch([y, x])
10141041

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

10191059
.. versionadded:: 3.3
10201060

10211061

10221062
.. method:: window.getkey([y, x])
10231063

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

10291069

10301070
.. method:: window.getmaxyx()
@@ -1044,8 +1084,11 @@ Window objects
10441084
window.getstr(y, x)
10451085
window.getstr(y, x, n)
10461086

1047-
Read a bytes object from the user, with primitive line editing capacity.
1048-
At most *n* characters are read;
1087+
Read a line of input from the user, with primitive line editing capacity,
1088+
after moving the cursor to *y*, *x* if specified.
1089+
Return it as a bytes object, in the encoding of the current locale
1090+
and without the terminating newline.
1091+
At most *n* bytes are read;
10491092
*n* defaults to and cannot exceed 2047.
10501093

10511094
.. versionchanged:: 3.14
@@ -1147,12 +1190,11 @@ Window objects
11471190
.. method:: window.instr([n])
11481191
window.instr(y, x[, n])
11491192

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

11571199
.. versionchanged:: 3.14
11581200
The maximum value for *n* was increased from 1023 to 2047.
@@ -1176,6 +1218,8 @@ Window objects
11761218
If *flag* is ``True``, escape sequences generated by some keys (keypad, function keys)
11771219
will be interpreted by :mod:`!curses`. If *flag* is ``False``, escape sequences will be
11781220
left as is in the input stream.
1221+
Keypad mode is disabled by default, but :func:`wrapper` enables it for the
1222+
main window.
11791223

11801224

11811225
.. method:: window.leaveok(flag)
@@ -1528,6 +1572,8 @@ by some methods.
15281572
| | color-pair field information |
15291573
+-------------------------+-------------------------------+
15301574

1575+
.. _curses-key-constants:
1576+
15311577
Keys are referred to by integer constants with names starting with ``KEY_``.
15321578
The exact keycaps available are system dependent.
15331579

Modules/_cursesmodule.c

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1547,18 +1547,25 @@ _curses.window.getch
15471547
]
15481548
/
15491549
1550-
Get a character code from terminal keyboard.
1550+
Read a key press and return it as an integer.
15511551
1552-
The integer returned does not have to be in ASCII range: function
1553-
keys, keypad keys and so on return numbers higher than 256. In
1554-
no-delay mode, -1 is returned if there is no input, else getch()
1555-
waits until a key is pressed.
1552+
Wait until a key is pressed, or return -1 if the read is
1553+
non-blocking or times out.
1554+
1555+
An ordinary key is returned as the code of a single byte of its
1556+
encoding in the current locale, so a character encoded with several
1557+
bytes takes several calls. Use get_wch() to read it as a single
1558+
character.
1559+
1560+
In keypad mode function keys and other special keys are returned as
1561+
one of the KEY_* constants, which cannot be mistaken for an ordinary
1562+
key. Otherwise their bytes are returned one at a time.
15561563
[clinic start generated code]*/
15571564

15581565
static PyObject *
15591566
_curses_window_getch_impl(PyCursesWindowObject *self, int group_right_1,
15601567
int y, int x)
1561-
/*[clinic end generated code: output=e1639e87d545e676 input=0dc5ff40e079787a]*/
1568+
/*[clinic end generated code: output=e1639e87d545e676 input=882ddab9b41afbbd]*/
15621569
{
15631570
int rtn;
15641571

@@ -1595,18 +1602,18 @@ _curses.window.getkey
15951602
]
15961603
/
15971604
1598-
Get a character (string) from terminal keyboard.
1605+
Read a key press and return it as a str.
15991606
1600-
Returning a string instead of an integer, as getch() does. Function
1601-
keys, keypad keys and other special keys return a multibyte string
1602-
containing the key name. In no-delay mode, an exception is raised
1603-
if there is no input.
1607+
Read as getch() does, but return an ordinary key as a one-character
1608+
string, the byte decoded as Latin-1, and a special key as its name,
1609+
such as 'KEY_UP'. Raise curses.error instead of returning -1 if
1610+
there is no input.
16041611
[clinic start generated code]*/
16051612

16061613
static PyObject *
16071614
_curses_window_getkey_impl(PyCursesWindowObject *self, int group_right_1,
16081615
int y, int x)
1609-
/*[clinic end generated code: output=8490a182db46b10f input=bd24a7da1ed9c73b]*/
1616+
/*[clinic end generated code: output=8490a182db46b10f input=f054cf034c69e879]*/
16101617
{
16111618
int rtn;
16121619

@@ -1655,16 +1662,20 @@ _curses.window.get_wch
16551662
]
16561663
/
16571664
1658-
Get a wide character from terminal keyboard.
1665+
Read a key press and return it as a one-character str.
1666+
1667+
Wait until a key is pressed, or raise curses.error if the read is
1668+
non-blocking or times out.
16591669
1660-
Return a character for most keys, or an integer for function keys,
1661-
keypad keys, and other special keys.
1670+
In keypad mode function keys and other special keys are returned as
1671+
one of the KEY_* constants, an integer. Otherwise their characters
1672+
are returned one at a time.
16621673
[clinic start generated code]*/
16631674

16641675
static PyObject *
16651676
_curses_window_get_wch_impl(PyCursesWindowObject *self, int group_right_1,
16661677
int y, int x)
1667-
/*[clinic end generated code: output=9f4f86e91fe50ef3 input=dd7e5367fb49dc48]*/
1678+
/*[clinic end generated code: output=9f4f86e91fe50ef3 input=77eb2da426ebe71f]*/
16681679
{
16691680
int ct;
16701681
wint_t rtn;
@@ -1736,14 +1747,14 @@ curses_clinic_parse_optional_xy_n(PyObject *args,
17361747

17371748
PyDoc_STRVAR(_curses_window_getstr__doc__,
17381749
"getstr([[y, x,] n=2047])\n"
1739-
"Read a string from the user, with primitive line editing capacity.\n"
1750+
"Read a line of input and return it as a bytes object.\n"
17401751
"\n"
17411752
" y\n"
17421753
" Y-coordinate.\n"
17431754
" x\n"
17441755
" X-coordinate.\n"
17451756
" n\n"
1746-
" Maximal number of characters.");
1757+
" Maximal number of bytes.");
17471758

17481759
static PyObject *
17491760
PyCursesWindow_getstr(PyObject *op, PyObject *args)
@@ -1974,21 +1985,19 @@ _curses_window_inch_impl(PyCursesWindowObject *self, int group_right_1,
19741985

19751986
PyDoc_STRVAR(_curses_window_instr__doc__,
19761987
"instr([y, x,] n=2047)\n"
1977-
"Return a string of characters, extracted from the window.\n"
1988+
"Return the text of the window as a bytes object.\n"
19781989
"\n"
19791990
" y\n"
19801991
" Y-coordinate.\n"
19811992
" x\n"
19821993
" X-coordinate.\n"
19831994
" n\n"
1984-
" Maximal number of characters.\n"
1995+
" Maximal number of bytes.\n"
19851996
"\n"
1986-
"Return a string of characters, extracted from the window starting\n"
1987-
"at the current cursor position, or at y, x if specified, and\n"
1988-
"stopping at the end of the line. Attributes and color\n"
1989-
"information are stripped from the characters. If n is specified,\n"
1990-
"instr() returns a string at most n characters long (exclusive of\n"
1991-
"the trailing NUL).");
1997+
"Read from the current cursor position, or from y, x if specified, to\n"
1998+
"the end of the line, and return the text in the encoding of the\n"
1999+
"current locale, with attributes and color pairs stripped. At most n\n"
2000+
"bytes are read.");
19922001

19932002
static PyObject *
19942003
PyCursesWindow_instr(PyObject *op, PyObject *args)
@@ -4802,15 +4811,16 @@ _curses.unctrl
48024811
ch: object
48034812
/
48044813
4805-
Return a string which is a printable representation of the character ch.
4814+
Return a bytes object which is a printable representation of ch.
48064815
4807-
Control characters are displayed as a caret followed by the character,
4808-
for example as ^C. Printing characters are left as they are.
4816+
Control characters are displayed as a caret followed by the
4817+
character, for example as ^C. Printing characters are left as they
4818+
are. Any attributes and color pair are ignored.
48094819
[clinic start generated code]*/
48104820

48114821
static PyObject *
48124822
_curses_unctrl(PyObject *module, PyObject *ch)
4813-
/*[clinic end generated code: output=8e07fafc430c9434 input=cd1e35e16cd1ace4]*/
4823+
/*[clinic end generated code: output=8e07fafc430c9434 input=6732d59733d3ed5b]*/
48144824
{
48154825
chtype ch_;
48164826

0 commit comments

Comments
 (0)