https://github.com/python/cpython/commit/b1065767b4c1cbc8f98905e265a65a6a5189a185
commit: b1065767b4c1cbc8f98905e265a65a6a5189a185
branch: 3.13
author: Miss Islington (bot) <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2025-01-13T18:23:33+02:00
summary:

[3.13] gh-128562: Fix generation of the tkinter widget names (GH-128604) 
(GH-128791)

There were possible conflicts if the widget class name ends with a digit.
(cherry picked from commit da8825ea95a7096bb4f933d33b212a94ade10f6e)

Co-authored-by: Zhikang Yan <[email protected]>

files:
A Misc/NEWS.d/next/Library/2025-01-08-03-09-29.gh-issue-128562.Mlv-yO.rst
M Lib/test/test_tkinter/test_misc.py
M Lib/tkinter/__init__.py

diff --git a/Lib/test/test_tkinter/test_misc.py 
b/Lib/test/test_tkinter/test_misc.py
index c7c1120ac48d18..dbaf970161ce78 100644
--- a/Lib/test/test_tkinter/test_misc.py
+++ b/Lib/test/test_tkinter/test_misc.py
@@ -31,12 +31,20 @@ def test_repr(self):
         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):
diff --git a/Lib/tkinter/__init__.py b/Lib/tkinter/__init__.py
index 51d97634d38a12..8b2502b4c0a165 100644
--- a/Lib/tkinter/__init__.py
+++ b/Lib/tkinter/__init__.py
@@ -2739,6 +2739,8 @@ def _setup(self, master, cnf):
             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
index 00000000000000..eb50dded67bea8
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2025-01-08-03-09-29.gh-issue-128562.Mlv-yO.rst
@@ -0,0 +1 @@
+Fix possible conflicts in generated :mod:`tkinter` widget names if the widget 
class name ends with a digit.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to