Skip to content

Commit 996300c

Browse files
[3.15] gh-156347: Fix wrong statements in the curses documentation (GH-156354) (#156872)
window.encoding does not encode the string arguments on a build with wide-character support: the curses library converts the characters itself. In the HOWTO: getch() returns -1, not curses.ERR, when there is no input, and half-delay mode does the same as no-delay mode. getkey() returns the key name only for a special key. leaveok() is not a synonym for curs_set(). getstr() returns a bytes object, interprets the erase and kill characters, and limits bytes. The ACS_* constants are not all larger than 255. Also document get_wch() before getch(), and read whole characters in the example. (cherry picked from commit 161054c)
1 parent 6a1b26a commit 996300c

2 files changed

Lines changed: 46 additions & 33 deletions

File tree

Doc/howto/curses.rst

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -297,15 +297,18 @@ the next subsection.
297297

298298
The :meth:`~curses.window.addstr` method takes a Python string or
299299
bytestring as the value to be displayed. The contents of bytestrings
300-
are sent to the terminal as-is. Strings are encoded to bytes using
301-
the value of the window's :attr:`~window.encoding` attribute; this defaults to
302-
the default system encoding as returned by :func:`locale.getencoding`.
300+
are sent to the terminal as-is.
301+
On a build without wide-character support strings are encoded
302+
using the value of the window's :attr:`~window.encoding` attribute;
303+
this defaults to the default system encoding
304+
as returned by :func:`locale.getencoding`.
303305

304306
The :meth:`~curses.window.addch` methods take a character, which can be
305307
either a string of length 1, a bytestring of length 1, or an integer.
306308

307-
Constants are provided for extension characters; these constants are
308-
integers greater than 255. For example, :const:`ACS_PLMINUS` is a +/-
309+
Constants are provided for the characters of the terminal's alternate
310+
character set.
311+
For example, :const:`ACS_PLMINUS` is a +/-
309312
symbol, and :const:`ACS_ULCORNER` is the upper left corner of a box
310313
(handy for drawing borders). You can also use the appropriate Unicode
311314
character.
@@ -319,11 +322,11 @@ won't be distracting; it can be confusing to have the cursor blinking at some
319322
apparently random location.
320323

321324
If your application doesn't need a blinking cursor at all, you can
322-
call ``curs_set(False)`` to make it invisible. For compatibility
323-
with older curses versions, there's a ``leaveok(bool)`` function
324-
that's a synonym for :func:`~curses.curs_set`. When *bool* is true, the
325-
curses library will attempt to suppress the flashing cursor, and you
326-
won't need to worry about leaving it in odd locations.
325+
call ``curs_set(False)`` to make it invisible.
326+
The window method :meth:`~curses.window.leaveok` does something different:
327+
when its argument is true,
328+
curses leaves the cursor wherever the last update put it,
329+
instead of moving it back to the window's cursor position.
327330

328331

329332
Attributes and Color
@@ -429,40 +432,48 @@ The C curses library offers only very simple input mechanisms. Python's
429432
:mod:`curses` module adds a basic text-input widget. (Other libraries
430433
such as :pypi:`Urwid` have more extensive collections of widgets.)
431434

432-
There are two methods for getting input from a window:
435+
There are three methods for getting input from a window:
433436

434-
* :meth:`~curses.window.getch` refreshes the screen and then waits for
437+
* :meth:`~curses.window.get_wch` refreshes the screen and then waits for
435438
the user to hit a key, displaying the key if :func:`~curses.echo` has been
436439
called earlier. You can optionally specify a coordinate to which
437440
the cursor should be moved before pausing.
438441

439-
* :meth:`~curses.window.getkey` does the same thing but converts the
440-
integer to a string. Individual characters are returned as
441-
1-character strings, and special keys such as function keys return
442-
longer strings containing a key name such as ``KEY_UP`` or ``^G``.
442+
* :meth:`~curses.window.getch` does the same thing but returns the code of
443+
the key instead of a character.
444+
With ncurses this is a single byte of the key's encoding in the current
445+
locale, so a character encoded with several bytes takes several calls,
446+
one byte per call.
447+
448+
* :meth:`~curses.window.getkey` does the same as :meth:`!getch` but returns
449+
a string:
450+
an ordinary key as a 1-character string,
451+
and a special key as its name, such as ``KEY_UP``.
443452

444453
It's possible to not wait for the user using the
445454
:meth:`~curses.window.nodelay` window method. After ``nodelay(True)``,
446-
:meth:`!getch` and :meth:`!getkey` for the window become
447-
non-blocking. To signal that no input is ready, :meth:`!getch` returns
448-
``curses.ERR`` (a value of -1) and :meth:`!getkey` raises an exception.
455+
the reads for the window become non-blocking.
456+
To signal that no input is ready,
457+
:meth:`!get_wch` and :meth:`!getkey` raise an exception,
458+
and :meth:`!getch` returns ``-1``.
449459
There's also a :func:`~curses.halfdelay` function, which can be used to (in
450-
effect) set a timer on each :meth:`!getch`; if no input becomes
460+
effect) set a timer on each read; if no input becomes
451461
available within a specified delay (measured in tenths of a second),
452-
curses raises an exception.
462+
the read fails the same way.
453463

454-
The :meth:`!getch` method returns an integer; if it's between 0 and 255, it
455-
represents the ASCII code of the key pressed. Values greater than 255 are
456-
special keys such as Page Up, Home, or the cursor keys. You can compare the
457-
value returned to constants such as :const:`curses.KEY_PPAGE`,
464+
Special keys such as Page Up, Home, or the cursor keys are returned by all
465+
three as one of the :ref:`KEY_* constants <curses-key-constants>`,
466+
all larger than 255.
467+
You can compare the value returned to constants such as
468+
:const:`curses.KEY_PPAGE`,
458469
:const:`curses.KEY_HOME`, or :const:`curses.KEY_LEFT`. The main loop of
459470
your program may look something like this::
460471

461472
while True:
462-
c = stdscr.getch()
463-
if c == ord('p'):
473+
c = stdscr.get_wch()
474+
if c == 'p':
464475
PrintDocument()
465-
elif c == ord('q'):
476+
elif c == 'q':
466477
break # Exit the while loop
467478
elif c == curses.KEY_HOME:
468479
x = y = 0
@@ -474,15 +485,16 @@ conversion functions that take either integer or 1-character-string arguments
474485
and return the same type. For example, :func:`curses.ascii.ctrl` returns the
475486
control character corresponding to its argument.
476487

477-
There's also a method to retrieve an entire string,
488+
There's also a method to retrieve an entire line,
478489
:meth:`~curses.window.getstr`. It isn't used very often, because its
479490
functionality is quite limited; the only editing keys available are
480-
the backspace key and the Enter key, which terminates the string. It
481-
can optionally be limited to a fixed number of characters. ::
491+
the erase and kill characters, and the Enter key, which terminates the line.
492+
It returns a bytes object,
493+
and can optionally be limited to a fixed number of bytes. ::
482494

483495
curses.echo() # Enable echoing of characters
484496

485-
# Get a 15-character string, with the cursor on the top line
497+
# Get a line of at most 15 bytes, with the cursor on the top line
486498
s = stdscr.getstr(0,0, 15)
487499

488500
The :mod:`curses.textpad` module supplies a text box that supports an

Doc/library/curses.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,8 @@ Window objects
984984

985985
.. attribute:: window.encoding
986986

987-
Encoding used to encode method arguments (Unicode strings and characters).
987+
Encoding used to encode the string arguments of the methods and to decode
988+
their results on a build without wide-character support.
988989
The encoding attribute is inherited from the parent window when a subwindow
989990
is created, for example with :meth:`window.subwin`.
990991
By default, current locale encoding is used (see :func:`locale.getencoding`).

0 commit comments

Comments
 (0)