@@ -27,10 +27,24 @@ 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.
3335
36+ .. note ::
37+
38+ Whether curses may be used from several threads
39+ depends on the underlying library and how it was built.
40+ In many implementations, including the default build of ncurses,
41+ the screen state is shared and not thread-safe;
42+ since the blocking and refresh methods
43+ (such as :meth: `~window.getch ` and :meth: `~window.refresh `)
44+ release the :term: `GIL `,
45+ unsynchronized use from several threads can then crash the interpreter.
46+ Serialize the calls.
47+
3448.. seealso ::
3549
3650 Module :mod: `curses.ascii `
@@ -490,8 +504,8 @@ The module :mod:`!curses` defines the following functions:
490504.. function :: putp(str)
491505
492506 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.
507+ terminfo capability, a bytes object, for the current terminal.
508+ Note that the output of :func: ` putp ` always goes to standard output.
495509
496510 :func: `setupterm ` (or :func: `initscr `) must be called first.
497511
@@ -662,7 +676,7 @@ The module :mod:`!curses` defines the following functions:
662676.. function :: tparm(str[, ...])
663677
664678 Instantiate the bytes object *str * with the supplied parameters, where *str * should
665- be a parameterized string obtained from the terminfo database. For example,
679+ be a parameterized byte string obtained from the terminfo database. For example,
666680 ``tparm(tigetstr("cup"), 5, 3) `` could result in ``b'\033[6;4H' ``, the exact
667681 result depending on terminal type. Up to nine integer parameters may be supplied.
668682
@@ -683,7 +697,8 @@ The module :mod:`!curses` defines the following functions:
683697
684698.. function :: unctrl(ch)
685699
686- 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.
687702 Control characters are represented as a caret followed by the character, for
688703 example as ``b'^C' ``. Printing characters are left as they are.
689704
@@ -692,6 +707,9 @@ The module :mod:`!curses` defines the following functions:
692707
693708 Push *ch * so the next :meth: `~window.getch ` will return it.
694709
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+
695713 .. note ::
696714
697715 Only one *ch * can be pushed before :meth: `!getch ` is called.
@@ -709,6 +727,9 @@ The module :mod:`!curses` defines the following functions:
709727
710728 Push *ch * so the next :meth: `~window.get_wch ` will return it.
711729
730+ *ch * may be an integer (a character code, not a key code) or a string of
731+ length 1.
732+
712733 .. note ::
713734
714735 Only one *ch * can be pushed before :meth: `!get_wch ` is called.
@@ -989,27 +1010,58 @@ Window objects
9891010
9901011.. method :: window.getch([y, x])
9911012
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.
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.
9961035
9971036
9981037.. method :: window.get_wch([y, x])
9991038
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.
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.
10031055
10041056 .. versionadded :: 3.3
10051057
10061058
10071059.. method :: window.getkey([y, x])
10081060
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.
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.
10131065
10141066
10151067.. method :: window.getmaxyx()
@@ -1029,8 +1081,11 @@ Window objects
10291081 window.getstr(y, x)
10301082 window.getstr(y, x, n)
10311083
1032- Read a bytes object from the user, with primitive line editing capacity.
1033- 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;
10341089 *n * defaults to and cannot exceed 2047.
10351090
10361091 .. versionchanged :: 3.14
@@ -1132,12 +1187,11 @@ Window objects
11321187.. method :: window.instr([n])
11331188 window.instr(y, x[, n])
11341189
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.
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.
11411195
11421196 .. versionchanged :: 3.14
11431197 The maximum value for *n * was increased from 1023 to 2047.
@@ -1161,6 +1215,8 @@ Window objects
11611215 If *flag * is ``True ``, escape sequences generated by some keys (keypad, function keys)
11621216 will be interpreted by :mod: `!curses `. If *flag * is ``False ``, escape sequences will be
11631217 left as is in the input stream.
1218+ Keypad mode is disabled by default, but :func: `wrapper ` enables it for the
1219+ main window.
11641220
11651221
11661222.. method :: window.leaveok(flag)
@@ -1513,6 +1569,8 @@ by some methods.
15131569| | color-pair field information |
15141570+-------------------------+-------------------------------+
15151571
1572+ .. _curses-key-constants :
1573+
15161574Keys are referred to by integer constants with names starting with ``KEY_ ``.
15171575The exact keycaps available are system dependent.
15181576
0 commit comments