]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-128562: Fix generation of the tkinter widget names (GH-128604)
authorZhikang Yan <2951256653@qq.com>
Mon, 13 Jan 2025 15:54:46 +0000 (23:54 +0800)
committerGitHub <noreply@github.com>
Mon, 13 Jan 2025 15:54:46 +0000 (15:54 +0000)
There were possible conflicts if the widget class name ends with a digit.

Lib/test/test_tkinter/test_misc.py
Lib/tkinter/__init__.py
Misc/NEWS.d/next/Library/2025-01-08-03-09-29.gh-issue-128562.Mlv-yO.rst [new file with mode: 0644]

index 3362169391818b6268c722a032f613ed966595d4..96ea3f0117ca0363946273b4a6a9efc26d3faee7 100644 (file)
@@ -31,12 +31,20 @@ class MiscTest(AbstractTkTest, unittest.TestCase):
         self.assertEqual(repr(f), '<tkinter.Frame object .top.child>')
 
     def test_generated_names(self):
+        class Button2(tkinter.Button):
+            pass
+
         t = tkinter.Toplevel(self.root)
         f = tkinter.Frame(t)
         f2 = tkinter.Frame(t)
+        self.assertNotEqual(str(f), str(f2))
         b = tkinter.Button(f2)
-        for name in str(b).split('.'):
+        b2 = Button2(f2)
+        for name in str(b).split('.') + str(b2).split('.'):
             self.assertFalse(name.isidentifier(), msg=repr(name))
+        b3 = tkinter.Button(f2)
+        b4 = Button2(f2)
+        self.assertEqual(len({str(b), str(b2), str(b3), str(b4)}), 4)
 
     @requires_tk(8, 6, 6)
     def test_tk_busy(self):
index d494c0c9687cd1d46f5a23383aa3482c90ee6210..0baed8b569e40fac8d50e947df7b2eea488fa758 100644 (file)
@@ -2741,6 +2741,8 @@ class BaseWidget(Misc):
             del cnf['name']
         if not name:
             name = self.__class__.__name__.lower()
+            if name[-1].isdigit():
+                name += "!"  # Avoid duplication when calculating names below
             if master._last_child_ids is None:
                 master._last_child_ids = {}
             count = master._last_child_ids.get(name, 0) + 1
diff --git a/Misc/NEWS.d/next/Library/2025-01-08-03-09-29.gh-issue-128562.Mlv-yO.rst b/Misc/NEWS.d/next/Library/2025-01-08-03-09-29.gh-issue-128562.Mlv-yO.rst
new file mode 100644 (file)
index 0000000..eb50dde
--- /dev/null
@@ -0,0 +1 @@
+Fix possible conflicts in generated :mod:`tkinter` widget names if the widget class name ends with a digit.