bpo-37929: IDLE: avoid Squeezer-related config dialog crashes (GH-15452)

These were caused by keeping around a reference to the Squeezer
instance and calling it's load_font() upon config changes, which
sometimes happened even if the shell window no longer existed.

This change completely removes that mechanism, instead having the
editor window properly update its width attribute, which can then
be used by Squeezer.
This commit is contained in:
Tal Einat 2019-08-25 08:52:58 +03:00 committed by GitHub
parent aef9ad82f7
commit d4b4c00b57
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 50 deletions

View file

@ -15,10 +15,8 @@
messages and their tracebacks.
"""
import re
import weakref
import tkinter as tk
from tkinter.font import Font
import tkinter.messagebox as tkMessageBox
from idlelib.config import idleConf
@ -203,8 +201,6 @@ class Squeezer:
This avoids IDLE's shell slowing down considerably, and even becoming
completely unresponsive, when very long outputs are written.
"""
_instance_weakref = None
@classmethod
def reload(cls):
"""Load class variables from config."""
@ -213,14 +209,6 @@ def reload(cls):
type="int", default=50,
)
# Loading the font info requires a Tk root. IDLE doesn't rely
# on Tkinter's "default root", so the instance will reload
# font info using its editor windows's Tk root.
if cls._instance_weakref is not None:
instance = cls._instance_weakref()
if instance is not None:
instance.load_font()
def __init__(self, editwin):
"""Initialize settings for Squeezer.
@ -241,9 +229,6 @@ def __init__(self, editwin):
# however, needs to make such changes.
self.base_text = editwin.per.bottom
Squeezer._instance_weakref = weakref.ref(self)
self.load_font()
# Twice the text widget's border width and internal padding;
# pre-calculated here for the get_line_width() method.
self.window_width_delta = 2 * (
@ -298,24 +283,7 @@ def count_lines(self, s):
Tabs are considered tabwidth characters long.
"""
linewidth = self.get_line_width()
return count_lines_with_wrapping(s, linewidth)
def get_line_width(self):
# The maximum line length in pixels: The width of the text
# widget, minus twice the border width and internal padding.
linewidth_pixels = \
self.base_text.winfo_width() - self.window_width_delta
# Divide the width of the Text widget by the font width,
# which is taken to be the width of '0' (zero).
# http://www.tcl.tk/man/tcl8.6/TkCmd/text.htm#M21
return linewidth_pixels // self.zero_char_width
def load_font(self):
text = self.base_text
self.zero_char_width = \
Font(text, font=text.cget('font')).measure('0')
return count_lines_with_wrapping(s, self.editwin.width)
def squeeze_current_text_event(self, event):
"""squeeze-current-text event handler