| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -43,16 +43,20 @@ class Textbox: | |||
| 43 | 43 | def __init__(self, win, insert_mode=False): | |
| 44 | 44 | self.win = win | |
| 45 | 45 | 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() | ||
| 49 | 47 | self.stripspaces = 1 | |
| 50 | 48 | self.lastcmd = None | |
| 51 | 49 | win.keypad(1) | |
| 52 | 50 | ||
| 51 | + def _update_max_yx(self): | ||
| 52 | + maxy, maxx = self.win.getmaxyx() | ||
| 53 | + self.maxy = maxy - 1 | ||
| 54 | + self.maxx = maxx - 1 | ||
| 55 | + | ||
| 53 | 56 | def _end_of_line(self, y): | |
| 54 | 57 | """Go to the location of the first blank on the given line, | |
| 55 | 58 | returning the index of the last non-blank character.""" | |
| 59 | + self._update_max_yx() | ||
| 56 | 60 | last = self.maxx | |
| 57 | 61 | while True: | |
| 58 | 62 | if curses.ascii.ascii(self.win.inch(y, last)) != curses.ascii.SP: | |
@@ -64,8 +68,10 @@ def _end_of_line(self, y): | |||
| 64 | 68 | return last | |
| 65 | 69 | ||
| 66 | 70 | def _insert_printable_char(self, ch): | |
| 71 | + self._update_max_yx() | ||
| 67 | 72 | (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: | ||
| 69 | 75 | if self.insert_mode: | |
| 70 | 76 | oldch = self.win.inch() | |
| 71 | 77 | # The try-catch ignores the error we trigger from some curses | |
@@ -75,14 +81,20 @@ def _insert_printable_char(self, ch): | |||
| 75 | 81 | self.win.addch(ch) | |
| 76 | 82 | except curses.error: | |
| 77 | 83 | 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) | ||
| 83 | 94 | ||
| 84 | 95 | def do_command(self, ch): | |
| 85 | 96 | "Process a single editing command." | |
| 97 | + self._update_max_yx() | ||
| 86 | 98 | (y, x) = self.win.getyx() | |
| 87 | 99 | self.lastcmd = ch | |
| 88 | 100 | if curses.ascii.isprint(ch): | |
@@ -148,6 +160,7 @@ def do_command(self, ch): | |||
| 148 | 160 | def gather(self): | |
| 149 | 161 | "Collect and return the contents of the window." | |
| 150 | 162 | result = "" | |
| 163 | + self._update_max_yx() | ||
| 151 | 164 | for y in range(self.maxy+1): | |
| 152 | 165 | self.win.move(y, 0) | |
| 153 | 166 | stop = self._end_of_line(y) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -27,6 +27,7 @@ | |||
| 27 | 27 | curses = import_module('curses') | |
| 28 | 28 | import_module('curses.panel') | |
| 29 | 29 | import_module('curses.ascii') | |
| 30 | + import_module('curses.textpad') | ||
| 30 | 31 | ||
| 31 | 32 | def requires_curses_func(name): | |
| 32 | 33 | return unittest.skipUnless(hasattr(curses, name), | |
@@ -392,6 +393,14 @@ def test_issue21088(self): | |||
| 392 | 393 | human_readable_signature = stdscr.addch.__doc__.split("\n")[0] | |
| 393 | 394 | self.assertIn("[y, x,]", human_readable_signature) | |
| 394 | 395 | ||
| 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 | + | ||
| 395 | 404 | ||
| 396 | 405 | class MiscTests(unittest.TestCase): | |
| 397 | 406 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -41,6 +41,7 @@ A. Amoroso | |||
| 41 | 41 | Mark Anacker | |
| 42 | 42 | Shashwat Anand | |
| 43 | 43 | Anders Andersen | |
| 44 | + Tycho Andersen | ||
| 44 | 45 | John Anderson | |
| 45 | 46 | Pehr Anderson | |
| 46 | 47 | Erik Andersén | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -140,6 +140,9 @@ Core and Builtins | |||
| 140 | 140 | Library | |
| 141 | 141 | ------- | |
| 142 | 142 | ||
| 143 | + - Issue #13051: Fixed recursion errors in large or resized | ||
| 144 | + curses.textpad.Textbox. Based on patch by Tycho Andersen. | ||
| 145 | + | ||
| 143 | 146 | - Issue #9770: curses.ascii predicates now work correctly with negative | |
| 144 | 147 | integers. | |
| 145 | 148 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments