[Python-checkins] bpo-44404: tkinter `after` support callable classes (GH-26812)

miss-islington webhook-mailer at python.org
Sun Jun 27 04:02:13 EDT 2021


https://github.com/python/cpython/commit/e1f3bd2bb50a76ba15a2f8d561e2c9968ae3a1b2
commit: e1f3bd2bb50a76ba15a2f8d561e2c9968ae3a1b2
branch: 3.10
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2021-06-27T01:02:02-07:00
summary:

bpo-44404: tkinter `after` support callable classes (GH-26812)

(cherry picked from commit e9c8f784fa13ea3a51df3b72a498a3896ec9e768)

Co-authored-by: E-Paine <63801254+E-Paine at users.noreply.github.com>

files:
A Misc/NEWS.d/next/Library/2021-06-20-19-01-11.bpo-44404.McfrYB.rst
M Lib/tkinter/__init__.py
M Lib/tkinter/test/test_tkinter/test_misc.py

diff --git a/Lib/tkinter/__init__.py b/Lib/tkinter/__init__.py
index 369004c9d1b3d..2513c972bc77f 100644
--- a/Lib/tkinter/__init__.py
+++ b/Lib/tkinter/__init__.py
@@ -841,7 +841,11 @@ def callit():
                         self.deletecommand(name)
                     except TclError:
                         pass
-            callit.__name__ = func.__name__
+            try:
+                callit.__name__ = func.__name__
+            except AttributeError:
+                # Required for callable classes (bpo-44404)
+                callit.__name__ = type(func).__name__
             name = self._register(callit)
             return self.tk.call('after', ms, name)
 
diff --git a/Lib/tkinter/test/test_tkinter/test_misc.py b/Lib/tkinter/test/test_tkinter/test_misc.py
index d4b7cbd867bc0..ab8f64790dfc0 100644
--- a/Lib/tkinter/test/test_tkinter/test_misc.py
+++ b/Lib/tkinter/test/test_tkinter/test_misc.py
@@ -1,3 +1,4 @@
+import functools
 import unittest
 import tkinter
 import enum
@@ -98,6 +99,12 @@ def callback(start=0, step=1):
         with self.assertRaises(tkinter.TclError):
             root.tk.call(script)
 
+        # Call with a callable class
+        count = 0
+        timer1 = root.after(0, functools.partial(callback, 42, 11))
+        root.update()  # Process all pending events.
+        self.assertEqual(count, 53)
+
     def test_after_idle(self):
         root = self.root
 
diff --git a/Misc/NEWS.d/next/Library/2021-06-20-19-01-11.bpo-44404.McfrYB.rst b/Misc/NEWS.d/next/Library/2021-06-20-19-01-11.bpo-44404.McfrYB.rst
new file mode 100644
index 0000000000000..ff6ca1bfa7242
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-06-20-19-01-11.bpo-44404.McfrYB.rst
@@ -0,0 +1 @@
+:mod:`tkinter`'s ``after()`` method now supports callables without the ``__name__`` attribute.



More information about the Python-checkins mailing list