Skip to content

Commit efeb32e

Browse files
[3.13] gh-153862: Fix curses window.inch() for non-ASCII characters on a wide build (GH-153863)
On a wide build, winch() returns the low 8 bits of the character's code point with no locale conversion, so inch()/mvinch() disagreed with instr() for a non-ASCII character of an 8-bit locale ('€' under ISO-8859-15 gave 0xAC instead of 0xA4). Re-encode the cell to its locale byte via wctob(), as ncurses does for getbkgd(). (cherry picked from commit a83aaa9) Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 303d0ac commit efeb32e

4 files changed

Lines changed: 45 additions & 7 deletions

File tree

Doc/library/curses.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,6 +1063,8 @@ Window objects
10631063
The bottom 8 bits are the character proper and the upper bits are the attributes;
10641064
extract them with the :data:`A_CHARTEXT` and :data:`A_ATTRIBUTES` bit-masks,
10651065
and the color pair with :func:`pair_number`.
1066+
The character byte is the locale-encoded byte of the cell's character,
1067+
consistent with :meth:`instr`.
10661068

10671069

10681070
.. method:: window.insch(ch[, attr])

Lib/test/test_curses.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -512,10 +512,9 @@ def test_read_from_window(self):
512512
self.assertRaises(ValueError, stdscr.instr, -2)
513513
self.assertRaises(ValueError, stdscr.instr, 0, 2, -2)
514514
# A non-ASCII character of an 8-bit locale reads back as its encoded
515-
# byte (see _encodable for the set). instr() returns the locale bytes
516-
# for any single-byte character; inch() packs the text into a chtype, so
517-
# on a wide build it only round-trips a Latin-1 codepoint (byte ==
518-
# codepoint).
515+
# byte (see _encodable for the set). Both instr() and inch() return the
516+
# locale byte for any character that fits the locale's single-byte
517+
# encoding.
519518
encoding = stdscr.encoding
520519
for ch in ('A', 'é', '¤', '€', 'є'):
521520
try:
@@ -527,8 +526,7 @@ def test_read_from_window(self):
527526
with self.subTest(ch=ch):
528527
stdscr.addstr(2, 0, ch)
529528
self.assertEqual(stdscr.instr(2, 0, 1), b)
530-
if ord(ch) < 0x100:
531-
self.assertEqual(stdscr.inch(2, 0) & curses.A_CHARTEXT, b[0])
529+
self.assertEqual(stdscr.inch(2, 0) & curses.A_CHARTEXT, b[0])
532530

533531
def test_coordinate_errors(self):
534532
# Addressing a cell outside the window raises curses.error.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
On a wide :mod:`curses` build, :meth:`curses.window.inch` now returns the
2+
locale-encoded byte of a non-ASCII character, matching
3+
:meth:`~curses.window.instr`, instead of the low byte of its code point.

Modules/_cursesmodule.c

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1714,6 +1714,34 @@ _curses_window_insch_impl(PyCursesWindowObject *self, int group_left_1,
17141714
return PyCursesCheckERR(rtn, "insch");
17151715
}
17161716

1717+
#ifdef HAVE_NCURSESW
1718+
/* winch() returns the low 8 bits of the character's code point with no locale
1719+
conversion, unlike instr(), so recover the locale byte from the wide cell
1720+
when the character maps to exactly one byte, keeping the attribute and color
1721+
bits in RTN. A character with no single-byte form is left to winch(). */
1722+
static chtype
1723+
curses_cell_locale_byte(chtype rtn, const cchar_t *cell)
1724+
{
1725+
wchar_t wstr[CCHARW_MAX + 1];
1726+
attr_t attrs;
1727+
short pair;
1728+
/* getcchar() is not guaranteed to write the text of an empty cell. */
1729+
wstr[0] = L'\0';
1730+
if (getcchar(cell, wstr, &attrs, &pair, NULL) == ERR
1731+
|| wstr[0] == L'\0' || wstr[1] != L'\0')
1732+
{
1733+
return rtn;
1734+
}
1735+
/* wctob() mirrors ncurses' own _nc_to_char(): the single-byte form, or EOF
1736+
when the character has none in this locale. */
1737+
int byte = wctob(wstr[0]);
1738+
if (byte != EOF) {
1739+
rtn = (rtn & ~(chtype)A_CHARTEXT) | (unsigned char)byte;
1740+
}
1741+
return rtn;
1742+
}
1743+
#endif
1744+
17171745
/*[clinic input]
17181746
_curses.window.inch -> unsigned_long
17191747
@@ -1744,7 +1772,14 @@ _curses_window_inch_impl(PyCursesWindowObject *self, int group_right_1,
17441772
else {
17451773
rtn = mvwinch(self->win, y, x);
17461774
}
1747-
1775+
#ifdef HAVE_NCURSESW
1776+
cchar_t cell = {0};
1777+
if ((group_right_1 ? mvwin_wch(self->win, y, x, &cell)
1778+
: win_wch(self->win, &cell)) != ERR)
1779+
{
1780+
rtn = curses_cell_locale_byte(rtn, &cell);
1781+
}
1782+
#endif
17481783
return rtn;
17491784
}
17501785

0 commit comments

Comments
 (0)