Skip to content

Commit b7ef886

Browse files
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 bc8af59 commit b7ef886

4 files changed

Lines changed: 45 additions & 6 deletions

File tree

Doc/library/curses.rst

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

10831085

10841086
.. 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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2076,6 +2076,34 @@ _curses_window_insch_impl(PyCursesWindowObject *self, int group_left_1,
20762076
return curses_window_check_err(self, rtn, funcname, "insch");
20772077
}
20782078

2079+
#ifdef HAVE_NCURSESW
2080+
/* winch() returns the low 8 bits of the character's code point with no locale
2081+
conversion, unlike instr(), so recover the locale byte from the wide cell
2082+
when the character maps to exactly one byte, keeping the attribute and color
2083+
bits in RTN. A character with no single-byte form is left to winch(). */
2084+
static chtype
2085+
curses_cell_locale_byte(chtype rtn, const cchar_t *cell)
2086+
{
2087+
wchar_t wstr[CCHARW_MAX + 1];
2088+
attr_t attrs;
2089+
short pair;
2090+
/* getcchar() is not guaranteed to write the text of an empty cell. */
2091+
wstr[0] = L'\0';
2092+
if (getcchar(cell, wstr, &attrs, &pair, NULL) == ERR
2093+
|| wstr[0] == L'\0' || wstr[1] != L'\0')
2094+
{
2095+
return rtn;
2096+
}
2097+
/* wctob() mirrors ncurses' own _nc_to_char(): the single-byte form, or EOF
2098+
when the character has none in this locale. */
2099+
int byte = wctob(wstr[0]);
2100+
if (byte != EOF) {
2101+
rtn = (rtn & ~(chtype)A_CHARTEXT) | (unsigned char)byte;
2102+
}
2103+
return rtn;
2104+
}
2105+
#endif
2106+
20792107
/*[clinic input]
20802108
_curses.window.inch
20812109
@@ -2113,6 +2141,14 @@ _curses_window_inch_impl(PyCursesWindowObject *self, int group_right_1,
21132141
curses_window_set_error(self, funcname, "inch");
21142142
return NULL;
21152143
}
2144+
#ifdef HAVE_NCURSESW
2145+
cchar_t cell = {0};
2146+
if ((group_right_1 ? mvwin_wch(self->win, y, x, &cell)
2147+
: win_wch(self->win, &cell)) != ERR)
2148+
{
2149+
rtn = curses_cell_locale_byte(rtn, &cell);
2150+
}
2151+
#endif
21162152
return PyLong_FromUnsignedLong(rtn);
21172153
}
21182154

0 commit comments

Comments
 (0)