]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-97928: Partially restore the behavior of tkinter.Text.count() by default (GH-115031)
authorSerhiy Storchaka <storchaka@gmail.com>
Sun, 11 Feb 2024 10:43:14 +0000 (12:43 +0200)
committerGitHub <noreply@github.com>
Sun, 11 Feb 2024 10:43:14 +0000 (12:43 +0200)
By default, it preserves an inconsistent behavior of older Python
versions: packs the count into a 1-tuple if only one or none
options are specified (including 'update'), returns None instead of 0.
Except that setting wantobjects to 0 no longer affects the result.

Add a new parameter return_ints: specifying return_ints=True makes
Text.count() always returning the single count as an integer
instead of a 1-tuple or None.

Doc/whatsnew/3.13.rst
Lib/idlelib/sidebar.py
Lib/test/test_tkinter/test_text.py
Lib/tkinter/__init__.py
Misc/NEWS.d/next/Library/2024-02-05-16-48-06.gh-issue-97928.JZCies.rst [new file with mode: 0644]

index aee37737a9990accbc61fb9e73a4c02c6429b45a..1b803278ae0d5bdf04460684f85a8cf384d3664c 100644 (file)
@@ -469,6 +469,12 @@ tkinter
   a dict instead of a tuple.
   (Contributed by Serhiy Storchaka in :gh:`43457`.)
 
+* Add new optional keyword-only parameter *return_ints* in
+  the :meth:`!Text.count` method.
+  Passing ``return_ints=True`` makes it always returning the single count
+  as an integer instead of a 1-tuple or ``None``.
+  (Contributed by Serhiy Storchaka in :gh:`97928`.)
+
 * Add support of the "vsapi" element type in
   the :meth:`~tkinter.ttk.Style.element_create` method of
   :class:`tkinter.ttk.Style`.
@@ -1286,13 +1292,6 @@ that may require changes to your code.
 Changes in the Python API
 -------------------------
 
-* :meth:`!tkinter.Text.count` 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``.
-  (Contributed by Serhiy Storchaka in :gh:`97928`.)
-
 * Functions :c:func:`PyDict_GetItem`, :c:func:`PyDict_GetItemString`,
   :c:func:`PyMapping_HasKey`, :c:func:`PyMapping_HasKeyString`,
   :c:func:`PyObject_HasAttr`, :c:func:`PyObject_HasAttrString`, and
index ff77b568a786e0faf0663745c3f3e49de35a04a2..aa19a24e3edef28cc864d7fc3dda979d9c3ef76a 100644 (file)
@@ -27,7 +27,7 @@ def get_displaylines(text, index):
     """Display height, in lines, of a logical line in a Tk text widget."""
     return text.count(f"{index} linestart",
                       f"{index} lineend",
-                      "displaylines")
+                      "displaylines", return_ints=True)
 
 def get_widget_padding(widget):
     """Get the total padding of a Tk widget, including its border."""
index f809c4510e3a1f3e02cc6074ea6e423678a10eed..b26956930d340283180b087a3cb9941dee3477ed 100644 (file)
@@ -52,27 +52,47 @@ class TextTest(AbstractTkTest, unittest.TestCase):
         options = ('chars', 'indices', 'lines',
                    'displaychars', 'displayindices', 'displaylines',
                    'xpixels', 'ypixels')
+        self.assertEqual(len(text.count('1.0', 'end', *options, return_ints=True)), 8)
         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.0', 'end', 'chars', 'lines', return_ints=True),
+                         (124, 4))
         self.assertEqual(text.count('1.3', '4.5', 'chars', 'lines'), (92, 3))
+        self.assertEqual(text.count('4.5', '1.3', 'chars', 'lines', return_ints=True),
+                         (-92, -3))
         self.assertEqual(text.count('4.5', '1.3', 'chars', 'lines'), (-92, -3))
+        self.assertEqual(text.count('1.3', '1.3', 'chars', 'lines', return_ints=True),
+                         (0, 0))
         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.assertEqual(text.count('1.0', 'end', 'lines', return_ints=True), 4)
+        self.assertEqual(text.count('1.0', 'end', 'lines'), (4,))
+        self.assertEqual(text.count('end', '1.0', 'lines', return_ints=True), -4)
+        self.assertEqual(text.count('end', '1.0', 'lines'), (-4,))
+        self.assertEqual(text.count('1.3', '1.5', 'lines', return_ints=True), 0)
+        self.assertEqual(text.count('1.3', '1.5', 'lines'), None)
+        self.assertEqual(text.count('1.3', '1.3', 'lines', return_ints=True), 0)
+        self.assertEqual(text.count('1.3', '1.3', 'lines'), None)
+        # Count 'indices' by default.
+        self.assertEqual(text.count('1.0', 'end', return_ints=True), 124)
+        self.assertEqual(text.count('1.0', 'end'), (124,))
+        self.assertEqual(text.count('1.0', 'end', 'indices', return_ints=True), 124)
+        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'), int)
+        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)
-        self.assertEqual(text.count('1.3', '1.3', 'update', 'ypixels'), 0)
+        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)
         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)
+        self.assertEqual(text.count('1.3', '1.3', 'update', 'indices', return_ints=True), 0)
+        self.assertEqual(text.count('1.3', '1.3', 'update', 'indices'), None)
+        self.assertEqual(text.count('1.3', '1.5', 'update', return_ints=True), 2)
+        self.assertEqual(text.count('1.3', '1.5', 'update'), (2,))
+        self.assertEqual(text.count('1.3', '1.3', 'update', return_ints=True), 0)
+        self.assertEqual(text.count('1.3', '1.3', 'update'), None)
 
 
 if __name__ == "__main__":
index 2be9da2cfb92993da17ec30ee65ec3a1e0d02c10..175bfbd7d912d2e99d2b4b3b0f7a3f927b043b3f 100644 (file)
@@ -3745,7 +3745,7 @@ class Text(Widget, XView, YView):
         return self.tk.getboolean(self.tk.call(
             self._w, 'compare', index1, op, index2))
 
-    def count(self, index1, index2, *options): # new in Tk 8.5
+    def count(self, index1, index2, *options, return_ints=False): # new in Tk 8.5
         """Counts the number of relevant things between the two indices.
 
         If INDEX1 is after INDEX2, the result will be a negative number
@@ -3753,19 +3753,26 @@ class Text(Widget, XView, YView):
 
         The actual items which are counted depends on the options given.
         The result is a tuple of integers, one for the result of each
-        counting option given, if more than one option is specified,
-        otherwise it is an integer. Valid counting options are "chars",
-        "displaychars", "displayindices", "displaylines", "indices",
-        "lines", "xpixels" and "ypixels". The default value, if no
-        option is specified, is "indices". There is an additional possible
-        option "update", which if given then all subsequent options ensure
-        that any possible out of date information is recalculated."""
+        counting option given, if more than one option is specified or
+        return_ints is false (default), otherwise it is an integer.
+        Valid counting options are "chars", "displaychars",
+        "displayindices", "displaylines", "indices", "lines", "xpixels"
+        and "ypixels". The default value, if no option is specified, is
+        "indices". There is an additional possible option "update",
+        which if given then all subsequent options ensure that any
+        possible out of date information is recalculated.
+        """
         options = ['-%s' % arg for arg in options]
         res = self.tk.call(self._w, 'count', *options, index1, index2)
         if not isinstance(res, int):
             res = self._getints(res)
             if len(res) == 1:
                 res, = res
+        if not return_ints:
+            if not res:
+                res = None
+            elif len(options) <= 1:
+                res = (res,)
         return res
 
     def debug(self, boolean=None):
diff --git a/Misc/NEWS.d/next/Library/2024-02-05-16-48-06.gh-issue-97928.JZCies.rst b/Misc/NEWS.d/next/Library/2024-02-05-16-48-06.gh-issue-97928.JZCies.rst
new file mode 100644 (file)
index 0000000..24fed92
--- /dev/null
@@ -0,0 +1,5 @@
+Partially revert the behavior of :meth:`tkinter.Text.count`. By default it
+preserves the behavior of older Python versions, except that setting
+``wantobjects`` to 0 no longer has effect. Add a new parameter *return_ints*:
+specifying ``return_ints=True`` makes ``Text.count()`` always returning the
+single count as an integer instead of a 1-tuple or ``None``.