Replace long outputs in the shell with a simple button. This avoids IDLE's shell slowing down considerably, and even becoming completely unresponsive, when very long outputs are written.
| 198 | |
| 199 | |
| 200 | class Squeezer: |
| 201 | """Replace long outputs in the shell with a simple button. |
| 202 | |
| 203 | This avoids IDLE's shell slowing down considerably, and even becoming |
| 204 | completely unresponsive, when very long outputs are written. |
| 205 | """ |
| 206 | @classmethod |
| 207 | def reload(cls): |
| 208 | """Load class variables from config.""" |
| 209 | cls.auto_squeeze_min_lines = idleConf.GetOption( |
| 210 | "main", "PyShell", "auto-squeeze-min-lines", |
| 211 | type="int", default=50, |
| 212 | ) |
| 213 | |
| 214 | def __init__(self, editwin): |
| 215 | """Initialize settings for Squeezer. |
| 216 | |
| 217 | editwin is the shell's Editor window. |
| 218 | self.text is the editor window text widget. |
| 219 | self.base_test is the actual editor window Tk text widget, rather than |
| 220 | EditorWindow's wrapper. |
| 221 | self.expandingbuttons is the list of all buttons representing |
| 222 | "squeezed" output. |
| 223 | """ |
| 224 | self.editwin = editwin |
| 225 | self.text = text = editwin.text |
| 226 | |
| 227 | # Get the base Text widget of the PyShell object, used to change |
| 228 | # text before the iomark. PyShell deliberately disables changing |
| 229 | # text before the iomark via its 'text' attribute, which is |
| 230 | # actually a wrapper for the actual Text widget. Squeezer, |
| 231 | # however, needs to make such changes. |
| 232 | self.base_text = editwin.per.bottom |
| 233 | |
| 234 | # Twice the text widget's border width and internal padding; |
| 235 | # pre-calculated here for the get_line_width() method. |
| 236 | self.window_width_delta = 2 * ( |
| 237 | int(text.cget('border')) + |
| 238 | int(text.cget('padx')) |
| 239 | ) |
| 240 | |
| 241 | self.expandingbuttons = [] |
| 242 | |
| 243 | # Replace the PyShell instance's write method with a wrapper, |
| 244 | # which inserts an ExpandingButton instead of a long text. |
| 245 | def mywrite(s, tags=(), write=editwin.write): |
| 246 | # Only auto-squeeze text which has just the "stdout" tag. |
| 247 | if tags != "stdout": |
| 248 | return write(s, tags) |
| 249 | |
| 250 | # Only auto-squeeze text with at least the minimum |
| 251 | # configured number of lines. |
| 252 | auto_squeeze_min_lines = self.auto_squeeze_min_lines |
| 253 | # First, a very quick check to skip very short texts. |
| 254 | if len(s) < auto_squeeze_min_lines: |
| 255 | return write(s, tags) |
| 256 | # Now the full line-count check. |
| 257 | numoflines = self.count_lines(s) |
no outgoing calls
searching dependent graphs…