Skip to content

Commit 61dffa7

Browse files
committed
gh-156207: Fix curses Textbox.gather() for double-width characters
gather() read the window one cell at a time, so the second cell of a double-width character was reported as another copy of it. Read each line with in_wchstr() instead: its count is a cell count, and it skips the continuation cell.
1 parent 999a046 commit 61dffa7

2 files changed

Lines changed: 18 additions & 4 deletions

File tree

Lib/curses/textpad.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,8 @@ def gather(self):
204204
stop = self._end_of_line(y)
205205
if stop == 0 and self.stripspaces:
206206
continue
207-
for x in range(self.maxx+1):
208-
if self.stripspaces and x > stop:
209-
break
210-
result = result + str(self.win.in_wch(y, x))
207+
count = stop+1 if self.stripspaces else self.maxx+1
208+
result = result + str(self.win.in_wchstr(y, 0, count))
211209
if self.maxy > 0:
212210
result = result + "\n"
213211
return result

Lib/test/test_curses.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2660,6 +2660,22 @@ def test_textbox_combining(self):
26602660
box.do_command(ch)
26612661
self.assertEqual(box.gather(), text + ' ')
26622662

2663+
@requires_wide_build
2664+
def test_textbox_double_width(self):
2665+
# A double-width (East Asian) character occupies two cells. gather()
2666+
# reads a whole line at a time so that the second cell, which holds
2667+
# the same character, is not reported as another one.
2668+
text = '你好'
2669+
if self._encodable(text):
2670+
box, win = self._make_textbox(1, 12)
2671+
for ch in text:
2672+
box.do_command(ch)
2673+
self.assertEqual(box.gather(), text + ' ')
2674+
box, win = self._make_textbox(1, 12, stripspaces=0)
2675+
for ch in text:
2676+
box.do_command(ch)
2677+
self.assertEqual(box.gather(), text + ' ' * 8)
2678+
26632679
def test_textbox_edit_wide(self):
26642680
# edit() reads characters through get_wch(). Each character is pushed
26652681
# with unget_wch(), which on a narrow build requires it to encode to a

0 commit comments

Comments
 (0)