From c60c85433b3ade71e3c337fc8b57f29f22ce6438 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sat, 10 Oct 2026 19:16:13 +0300 Subject: [PATCH] gh-159127: Fix tests depending on the layout of unmapped text widgets A text widget which has not been mapped yet had size 1x1 and its content was laid out in that area, putting every character on its own display line. Tk no longer does this. --- Lib/idlelib/idle_test/test_codecontext.py | 2 ++ Lib/idlelib/idle_test/test_sidebar.py | 4 ++++ Lib/test/test_tkinter/test_text.py | 14 ++++++++++---- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/Lib/idlelib/idle_test/test_codecontext.py b/Lib/idlelib/idle_test/test_codecontext.py index 3f070da022384ec..13465d5c98c4872 100644 --- a/Lib/idlelib/idle_test/test_codecontext.py +++ b/Lib/idlelib/idle_test/test_codecontext.py @@ -60,6 +60,8 @@ def setUpClass(cls): frame = cls.frame = Frame(root) text = cls.text = Text(frame) text.insert('1.0', code_sample) + # Make the text longer than the visible height, so that it can scroll. + text.insert('end', '\n' * (int(text['height']) + 2)) # Need to pack for creation of code context text widget. frame.pack(side='left', fill='both', expand=1) text.grid(row=1, column=1, sticky=NSEW) diff --git a/Lib/idlelib/idle_test/test_sidebar.py b/Lib/idlelib/idle_test/test_sidebar.py index 848f12a5d7c1850..6901413c416afda 100644 --- a/Lib/idlelib/idle_test/test_sidebar.py +++ b/Lib/idlelib/idle_test/test_sidebar.py @@ -317,6 +317,10 @@ def test_drag_selection_up(self): self.assertEqual(self.get_selection(), ('2.0', '5.0')) def test_scroll(self): + # Synchronized scrolling needs the geometry of mapped widgets. + self.root.deiconify() + self.addCleanup(self.root.withdraw) + self.linenumber.show_sidebar() self.text.insert('1.0', 'line\n' * 100) self.root.update() diff --git a/Lib/test/test_tkinter/test_text.py b/Lib/test/test_tkinter/test_text.py index 27423006a7e71b8..c28f4e3f130d7af 100644 --- a/Lib/test/test_tkinter/test_text.py +++ b/Lib/test/test_tkinter/test_text.py @@ -774,10 +774,16 @@ def test_count(self): self.assertRaises(TclError, text.count, '1.0', 'end', 'spam') self.assertRaises(TclError, text.count, '1.0', 'end', '-lines') - self.assertIsInstance(text.count('1.3', '1.5', 'ypixels', return_ints=True), int) - self.assertIsInstance(text.count('1.3', '1.5', 'ypixels'), tuple) - self.assertIsInstance(text.count('1.3', '1.5', 'update', 'ypixels', return_ints=True), int) - self.assertIsInstance(text.count('1.3', '1.5', 'update', 'ypixels'), int) + # The ypixels counts depend on the widget geometry. + text.update() + + # Indices on the same display line have zero vertical distance. + self.assertEqual(text.count('1.3', '1.5', 'ypixels', return_ints=True), 0) + self.assertIsNone(text.count('1.3', '1.5', 'ypixels')) + self.assertIsInstance(text.count('1.3', '2.5', 'ypixels', return_ints=True), int) + self.assertIsInstance(text.count('1.3', '2.5', 'ypixels'), tuple) + self.assertIsInstance(text.count('1.3', '2.5', 'update', 'ypixels', return_ints=True), int) + self.assertIsInstance(text.count('1.3', '2.5', 'update', 'ypixels'), int) self.assertEqual(text.count('1.3', '1.3', 'update', 'ypixels', return_ints=True), 0) self.assertEqual(text.count('1.3', '1.3', 'update', 'ypixels'), None) self.assertEqual(text.count('1.3', '1.5', 'update', 'indices', return_ints=True), 2)