gh-66410: Do not stringify arguments of Tkinter callback by serhiy-storchaka · Pull Request #98592 · python/cpython · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Doc/whatsnew/3.13.rst
1 change: 1 addition & 0 deletions Lib/idlelib/redirector.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ def dispatch(self, operation, *args):
to *args to accomplish that. For an example, see colorizer.py.

'''
operation = str(operation) # can be a Tcl_Obj
m = self._operations.get(operation)
try:
if m:
Expand Down
35 changes: 23 additions & 12 deletions Lib/test/test_tcl.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,29 +482,36 @@ def testfunc(arg):
return arg
self.interp.createcommand('testfunc', testfunc)
self.addCleanup(self.interp.tk.deletecommand, 'testfunc')
def check(value, expected=None, *, eq=self.assertEqual):
if expected is None:
expected = value
def check(value, expected1=None, expected2=None, *, eq=self.assertEqual):
expected = value
if self.wantobjects >= 2:
if expected2 is not None:
expected = expected2
expected_type = type(expected)
else:
if expected1 is not None:
expected = expected1
expected_type = str
nonlocal result
result = None
r = self.interp.call('testfunc', value)
self.assertIsInstance(result, str)
self.assertIsInstance(result, expected_type)
eq(result, expected)
self.assertIsInstance(r, str)
self.assertIsInstance(r, expected_type)
eq(r, expected)
def float_eq(actual, expected):
self.assertAlmostEqual(float(actual), expected,
delta=abs(expected) * 1e-10)

check(True, '1')
check(False, '0')
check(True, '1', 1)
check(False, '0', 0)
check('string')
check('string\xbd')
check('string\u20ac')
check('string\U0001f4bb')
if sys.platform != 'win32':
check('<\udce2\udc82\udcac>', '<\u20ac>')
check('<\udced\udca0\udcbd\udced\udcb2\udcbb>', '<\U0001f4bb>')
check('<\udce2\udc82\udcac>', '<\u20ac>', '<\u20ac>')
check('<\udced\udca0\udcbd\udced\udcb2\udcbb>', '<\U0001f4bb>', '<\U0001f4bb>')
check('')
check(b'string', 'string')
check(b'string\xe2\x82\xac', 'string\xe2\x82\xac')
Expand All @@ -526,9 +533,13 @@ def float_eq(actual, expected):
check(float('inf'), eq=float_eq)
check(-float('inf'), eq=float_eq)
# XXX NaN representation can be not parsable by float()
check((), '')
check((1, (2,), (3, 4), '5 6', ()), '1 2 {3 4} {5 6} {}')
check([1, [2,], [3, 4], '5 6', []], '1 2 {3 4} {5 6} {}')
check((), '', '')
check((1, (2,), (3, 4), '5 6', ()),
'1 2 {3 4} {5 6} {}',
(1, (2,), (3, 4), '5 6', ''))
check([1, [2,], [3, 4], '5 6', []],
'1 2 {3 4} {5 6} {}',
(1, (2,), (3, 4), '5 6', ''))

def test_splitlist(self):
splitlist = self.interp.tk.splitlist
Expand Down
7 changes: 5 additions & 2 deletions Lib/tkinter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
from tkinter.constants import *
import re

wantobjects = 1
wantobjects = 2
_debug = False # set to True to print executed Tcl/Tk commands

TkVersion = float(_tkinter.TK_VERSION)
Expand Down Expand Up @@ -1762,7 +1762,10 @@ def getint_event(s):
try:
e.type = EventType(T)
except ValueError:
e.type = T
try:
e.type = EventType(str(T)) # can be int
except ValueError:
e.type = T
try:
e.widget = self._nametowidget(W)
except KeyError:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Callbacks registered in the :mod:`tkinter` module now take arguments as
various Python objects (``int``, ``float``, ``bytes``, ``tuple``), not just
``str``. To restore the previous behavior set :mod:`!tkinter` module global
:data:`~tkinter.wantobject` to ``1`` before creating the
:class:`~tkinter.Tk` object or call the :meth:`~tkinter.Tk.wantobject`
method of the :class:`!Tk` object with argument ``1``. Calling it with
argument ``2`` restores the current default behavior.
17 changes: 10 additions & 7 deletions Modules/_tkinter.c
8 changes: 4 additions & 4 deletions Modules/clinic/_tkinter.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.