X Tutup
Skip to content

Commit bdf9e0e

Browse files
Issue python#13051: Fixed recursion errors in large or resized curses.textpad.Textbox.
Based on patch by Tycho Andersen.
1 parent 283de2b commit bdf9e0e

File tree

4 files changed

+35
-9
lines changed

4 files changed

+35
-9
lines changed

Lib/curses/textpad.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,20 @@ class Textbox:
4343
def __init__(self, win, insert_mode=False):
4444
self.win = win
4545
self.insert_mode = insert_mode
46-
(self.maxy, self.maxx) = win.getmaxyx()
47-
self.maxy = self.maxy - 1
48-
self.maxx = self.maxx - 1
46+
self._update_max_yx()
4947
self.stripspaces = 1
5048
self.lastcmd = None
5149
win.keypad(1)
5250

51+
def _update_max_yx(self):
52+
maxy, maxx = self.win.getmaxyx()
53+
self.maxy = maxy - 1
54+
self.maxx = maxx - 1
55+
5356
def _end_of_line(self, y):
5457
"""Go to the location of the first blank on the given line,
5558
returning the index of the last non-blank character."""
59+
self._update_max_yx()
5660
last = self.maxx
5761
while True:
5862
if curses.ascii.ascii(self.win.inch(y, last)) != curses.ascii.SP:
@@ -64,8 +68,10 @@ def _end_of_line(self, y):
6468
return last
6569

6670
def _insert_printable_char(self, ch):
71+
self._update_max_yx()
6772
(y, x) = self.win.getyx()
68-
if y < self.maxy or x < self.maxx:
73+
backyx = None
74+
while y < self.maxy or x < self.maxx:
6975
if self.insert_mode:
7076
oldch = self.win.inch()
7177
# The try-catch ignores the error we trigger from some curses
@@ -75,14 +81,20 @@ def _insert_printable_char(self, ch):
7581
self.win.addch(ch)
7682
except curses.error:
7783
pass
78-
if self.insert_mode:
79-
(backy, backx) = self.win.getyx()
80-
if curses.ascii.isprint(oldch):
81-
self._insert_printable_char(oldch)
82-
self.win.move(backy, backx)
84+
if not self.insert_mode or not curses.ascii.isprint(oldch):
85+
break
86+
ch = oldch
87+
(y, x) = self.win.getyx()
88+
# Remember where to put the cursor back since we are in insert_mode
89+
if backyx is None:
90+
backyx = y, x
91+
92+
if backyx is not None:
93+
self.win.move(*backyx)
8394

8495
def do_command(self, ch):
8596
"Process a single editing command."
97+
self._update_max_yx()
8698
(y, x) = self.win.getyx()
8799
self.lastcmd = ch
88100
if curses.ascii.isprint(ch):
@@ -148,6 +160,7 @@ def do_command(self, ch):
148160
def gather(self):
149161
"Collect and return the contents of the window."
150162
result = ""
163+
self._update_max_yx()
151164
for y in range(self.maxy+1):
152165
self.win.move(y, 0)
153166
stop = self._end_of_line(y)

Lib/test/test_curses.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
curses = import_module('curses')
2828
import_module('curses.panel')
2929
import_module('curses.ascii')
30+
import_module('curses.textpad')
3031

3132
def requires_curses_func(name):
3233
return unittest.skipUnless(hasattr(curses, name),
@@ -392,6 +393,14 @@ def test_issue21088(self):
392393
human_readable_signature = stdscr.addch.__doc__.split("\n")[0]
393394
self.assertIn("[y, x,]", human_readable_signature)
394395

396+
def test_issue13051(self):
397+
stdscr = self.stdscr
398+
box = curses.textpad.Textbox(stdscr, insert_mode=True)
399+
lines, cols = stdscr.getmaxyx()
400+
stdscr.resize(lines-2, cols-2)
401+
# this may cause infinite recursion, leading to a RuntimeError
402+
box._insert_printable_char('a')
403+
395404

396405
class MiscTests(unittest.TestCase):
397406

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ A. Amoroso
4141
Mark Anacker
4242
Shashwat Anand
4343
Anders Andersen
44+
Tycho Andersen
4445
John Anderson
4546
Pehr Anderson
4647
Erik Andersén

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ Core and Builtins
140140
Library
141141
-------
142142

143+
- Issue #13051: Fixed recursion errors in large or resized
144+
curses.textpad.Textbox. Based on patch by Tycho Andersen.
145+
143146
- Issue #9770: curses.ascii predicates now work correctly with negative
144147
integers.
145148

0 commit comments

Comments
 (0)
X Tutup