From c2e76506af47b9378d4ab2d7ec055914221c2a1a Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Wed, 8 Jul 2026 14:00:25 +0300 Subject: [PATCH] gh-143990: Do not assume the requested font size in test_font On platforms with only bitmap fonts Tk substitutes the nearest available size, so query the resolved size instead of assuming it. Co-Authored-By: Claude Opus 4.8 --- Lib/test/test_tkinter/test_font.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_tkinter/test_font.py b/Lib/test/test_tkinter/test_font.py index e6f331332eb20a9..ece98988fd7e1a9 100644 --- a/Lib/test/test_tkinter/test_font.py +++ b/Lib/test/test_tkinter/test_font.py @@ -20,6 +20,10 @@ def setUpClass(cls): except tkinter.TclError: cls.font = font.Font(root=cls.root, name=fontname, exists=False) + def actual_size(self, desc): + # The requested size is not always available (e.g. bitmap fonts). + return self.root.tk.call('font', 'actual', desc, '-size') + def test_configure(self): self.assertEqual(self.font.config, self.font.configure) options = self.font.configure() @@ -51,7 +55,7 @@ def test_create(self): f = font.Font(root=self.root, font=('Times', 20, 'bold')) self.assertIn(f.name, font.names(self.root)) self.assertEqual(f.actual('weight'), 'bold') - self.assertEqual(f.cget('size'), sizetype(20)) + self.assertEqual(f.cget('size'), self.actual_size(('Times', 20, 'bold'))) # ... or from the keyword options. f = font.Font(root=self.root, family='Times', size=20, weight='bold') @@ -62,13 +66,13 @@ def test_create(self): # Explicit options override the corresponding settings of *font*. f = font.Font(root=self.root, font=('Times', 20, 'bold'), weight='normal') self.assertEqual(f.actual('weight'), 'normal') - self.assertEqual(f.cget('size'), sizetype(20)) + self.assertEqual(f.cget('size'), self.actual_size(('Times', 20, 'bold'))) # The new font can be given an explicit name. f = font.Font(root=self.root, name='testfont', font=('Times', 20)) self.assertEqual(f.name, 'testfont') self.assertIn('testfont', font.names(self.root)) - self.assertEqual(f.cget('size'), sizetype(20)) + self.assertEqual(f.cget('size'), self.actual_size(('Times', 20))) # Reusing the name of an existing font fails. self.assertRaises(tkinter.TclError, font.Font, root=self.root, name='testfont', font=('Times', 10)) @@ -96,7 +100,7 @@ def test_existing(self): self.assertEqual(str(f), 'Times 20 bold') self.assertNotIn(f.name, font.names(self.root)) self.assertEqual(f.actual('weight'), 'bold') - self.assertEqual(f.actual('size'), sizetype(20)) + self.assertEqual(f.actual('size'), self.actual_size(('Times', 20, 'bold'))) # It can be used as a widget option, with the same effect as the # description itself (gh-143990). self.assertEqual(tkinter.Label(self.root, font=f).cget('font'),