]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.11] gh-103685: Fix tkinter.Menu.index() for Tk 8.7 (GH-103686) (#103734)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Mon, 24 Apr 2023 02:29:58 +0000 (19:29 -0700)
committerGitHub <noreply@github.com>
Mon, 24 Apr 2023 02:29:58 +0000 (22:29 -0400)
gh-103685: Fix tkinter.Menu.index() for Tk 8.7 (GH-103686)

---------

(cherry picked from commit f0ed293f6aec1c2ed22725301b77d6ccedc2d486)

Co-authored-by: Christopher Chavez <chrischavez@gmx.us>
Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Lib/tkinter/__init__.py
Lib/tkinter/test/test_tkinter/test_widgets.py
Misc/NEWS.d/next/Library/2023-04-24-00-34-23.gh-issue-103685.U14jBM.rst [new file with mode: 0644]

index 7565e0f7e46073e156597f0807627e7671ce6ea4..054034098880f0f079c8f4030462feb0d7d04e74 100644 (file)
@@ -3429,8 +3429,7 @@ class Menu(Widget):
     def index(self, index):
         """Return the index of a menu item identified by INDEX."""
         i = self.tk.call(self._w, 'index', index)
-        if i == 'none': return None
-        return self.tk.getint(i)
+        return None if i in ('', 'none') else self.tk.getint(i)  # GH-103685.
 
     def invoke(self, index):
         """Invoke a menu item identified by INDEX and execute
index da321a1daedb5464746ea77de377fa7c0ee2b80f..a756276ec76966b6c0ef503ff9f6ac4d27a91e24 100644 (file)
@@ -1378,6 +1378,11 @@ class MenuTest(AbstractWidgetTest, unittest.TestCase):
     def create(self, **kwargs):
         return tkinter.Menu(self.root, **kwargs)
 
+    def test_indexcommand_none(self):
+        widget = self.create()
+        i = widget.index('none')
+        self.assertIsNone(i)
+
     def test_configure_postcommand(self):
         widget = self.create()
         self.checkCommandParam(widget, 'postcommand')
diff --git a/Misc/NEWS.d/next/Library/2023-04-24-00-34-23.gh-issue-103685.U14jBM.rst b/Misc/NEWS.d/next/Library/2023-04-24-00-34-23.gh-issue-103685.U14jBM.rst
new file mode 100644 (file)
index 0000000..31df047
--- /dev/null
@@ -0,0 +1 @@
+Prepare :meth:`tkinter.Menu.index` for Tk 8.7 so that it does not raise ``TclError: expected integer but got ""`` when it should return ``None``.