From: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> Date: Mon, 13 Jan 2025 16:23:53 +0000 (+0100) Subject: [3.12] gh-128562: Fix generation of the tkinter widget names (GH-128604) (GH-128792) X-Git-Tag: v3.12.9~81 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=ca5ff6b40c8e8223c3d105f161c51e3f8254ed48;p=thirdparty%2FPython%2Fcpython.git [3.12] gh-128562: Fix generation of the tkinter widget names (GH-128604) (GH-128792) There were possible conflicts if the widget class name ends with a digit. (cherry picked from commit da8825ea95a7096bb4f933d33b212a94ade10f6e) Co-authored-by: Zhikang Yan <2951256653@qq.com> --- diff --git a/Lib/test/test_tkinter/test_misc.py b/Lib/test/test_tkinter/test_misc.py index bb6f7824ea74..605c0e51c817 100644 --- a/Lib/test/test_tkinter/test_misc.py +++ b/Lib/test/test_tkinter/test_misc.py @@ -30,12 +30,20 @@ class MiscTest(AbstractTkTest, unittest.TestCase): self.assertEqual(repr(f), '') 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) def test_tk_setPalette(self): root = self.root diff --git a/Lib/tkinter/__init__.py b/Lib/tkinter/__init__.py index ac78c04efc58..12bf7e3a9328 100644 --- a/Lib/tkinter/__init__.py +++ b/Lib/tkinter/__init__.py @@ -2626,6 +2626,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 index 000000000000..eb50dded67be --- /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.