Skip to content

Commit a3084ff

Browse files
[3.14] gh-156347: Fix wrong statements in the curses documentation (GH-156354) (GH-156873)
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 54717b6 commit a3084ff

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
@@ -298,15 +298,18 @@ the next subsection.
298298

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

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

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

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

329332

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

433-
There are two methods for getting input from a window:
436+
There are three methods for getting input from a window:
434437

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

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

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

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

462473
while True:
463-
c = stdscr.getch()
464-
if c == ord('p'):
474+
c = stdscr.get_wch()
475+
if c == 'p':
465476
PrintDocument()
466-
elif c == ord('q'):
477+
elif c == 'q':
467478
break # Exit the while loop
468479
elif c == curses.KEY_HOME:
469480
x = y = 0
@@ -475,15 +486,16 @@ conversion functions that take either integer or 1-character-string arguments
475486
and return the same type. For example, :func:`curses.ascii.ctrl` returns the
476487
control character corresponding to its argument.
477488

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

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

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

489501
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
@@ -987,7 +987,8 @@ Window objects
987987

988988
.. attribute:: window.encoding
989989

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

0 commit comments

Comments
 (0)