Skip to content

Commit 56cfdb0

Browse files
[3.13] gh-153862: Fix curses window.inch() for non-ASCII characters on a wide build (GH-153863) (GH-154153)
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 fb9a98a commit 56cfdb0

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
@@ -509,10 +509,9 @@ def test_read_from_window(self):
509509
self.assertRaises(ValueError, stdscr.instr, -2)
510510
self.assertRaises(ValueError, stdscr.instr, 0, 2, -2)
511511
# A non-ASCII character of an 8-bit locale reads back as its encoded
512-
# byte (see _encodable for the set). instr() returns the locale bytes
513-
# for any single-byte character; inch() packs the text into a chtype, so
514-
# on a wide build it only round-trips a Latin-1 codepoint (byte ==
515-
# codepoint).
512+
# byte (see _encodable for the set). Both instr() and inch() return the
513+
# locale byte for any character that fits the locale's single-byte
514+
# encoding.
516515
encoding = stdscr.encoding
517516
for ch in ('A', 'é', '¤', '€', 'є'):
518517
try:
@@ -524,8 +523,7 @@ def test_read_from_window(self):
524523
with self.subTest(ch=ch):
525524
stdscr.addstr(2, 0, ch)
526525
self.assertEqual(stdscr.instr(2, 0, 1), b)
527-
if ord(ch) < 0x100:
528-
self.assertEqual(stdscr.inch(2, 0) & curses.A_CHARTEXT, b[0])
526+
self.assertEqual(stdscr.inch(2, 0) & curses.A_CHARTEXT, b[0])
529527

530528
def test_coordinate_errors(self):
531529
# 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
@@ -1735,6 +1735,34 @@ _curses_window_insch_impl(PyCursesWindowObject *self, int group_left_1,
17351735
return PyCursesCheckERR(rtn, "insch");
17361736
}
17371737

1738+
#ifdef HAVE_NCURSESW
1739+
/* winch() returns the low 8 bits of the character's code point with no locale
1740+
conversion, unlike instr(), so recover the locale byte from the wide cell
1741+
when the character maps to exactly one byte, keeping the attribute and color
1742+
bits in RTN. A character with no single-byte form is left to winch(). */
1743+
static chtype
1744+
curses_cell_locale_byte(chtype rtn, const cchar_t *cell)
1745+
{
1746+
wchar_t wstr[CCHARW_MAX + 1];
1747+
attr_t attrs;
1748+
short pair;
1749+
/* getcchar() is not guaranteed to write the text of an empty cell. */
1750+
wstr[0] = L'\0';
1751+
if (getcchar(cell, wstr, &attrs, &pair, NULL) == ERR
1752+
|| wstr[0] == L'\0' || wstr[1] != L'\0')
1753+
{
1754+
return rtn;
1755+
}
1756+
/* wctob() mirrors ncurses' own _nc_to_char(): the single-byte form, or EOF
1757+
when the character has none in this locale. */
1758+
int byte = wctob(wstr[0]);
1759+
if (byte != EOF) {
1760+
rtn = (rtn & ~(chtype)A_CHARTEXT) | (unsigned char)byte;
1761+
}
1762+
return rtn;
1763+
}
1764+
#endif
1765+
17381766
/*[clinic input]
17391767
_curses.window.inch -> unsigned_long
17401768
@@ -1765,7 +1793,14 @@ _curses_window_inch_impl(PyCursesWindowObject *self, int group_right_1,
17651793
else {
17661794
rtn = mvwinch(self->win, y, x);
17671795
}
1768-
1796+
#ifdef HAVE_NCURSESW
1797+
cchar_t cell = {0};
1798+
if ((group_right_1 ? mvwin_wch(self->win, y, x, &cell)
1799+
: win_wch(self->win, &cell)) != ERR)
1800+
{
1801+
rtn = curses_cell_locale_byte(rtn, &cell);
1802+
}
1803+
#endif
17691804
return rtn;
17701805
}
17711806

0 commit comments

Comments
 (0)