gh-97928: Change the behavior of tkinter.Text.count() (GH-98484)

It now always returns an integer if one or less counting options are specified.
Previously it could return a single count as a 1-tuple, an integer (only if
option "update" was specified) or None if no items found.
The result is now the same if wantobjects is set to 0.
This commit is contained in:
Serhiy Storchaka 2023-10-24 12:59:19 +03:00 committed by GitHub
parent 81eba76450
commit b8c20f9049
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 53 additions and 55 deletions

View file

@ -10,6 +10,7 @@ class TextTest(AbstractTkTest, unittest.TestCase):
def setUp(self):
super().setUp()
self.text = tkinter.Text(self.root)
self.text.pack()
def test_debug(self):
text = self.text
@ -41,8 +42,6 @@ class TextTest(AbstractTkTest, unittest.TestCase):
self.assertEqual(text.search('test', '1.0', 'end'), '1.3')
def test_count(self):
# XXX Some assertions do not check against the intended result,
# but instead check the current result to prevent regression.
text = self.text
text.insert('1.0',
'Lorem ipsum dolor sit amet,\n'
@ -53,44 +52,27 @@ class TextTest(AbstractTkTest, unittest.TestCase):
options = ('chars', 'indices', 'lines',
'displaychars', 'displayindices', 'displaylines',
'xpixels', 'ypixels')
if self.wantobjects:
self.assertEqual(len(text.count('1.0', 'end', *options)), 8)
else:
text.count('1.0', 'end', *options)
self.assertEqual(text.count('1.0', 'end', 'chars', 'lines'), (124, 4)
if self.wantobjects else '124 4')
self.assertEqual(text.count('1.3', '4.5', 'chars', 'lines'), (92, 3)
if self.wantobjects else '92 3')
self.assertEqual(text.count('4.5', '1.3', 'chars', 'lines'), (-92, -3)
if self.wantobjects else '-92 -3')
self.assertEqual(text.count('1.3', '1.3', 'chars', 'lines'), (0, 0)
if self.wantobjects else '0 0')
self.assertEqual(text.count('1.0', 'end', 'lines'), (4,)
if self.wantobjects else ('4',))
self.assertEqual(text.count('end', '1.0', 'lines'), (-4,)
if self.wantobjects else ('-4',))
self.assertEqual(text.count('1.3', '1.5', 'lines'), None
if self.wantobjects else ('0',))
self.assertEqual(text.count('1.3', '1.3', 'lines'), None
if self.wantobjects else ('0',))
self.assertEqual(text.count('1.0', 'end'), (124,) # 'indices' by default
if self.wantobjects else ('124',))
self.assertEqual(len(text.count('1.0', 'end', *options)), 8)
self.assertEqual(text.count('1.0', 'end', 'chars', 'lines'), (124, 4))
self.assertEqual(text.count('1.3', '4.5', 'chars', 'lines'), (92, 3))
self.assertEqual(text.count('4.5', '1.3', 'chars', 'lines'), (-92, -3))
self.assertEqual(text.count('1.3', '1.3', 'chars', 'lines'), (0, 0))
self.assertEqual(text.count('1.0', 'end', 'lines'), 4)
self.assertEqual(text.count('end', '1.0', 'lines'), -4)
self.assertEqual(text.count('1.3', '1.5', 'lines'), 0)
self.assertEqual(text.count('1.3', '1.3', 'lines'), 0)
self.assertEqual(text.count('1.0', 'end'), 124) # 'indices' by default
self.assertEqual(text.count('1.0', 'end', 'indices'), 124)
self.assertRaises(tkinter.TclError, text.count, '1.0', 'end', 'spam')
self.assertRaises(tkinter.TclError, text.count, '1.0', 'end', '-lines')
self.assertIsInstance(text.count('1.3', '1.5', 'ypixels'), tuple)
self.assertIsInstance(text.count('1.3', '1.5', 'update', 'ypixels'), int
if self.wantobjects else str)
self.assertEqual(text.count('1.3', '1.3', 'update', 'ypixels'), None
if self.wantobjects else '0')
self.assertEqual(text.count('1.3', '1.5', 'update', 'indices'), 2
if self.wantobjects else '2')
self.assertEqual(text.count('1.3', '1.3', 'update', 'indices'), None
if self.wantobjects else '0')
self.assertEqual(text.count('1.3', '1.5', 'update'), (2,)
if self.wantobjects else ('2',))
self.assertEqual(text.count('1.3', '1.3', 'update'), None
if self.wantobjects else ('0',))
self.assertIsInstance(text.count('1.3', '1.5', 'ypixels'), int)
self.assertIsInstance(text.count('1.3', '1.5', 'update', 'ypixels'), int)
self.assertEqual(text.count('1.3', '1.3', 'update', 'ypixels'), 0)
self.assertEqual(text.count('1.3', '1.5', 'update', 'indices'), 2)
self.assertEqual(text.count('1.3', '1.3', 'update', 'indices'), 0)
self.assertEqual(text.count('1.3', '1.5', 'update'), 2)
self.assertEqual(text.count('1.3', '1.3', 'update'), 0)
if __name__ == "__main__":