Skip to content

Commit 346f82c

Browse files
[3.14] gh-153864: Fix curses window.insch() for non-ASCII characters on a wide build (GH-153865)
On a wide build, winsch() does not locale-decode a byte above 127, unlike waddch(), so insch()/mvinsch() stored the wrong character for a non-ASCII byte of an 8-bit locale ('€' 0xA4 under ISO-8859-15 became U+00A4 '¤'). Decode the byte with btowc() and insert it as a wide character, as addch() effectively does. (cherry picked from commit 223cbff) Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 2b807eb commit 346f82c

3 files changed

Lines changed: 28 additions & 7 deletions

File tree

Lib/test/test_curses.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -326,13 +326,10 @@ def test_output_character(self):
326326
stdscr.move(2, 0)
327327
stdscr.echochar(v)
328328
self.assertEqual(self._read_char(2, 0), c)
329-
# insch() round-trips a byte only where its code point equals
330-
# the byte value (Latin-1): on a wide build ncurses winsch
331-
# stores a printable byte directly as a code point instead of
332-
# decoding it through the locale.
333-
if ord(c) < 0x100:
334-
stdscr.insch(1, 0, v)
335-
self.assertEqual(self._read_char(1, 0), c)
329+
# insch() decodes the byte through the locale like addch(), so
330+
# it round-trips the same character.
331+
stdscr.insch(1, 0, v)
332+
self.assertEqual(self._read_char(1, 0), c)
336333

337334
# The same characters supplied as a str. Unlike the int path above, a
338335
# str is stored as a wide-character cell on a wide build, so every
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.insch` now inserts a
2+
non-ASCII byte as the character it encodes in the window's encoding,
3+
consistently with :meth:`~curses.window.addch`, instead of its code point.

Modules/_cursesmodule.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1871,6 +1871,27 @@ _curses_window_insch_impl(PyCursesWindowObject *self, int group_left_1,
18711871
if (!PyCurses_ConvertToChtype(self, ch, &ch_))
18721872
return NULL;
18731873

1874+
#ifdef HAVE_NCURSESW
1875+
/* winsch() does not locale-decode a byte above 127 on a wide build,
1876+
unlike waddch(), so decode it here and insert it as a wide character. */
1877+
chtype cch = ch_ & A_CHARTEXT;
1878+
if (cch > 127) {
1879+
wint_t wc = btowc((int)cch);
1880+
if (wc != WEOF) {
1881+
cchar_t wch;
1882+
wchar_t wstr[2] = { (wchar_t)wc, L'\0' };
1883+
attr_t cattr = (attr_t)((ch_ | (attr_t)attr) & ~(chtype)A_CHARTEXT);
1884+
setcchar(&wch, wstr, cattr, PAIR_NUMBER(cattr), NULL);
1885+
if (!group_left_1) {
1886+
rtn = wins_wch(self->win, &wch);
1887+
}
1888+
else {
1889+
rtn = mvwins_wch(self->win, y, x, &wch);
1890+
}
1891+
return PyCursesCheckERR_ForWin(self, rtn, "insch");
1892+
}
1893+
}
1894+
#endif
18741895
if (!group_left_1) {
18751896
rtn = winsch(self->win, ch_ | (attr_t)attr);
18761897
}

0 commit comments

Comments
 (0)