Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f5f6ace

Browse files
miss-islingtonserhiy-storchakaclaude
authored
[3.13] gh-90304: Fix IDLE startup failure with a broken font (GH-152419) (#157397)
gh-90304: Fix IDLE startup failure with a broken font (GH-152419) On some systems a font reports a zero width for the '0' character (seen with a broken font on openSUSE). EditorWindow.set_width() divides the Text widget's pixel width by that value, so the ZeroDivisionError prevented IDLE from starting at all. set_width() now falls back to the configured width when the measured width is zero. self.width is consumed by the === RESTART === separator in the shell and by the squeezer's line wrapping. (cherry picked from commit 8a2db99) Co-authored-by: Serhiy Storchaka <[email protected]> Co-authored-by: Claude Opus 4.8 <[email protected]>
1 parent 0a7db33 commit f5f6ace

3 files changed

Lines changed: 20 additions & 1 deletion

File tree

Lib/idlelib/editor.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,9 @@ def set_width(self):
325325
# http://www.tcl.tk/man/tcl8.6/TkCmd/text.htm#M21
326326
zero_char_width = \
327327
Font(text, font=text.cget('font')).measure('0')
328-
self.width = pixel_width // zero_char_width
328+
# Some fonts report a zero width for '0' (gh-90304).
329+
self.width = (pixel_width // zero_char_width if zero_char_width
330+
else text.tk.getint(text.cget('width')))
329331

330332
def new_callback(self, event):
331333
dirname, basename = self.io.defaultfilename()

Lib/idlelib/idle_test/test_editor.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from idlelib import editor
44
import unittest
55
from collections import namedtuple
6+
from unittest import mock
67
from test.support import requires
78
from tkinter import Tk, Text
89

@@ -30,6 +31,18 @@ def test_init(self):
3031
self.assertEqual(e.root, self.root)
3132
e._close()
3233

34+
def test_set_width_zero_char_width(self):
35+
# A zero-width '0' must not raise ZeroDivisionError (gh-90304).
36+
e = Editor(root=self.root)
37+
try:
38+
with mock.patch.object(editor, 'Font') as MockFont:
39+
MockFont.return_value.measure.return_value = 0
40+
e.set_width()
41+
self.assertEqual(e.width,
42+
e.text.tk.getint(e.text.cget('width')))
43+
finally:
44+
e._close()
45+
3346

3447
class GetLineIndentTest(unittest.TestCase):
3548
def test_empty_lines(self):
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Prevent IDLE from failing to start when a font reports a zero width for
2+
the ``'0'`` character. Such a broken font caused a
3+
:exc:`ZeroDivisionError`; the editor now falls back to the configured
4+
width.

0 commit comments

Comments
 (0)