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

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Lib/idlelib/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,9 @@ def set_width(self):
# http://www.tcl.tk/man/tcl8.6/TkCmd/text.htm#M21
zero_char_width = \
Font(text, font=text.cget('font')).measure('0')
self.width = pixel_width // zero_char_width
# Some fonts report a zero width for '0' (gh-90304).
self.width = (pixel_width // zero_char_width if zero_char_width
else text.tk.getint(text.cget('width')))

def new_callback(self, event):
dirname, basename = self.io.defaultfilename()
Expand Down
13 changes: 13 additions & 0 deletions Lib/idlelib/idle_test/test_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from idlelib import editor
import unittest
from collections import namedtuple
from unittest import mock
from test.support import requires
from tkinter import Tk, Text

Expand Down Expand Up @@ -30,6 +31,18 @@ def test_init(self):
self.assertEqual(e.root, self.root)
e._close()

def test_set_width_zero_char_width(self):
# A zero-width '0' must not raise ZeroDivisionError (gh-90304).
e = Editor(root=self.root)
try:
with mock.patch.object(editor, 'Font') as MockFont:
MockFont.return_value.measure.return_value = 0
e.set_width()
self.assertEqual(e.width,
e.text.tk.getint(e.text.cget('width')))
finally:
e._close()


class GetLineIndentTest(unittest.TestCase):
def test_empty_lines(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Prevent IDLE from failing to start when a font reports a zero width for
the ``'0'`` character. Such a broken font caused a
:exc:`ZeroDivisionError`; the editor now falls back to the configured
width.
Loading