Skip to content

Commit 2aeb57f

Browse files
gh-156234: Fix and rewrite the curses documentation on reading
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(), get_wstr(), instr(), in_wstr() and in_wchstr(), following X/Open Curses.
1 parent f74cdf8 commit 2aeb57f

3 files changed

Lines changed: 226 additions & 130 deletions

File tree

Doc/library/curses.rst

Lines changed: 106 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,15 @@ 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`.
32+
Methods that write to a window accept also a character cell: a Unicode
33+
string of a spacing character followed by combining characters, or a
34+
:class:`complexchar`.
3035

3136
Whenever the documentation mentions a *character string* it can be specified
3237
as a Unicode string or a byte string.
38+
Methods that write to a window accept also a :class:`complexstr`.
3339

3440
.. note::
3541

@@ -364,9 +370,9 @@ Keyboard input
364370
Push *ch* so the next :meth:`~window.getch` or :meth:`~window.get_wch` will
365371
return it.
366372

367-
*ch* may be an integer (a key code or character code), a byte, or a string of
368-
length 1. A one-character string is pushed like :func:`unget_wch`; on a
369-
narrow build it must encode to a single byte.
373+
*ch* may be an integer (a key code or the code of an encoded byte), a byte,
374+
or a string of length 1. A one-character string is pushed like
375+
:func:`unget_wch`; on a narrow build it must encode to a single byte.
370376

371377
.. note::
372378

@@ -380,6 +386,9 @@ Keyboard input
380386

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

389+
*ch* may be an integer (a character code, not a key code) or a string of
390+
length 1.
391+
383392
.. note::
384393

385394
Only one *ch* can be pushed before :meth:`!get_wch` is called.
@@ -1015,7 +1024,7 @@ Terminfo database
10151024
.. function:: tparm(str[, ...])
10161025

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

@@ -1024,8 +1033,8 @@ Terminfo database
10241033
.. function:: putp(str)
10251034

10261035
Equivalent to ``tputs(str, 1, putchar)``; emit the value of a specified
1027-
terminfo capability for the current terminal. Note that the output of :func:`putp`
1028-
always goes to standard output.
1036+
terminfo capability, a bytes object, for the current terminal.
1037+
Note that the output of :func:`putp` always goes to standard output.
10291038

10301039
:func:`setupterm` (or :func:`initscr`) must be called first.
10311040

@@ -1035,9 +1044,15 @@ Utilities
10351044

10361045
.. function:: unctrl(ch)
10371046

1038-
Return a bytes object which is a printable representation of the character *ch*.
1039-
*ch* cannot be a character that does not fit in a single byte; use
1040-
:func:`wunctrl` for those.
1047+
Return a bytes object which is a printable representation of the character *ch*;
1048+
any attributes and color pair are ignored.
1049+
Control characters are represented as a caret followed by a character,
1050+
for example as ``b'^C'``.
1051+
Printing characters are left as they are.
1052+
The representation of other characters is defined by the underlying curses
1053+
library.
1054+
1055+
*ch* must fit in a single byte; use :func:`wunctrl` for other characters.
10411056

10421057
.. function:: wunctrl(ch)
10431058

@@ -1264,19 +1279,47 @@ Reading input
12641279

12651280
.. method:: window.getch([y, x])
12661281

1267-
Get a character. Note that the integer returned does *not* have to be in ASCII
1268-
range: function keys, keypad keys and so on are represented by numbers higher
1269-
than 255. In no-delay mode, return ``-1`` if there is no input, otherwise
1270-
wait until a key is pressed.
1271-
A multibyte character is returned as its encoded bytes one at a time; use
1272-
:meth:`get_wch` to read it as a single character.
1282+
Read a key press, after moving the cursor to *y*, *x* if specified,
1283+
and return it as an integer.
1284+
The window is refreshed first if it is not a pad and was modified since
1285+
the last refresh.
1286+
Wait until a key is pressed, or return ``-1`` if the read is non-blocking
1287+
or times out (see :meth:`nodelay` and :meth:`timeout`).
1288+
1289+
An ordinary key is returned as the code of a single byte of its encoding
1290+
in the current locale,
1291+
so a character encoded with several bytes takes several calls.
1292+
For example, in a UTF-8 locale ``'é'`` is read as ``195``, then ``169``.
1293+
Use :meth:`get_wch` to read it as a single character.
1294+
1295+
In keypad mode (see :meth:`keypad`) function keys and other special keys
1296+
are returned as one of the :ref:`KEY_* constants <curses-key-constants>`,
1297+
which cannot be mistaken for an ordinary key.
1298+
Otherwise, or if their escape sequence does not arrive in time
1299+
(see :meth:`notimeout` and :func:`set_escdelay`),
1300+
their bytes are returned one at a time.
1301+
1302+
In echo mode (see :func:`echo`) the key is added to the window as by
1303+
:meth:`addch`; special keys are not echoed.
12731304

12741305
.. method:: window.get_wch([y, x])
12751306

1276-
Get a wide character. Return a character for most keys, or an integer for
1277-
function keys, keypad keys, and other special keys. Unlike :meth:`getch`, an
1278-
ordinary key is returned as a one-character :class:`str`.
1279-
In no-delay mode, raise an exception if there is no input.
1307+
Read a key press, after moving the cursor to *y*, *x* if specified,
1308+
and return it as a one-character :class:`str`.
1309+
The window is refreshed first if it is not a pad and was modified since
1310+
the last refresh.
1311+
Wait until a key is pressed, or raise :exc:`error` if the read is
1312+
non-blocking or times out (see :meth:`nodelay` and :meth:`timeout`).
1313+
1314+
In keypad mode (see :meth:`keypad`) function keys and other special keys
1315+
are returned as one of the :ref:`KEY_* constants <curses-key-constants>`,
1316+
an integer.
1317+
Otherwise, or if their escape sequence does not arrive in time
1318+
(see :meth:`notimeout` and :func:`set_escdelay`),
1319+
their characters are returned one at a time.
1320+
1321+
In echo mode (see :func:`echo`) the key is added to the window as by
1322+
:meth:`addch`; special keys are not echoed.
12801323

12811324
.. versionadded:: 3.3
12821325

@@ -1286,21 +1329,24 @@ Reading input
12861329

12871330
.. method:: window.getkey([y, x])
12881331

1289-
Get a character, returning a string instead of an integer, as :meth:`getch`
1290-
does. Function keys, keypad keys and other special keys return a multibyte
1291-
string containing the key name. In no-delay mode, raise an exception if
1292-
there is no input.
1332+
Read a key press as :meth:`getch` does, but return it as a :class:`str`:
1333+
an ordinary key as a one-character string, the byte decoded as Latin-1,
1334+
and a special key as its name, such as ``'KEY_UP'`` (see :func:`keyname`).
1335+
Raise :exc:`error` instead of returning ``-1`` if there is no input.
12931336

12941337
.. method:: window.getstr()
12951338
window.getstr(n)
12961339
window.getstr(y, x)
12971340
window.getstr(y, x, n)
12981341

1299-
Read a bytes object from the user, with primitive line editing capacity.
1300-
At most *n* characters are read;
1342+
Read a line of input from the user, with primitive line editing capacity,
1343+
after moving the cursor to *y*, *x* if specified.
1344+
Return it as a bytes object, in the encoding of the current locale
1345+
and without the terminating newline.
1346+
At most *n* bytes are read;
13011347
*n* defaults to and cannot exceed 2047.
1302-
A multibyte character is returned as its encoded bytes; use :meth:`get_wstr`
1303-
to read the input as a :class:`str`.
1348+
1349+
Use :meth:`get_wstr` to read the input as a :class:`str`.
13041350

13051351
.. versionchanged:: 3.14
13061352
The maximum value for *n* was increased from 1023 to 2047.
@@ -1310,10 +1356,13 @@ Reading input
13101356
window.get_wstr(y, x)
13111357
window.get_wstr(y, x, n)
13121358

1313-
Read a string from the user, with primitive line editing capacity.
1314-
Unlike :meth:`getstr`, it can return characters that are not representable in
1315-
the window's encoding.
1316-
At most *n* characters are read; *n* defaults to and cannot exceed 2047.
1359+
Read a line of input from the user, with primitive line editing capacity,
1360+
after moving the cursor to *y*, *x* if specified.
1361+
Return it as a :class:`str`, without the terminating newline.
1362+
At most *n* characters are read;
1363+
*n* defaults to and cannot exceed 2047.
1364+
1365+
This is the wide-character variant of :meth:`getstr`.
13171366

13181367
.. versionadded:: next
13191368

@@ -1354,13 +1403,13 @@ Reading window contents
13541403
.. method:: window.instr([n])
13551404
window.instr(y, x[, n])
13561405

1357-
Return a bytes object of characters, extracted from the window starting at the
1358-
current cursor position, or at *y*, *x* if specified, and stopping at the end
1359-
of the line. Attributes and color information are stripped
1360-
from the characters. If *n* is specified, :meth:`instr` returns a string
1361-
at most *n* characters long (exclusive of the trailing NUL).
1362-
The maximum value for *n* is 2047.
1363-
A character not representable in the window's encoding cannot be returned;
1406+
Read the text of the window from the current cursor position,
1407+
or from *y*, *x* if specified, to the end of the line,
1408+
and return it as a bytes object, in the encoding of the current locale.
1409+
Attributes and color pairs are stripped;
1410+
use :meth:`in_wchstr` to read them too.
1411+
At most *n* bytes are read; *n* defaults to and cannot exceed 2047.
1412+
A character not representable in the encoding cannot be returned;
13641413
use :meth:`in_wstr` for those.
13651414

13661415
.. versionchanged:: 3.14
@@ -1369,26 +1418,27 @@ Reading window contents
13691418
.. method:: window.in_wstr([n])
13701419
window.in_wstr(y, x[, n])
13711420

1372-
Return a string of characters, extracted from the window starting at the
1373-
current cursor position, or at *y*, *x* if specified. Unlike :meth:`instr`,
1374-
it can return characters that are not representable in the window's encoding.
1375-
Attributes and color information are stripped from the characters. The
1376-
maximum value for *n* is 2047.
1421+
Read the text of the window from the current cursor position,
1422+
or from *y*, *x* if specified, to the end of the line,
1423+
and return it as a :class:`str`.
1424+
Attributes and color pairs are stripped;
1425+
use :meth:`in_wchstr` to read them too.
1426+
At most *n* characters are read; *n* defaults to and cannot exceed 2047.
1427+
1428+
This is the wide-character variant of :meth:`instr`.
13771429

13781430
.. versionadded:: next
13791431

13801432
.. method:: window.in_wchstr([n])
13811433
window.in_wchstr(y, x[, n])
13821434

1383-
Return a :class:`complexstr` of the styled cells extracted from the window
1384-
starting at the current cursor position, or at *y*, *x* if specified, and
1385-
stopping at the end of the line. This is the variant of :meth:`instr` and
1386-
:meth:`in_wstr` that *keeps* each cell's attributes and color pair (those
1387-
methods strip the rendition). If *n* is specified, at most *n* cells are
1388-
returned. The maximum value for *n* is 2047.
1389-
1390-
The result can be written back unchanged with :meth:`addstr` (a read and a
1391-
re-write is a round-trip that preserves every cell's rendition).
1435+
Read the styled cells of the window from the current cursor position,
1436+
or from *y*, *x* if specified, to the end of the line,
1437+
and return them as a :class:`complexstr`.
1438+
Unlike :meth:`instr` and :meth:`in_wstr`, each cell keeps its attributes
1439+
and color pair, so the result can be written back unchanged
1440+
with :meth:`addstr`.
1441+
At most *n* cells are read; *n* defaults to and cannot exceed 2047.
13921442

13931443
.. versionadded:: next
13941444

@@ -1835,6 +1885,8 @@ Input options
18351885
If *flag* is ``True``, escape sequences generated by some keys (keypad, function keys)
18361886
will be interpreted by :mod:`!curses`. If *flag* is ``False``, escape sequences will be
18371887
left as is in the input stream.
1888+
Keypad mode is disabled by default, but :func:`wrapper` enables it for the
1889+
main window.
18381890

18391891
.. method:: window.nodelay(flag)
18401892

@@ -2331,6 +2383,8 @@ by some methods.
23312383
| .. data:: A_COLOR | | Bit-mask to extract color-pair field information |
23322384
+-------------------------+--------------------------+--------------------------------------------------+
23332385

2386+
.. _curses-key-constants:
2387+
23342388
Keys
23352389
~~~~
23362390

0 commit comments

Comments
 (0)