Skip to content

Commit 4f52e87

Browse files
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() inserted '¤' (U+00A4) instead of '€' for byte 0xA4 under ISO-8859-15. Decode the byte with btowc() and insert it as a wide character, like addch(). (cherry picked from commit 223cbff) Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent bc8af59 commit 4f52e87

3 files changed

Lines changed: 33 additions & 7 deletions

File tree

Lib/test/test_curses.py

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

340337
# The same characters supplied as a str. Unlike the int path above, a
341338
# 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: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2064,6 +2064,32 @@ _curses_window_insch_impl(PyCursesWindowObject *self, int group_left_1,
20642064
return NULL;
20652065

20662066
const char *funcname;
2067+
#ifdef HAVE_NCURSESW
2068+
/* winsch() does not locale-decode a byte above 127 on a wide build,
2069+
unlike waddch(), so decode it here and insert it as a wide character. */
2070+
chtype cch = ch_ & A_CHARTEXT;
2071+
if (cch > 127) {
2072+
wint_t wc = btowc((int)cch);
2073+
if (wc != WEOF) {
2074+
cchar_t wch;
2075+
wchar_t wstr[2] = { (wchar_t)wc, L'\0' };
2076+
attr_t cattr = (attr_t)((ch_ | (attr_t)attr) & ~(chtype)A_CHARTEXT);
2077+
if (setcchar(&wch, wstr, cattr, PAIR_NUMBER(cattr), NULL) == ERR) {
2078+
curses_window_set_error(self, "setcchar", "insch");
2079+
return NULL;
2080+
}
2081+
if (!group_left_1) {
2082+
rtn = wins_wch(self->win, &wch);
2083+
funcname = "wins_wch";
2084+
}
2085+
else {
2086+
rtn = mvwins_wch(self->win, y, x, &wch);
2087+
funcname = "mvwins_wch";
2088+
}
2089+
return curses_window_check_err(self, rtn, funcname, "insch");
2090+
}
2091+
}
2092+
#endif
20672093
if (!group_left_1) {
20682094
rtn = winsch(self->win, ch_ | (attr_t)attr);
20692095
funcname = "winsch";

0 commit comments

Comments
 (0)