Skip to content

Commit 2eb7ed5

Browse files
[3.13] gh-156234: Fix and rewrite the curses documentation on reading (GH-156235) (GH-156281)
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. The docstrings of getstr() and instr() said that n defaults to 2047, while the limit on this branch is 1023. (cherry picked from commit be87bfa)
1 parent 2786b69 commit 2eb7ed5

3 files changed

Lines changed: 149 additions & 80 deletions

File tree

Doc/library/curses.rst

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

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

3234
Whenever the documentation mentions a *character string* it can be specified
3335
as a Unicode string or a byte string.
@@ -484,8 +486,8 @@ The module :mod:`curses` defines the following functions:
484486
.. function:: putp(str)
485487

486488
Equivalent to ``tputs(str, 1, putchar)``; emit the value of a specified
487-
terminfo capability for the current terminal. Note that the output of :func:`putp`
488-
always goes to standard output.
489+
terminfo capability, a bytes object, for the current terminal.
490+
Note that the output of :func:`putp` always goes to standard output.
489491

490492
:func:`setupterm` (or :func:`initscr`) must be called first.
491493

@@ -656,7 +658,7 @@ The module :mod:`curses` defines the following functions:
656658
.. function:: tparm(str[, ...])
657659

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

@@ -677,7 +679,8 @@ The module :mod:`curses` defines the following functions:
677679

678680
.. function:: unctrl(ch)
679681

680-
Return a bytes object which is a printable representation of the character *ch*.
682+
Return a bytes object which is a printable representation of the character *ch*;
683+
any attributes and color pair are ignored.
681684
Control characters are represented as a caret followed by the character, for
682685
example as ``b'^C'``. Printing characters are left as they are.
683686

@@ -686,6 +689,9 @@ The module :mod:`curses` defines the following functions:
686689

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

692+
*ch* may be an integer (a key code or the code of an encoded byte), a byte,
693+
or a string of length 1 which encodes to a single byte.
694+
689695
.. note::
690696

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

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

712+
*ch* may be an integer (a character code, not a key code) or a string of
713+
length 1.
714+
706715
.. note::
707716

708717
Only one *ch* can be pushed before :meth:`!get_wch` is called.
@@ -988,27 +997,58 @@ Window objects
988997

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

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

9961023

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

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

10031043
.. versionadded:: 3.3
10041044

10051045

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

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

10131053

10141054
.. method:: window.getmaxyx()
@@ -1028,9 +1068,12 @@ Window objects
10281068
window.getstr(y, x)
10291069
window.getstr(y, x, n)
10301070

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

10351078

10361079
.. method:: window.getyx()
@@ -1128,11 +1171,11 @@ Window objects
11281171
.. method:: window.instr([n])
11291172
window.instr(y, x[, n])
11301173

1131-
Return a bytes object of characters, extracted from the window starting at the
1132-
current cursor position, or at *y*, *x* if specified, and stopping at the end
1133-
of the line. Attributes and color information are stripped
1134-
from the characters. If *n* is specified, :meth:`instr` returns a string
1135-
at most *n* characters long (exclusive of the trailing NUL).
1174+
Read the text of the window from the current cursor position,
1175+
or from *y*, *x* if specified, to the end of the line,
1176+
and return it as a bytes object, in the encoding of the current locale.
1177+
Attributes and color pairs are stripped.
1178+
At most *n* bytes are read; *n* defaults to and cannot exceed 1023.
11361179

11371180

11381181
.. method:: window.is_linetouched(line)
@@ -1153,6 +1196,8 @@ Window objects
11531196
If *flag* is ``True``, escape sequences generated by some keys (keypad, function keys)
11541197
will be interpreted by :mod:`curses`. If *flag* is ``False``, escape sequences will be
11551198
left as is in the input stream.
1199+
Keypad mode is disabled by default, but :func:`wrapper` enables it for the
1200+
main window.
11561201

11571202

11581203
.. method:: window.leaveok(flag)
@@ -1505,6 +1550,8 @@ by some methods.
15051550
| | color-pair field information |
15061551
+-------------------------+-------------------------------+
15071552

1553+
.. _curses-key-constants:
1554+
15081555
Keys are referred to by integer constants with names starting with ``KEY_``.
15091556
The exact keycaps available are system dependent.
15101557

Modules/_cursesmodule.c

Lines changed: 50 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,18 +1403,25 @@ _curses.window.getch
14031403
]
14041404
/
14051405
1406-
Get a character code from terminal keyboard.
1406+
Read a key press and return it as an integer.
14071407
1408-
The integer returned does not have to be in ASCII range: function
1409-
keys, keypad keys and so on return numbers higher than 256. In
1410-
no-delay mode, -1 is returned if there is no input, else getch()
1411-
waits until a key is pressed.
1408+
Wait until a key is pressed, or return -1 if the read is
1409+
non-blocking or times out.
1410+
1411+
An ordinary key is returned as the code of a single byte of its
1412+
encoding in the current locale, so a character encoded with several
1413+
bytes takes several calls. Use get_wch() to read it as a single
1414+
character.
1415+
1416+
In keypad mode function keys and other special keys are returned as
1417+
one of the KEY_* constants, which cannot be mistaken for an ordinary
1418+
key. Otherwise their bytes are returned one at a time.
14121419
[clinic start generated code]*/
14131420

14141421
static PyObject *
14151422
_curses_window_getch_impl(PyCursesWindowObject *self, int group_right_1,
14161423
int y, int x)
1417-
/*[clinic end generated code: output=e1639e87d545e676 input=0dc5ff40e079787a]*/
1424+
/*[clinic end generated code: output=e1639e87d545e676 input=882ddab9b41afbbd]*/
14181425
{
14191426
int rtn;
14201427

@@ -1451,18 +1458,18 @@ _curses.window.getkey
14511458
]
14521459
/
14531460
1454-
Get a character (string) from terminal keyboard.
1461+
Read a key press and return it as a str.
14551462
1456-
Returning a string instead of an integer, as getch() does. Function
1457-
keys, keypad keys and other special keys return a multibyte string
1458-
containing the key name. In no-delay mode, an exception is raised
1459-
if there is no input.
1463+
Read as getch() does, but return an ordinary key as a one-character
1464+
string, the byte decoded as Latin-1, and a special key as its name,
1465+
such as 'KEY_UP'. Raise curses.error instead of returning -1 if
1466+
there is no input.
14601467
[clinic start generated code]*/
14611468

14621469
static PyObject *
14631470
_curses_window_getkey_impl(PyCursesWindowObject *self, int group_right_1,
14641471
int y, int x)
1465-
/*[clinic end generated code: output=8490a182db46b10f input=bd24a7da1ed9c73b]*/
1472+
/*[clinic end generated code: output=8490a182db46b10f input=f054cf034c69e879]*/
14661473
{
14671474
int rtn;
14681475

@@ -1509,16 +1516,20 @@ _curses.window.get_wch
15091516
]
15101517
/
15111518
1512-
Get a wide character from terminal keyboard.
1519+
Read a key press and return it as a one-character str.
1520+
1521+
Wait until a key is pressed, or raise curses.error if the read is
1522+
non-blocking or times out.
15131523
1514-
Return a character for most keys, or an integer for function keys,
1515-
keypad keys, and other special keys.
1524+
In keypad mode function keys and other special keys are returned as
1525+
one of the KEY_* constants, an integer. Otherwise their characters
1526+
are returned one at a time.
15161527
[clinic start generated code]*/
15171528

15181529
static PyObject *
15191530
_curses_window_get_wch_impl(PyCursesWindowObject *self, int group_right_1,
15201531
int y, int x)
1521-
/*[clinic end generated code: output=9f4f86e91fe50ef3 input=dd7e5367fb49dc48]*/
1532+
/*[clinic end generated code: output=9f4f86e91fe50ef3 input=77eb2da426ebe71f]*/
15221533
{
15231534
int ct;
15241535
wint_t rtn;
@@ -1557,10 +1568,10 @@ _curses.window.getstr
15571568
X-coordinate.
15581569
]
15591570
n: int = 1023
1560-
Maximal number of characters.
1571+
Maximal number of bytes.
15611572
/
15621573
1563-
Read a string from the user, with primitive line editing capacity.
1574+
Read a line of input and return it as a bytes object.
15641575
[-clinic start generated code]*/
15651576

15661577
static PyObject *
@@ -1815,15 +1826,15 @@ _curses.window.instr
18151826
X-coordinate.
18161827
]
18171828
n: int = 1023
1818-
Maximal number of characters.
1829+
Maximal number of bytes.
18191830
/
18201831
1821-
Return a string of characters, extracted from the window.
1832+
Return the text of the window as a bytes object.
18221833
1823-
Return a string of characters, extracted from the window starting at the
1824-
current cursor position, or at y, x if specified. Attributes are stripped
1825-
from the characters. If n is specified, instr() returns a string at most
1826-
n characters long (exclusive of the trailing NUL).
1834+
Read from the current cursor position, or from y, x if specified, to
1835+
the end of the line, and return the text in the encoding of the
1836+
current locale, with attributes and color pairs stripped. At most n
1837+
bytes are read.
18271838
[-clinic start generated code]*/
18281839
static PyObject *
18291840
PyCursesWindow_InStr(PyCursesWindowObject *self, PyObject *args)
@@ -2585,33 +2596,31 @@ PyDoc_STRVAR(_curses_window_chgat__doc__,
25852596
"window refresh.");
25862597

25872598
PyDoc_STRVAR(_curses_window_getstr__doc__,
2588-
"getstr([[y, x,] n=2047])\n"
2589-
"Read a string from the user, with primitive line editing capacity.\n"
2599+
"getstr([[y, x,] n=1023])\n"
2600+
"Read a line of input and return it as a bytes object.\n"
25902601
"\n"
25912602
" y\n"
25922603
" Y-coordinate.\n"
25932604
" x\n"
25942605
" X-coordinate.\n"
25952606
" n\n"
2596-
" Maximal number of characters.");
2607+
" Maximal number of bytes.");
25972608

25982609
PyDoc_STRVAR(_curses_window_instr__doc__,
2599-
"instr([y, x,] n=2047)\n"
2600-
"Return a string of characters, extracted from the window.\n"
2610+
"instr([y, x,] n=1023)\n"
2611+
"Return the text of the window as a bytes object.\n"
26012612
"\n"
26022613
" y\n"
26032614
" Y-coordinate.\n"
26042615
" x\n"
26052616
" X-coordinate.\n"
26062617
" n\n"
2607-
" Maximal number of characters.\n"
2618+
" Maximal number of bytes.\n"
26082619
"\n"
2609-
"Return a string of characters, extracted from the window starting\n"
2610-
"at the current cursor position, or at y, x if specified, and\n"
2611-
"stopping at the end of the line. Attributes and color\n"
2612-
"information are stripped from the characters. If n is specified,\n"
2613-
"instr() returns a string at most n characters long (exclusive of\n"
2614-
"the trailing NUL).");
2620+
"Read from the current cursor position, or from y, x if specified, to\n"
2621+
"the end of the line, and return the text in the encoding of the\n"
2622+
"current locale, with attributes and color pairs stripped. At most n\n"
2623+
"bytes are read.");
26152624

26162625
static PyMethodDef PyCursesWindow_Methods[] = {
26172626
_CURSES_WINDOW_ADDCH_METHODDEF
@@ -4656,15 +4665,16 @@ _curses.unctrl
46564665
ch: object
46574666
/
46584667
4659-
Return a string which is a printable representation of the character ch.
4668+
Return a bytes object which is a printable representation of ch.
46604669
4661-
Control characters are displayed as a caret followed by the character,
4662-
for example as ^C. Printing characters are left as they are.
4670+
Control characters are displayed as a caret followed by the
4671+
character, for example as ^C. Printing characters are left as they
4672+
are. Any attributes and color pair are ignored.
46634673
[clinic start generated code]*/
46644674

46654675
static PyObject *
46664676
_curses_unctrl(PyObject *module, PyObject *ch)
4667-
/*[clinic end generated code: output=8e07fafc430c9434 input=cd1e35e16cd1ace4]*/
4677+
/*[clinic end generated code: output=8e07fafc430c9434 input=6732d59733d3ed5b]*/
46684678
{
46694679
chtype ch_;
46704680

0 commit comments

Comments
 (0)