Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1df56bc

Browse files
bpo-42759: Fix equality comparison of Variable and Font in Tkinter (GH-23968)
Objects which belong to different Tcl interpreters are now always different, even if they have the same name.
1 parent 156b7f7 commit 1df56bc

5 files changed

Lines changed: 27 additions & 11 deletions

File tree

Lib/tkinter/__init__.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -516,15 +516,11 @@ def trace_vinfo(self):
516516
self._tk.call("trace", "vinfo", self._name))]
517517

518518
def __eq__(self, other):
519-
"""Comparison for equality (==).
520-
521-
Note: if the Variable's master matters to behavior
522-
also compare self._master == other._master
523-
"""
524519
if not isinstance(other, Variable):
525520
return NotImplemented
526-
return self.__class__.__name__ == other.__class__.__name__ \
527-
and self._name == other._name
521+
return (self._name == other._name
522+
and self.__class__.__name__ == other.__class__.__name__
523+
and self._tk == other._tk)
528524

529525

530526
class StringVar(Variable):

Lib/tkinter/font.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def __repr__(self):
107107
def __eq__(self, other):
108108
if not isinstance(other, Font):
109109
return NotImplemented
110-
return self.name == other.name
110+
return self.name == other.name and self._tk == other._tk
111111

112112
def __getitem__(self, key):
113113
return self.cget(key)

Lib/tkinter/test/test_tkinter/test_font.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,22 @@ def test_name(self):
6363
self.assertEqual(self.font.name, fontname)
6464
self.assertEqual(str(self.font), fontname)
6565

66-
def test_eq(self):
66+
def test_equality(self):
6767
font1 = font.Font(root=self.root, name=fontname, exists=True)
6868
font2 = font.Font(root=self.root, name=fontname, exists=True)
6969
self.assertIsNot(font1, font2)
7070
self.assertEqual(font1, font2)
7171
self.assertNotEqual(font1, font1.copy())
72+
7273
self.assertNotEqual(font1, 0)
7374
self.assertEqual(font1, ALWAYS_EQ)
7475

76+
root2 = tkinter.Tk()
77+
self.addCleanup(root2.destroy)
78+
font3 = font.Font(root=root2, name=fontname, exists=True)
79+
self.assertEqual(str(font1), str(font3))
80+
self.assertNotEqual(font1, font3)
81+
7582
def test_measure(self):
7683
self.assertIsInstance(self.font.measure('abc'), int)
7784

Lib/tkinter/test/test_tkinter/test_variables.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,32 @@ def test_dont_unset_not_existing(self):
5858
del v2
5959
self.assertFalse(self.info_exists("name"))
6060

61-
def test___eq__(self):
61+
def test_equality(self):
6262
# values doesn't matter, only class and name are checked
6363
v1 = Variable(self.root, name="abc")
6464
v2 = Variable(self.root, name="abc")
6565
self.assertIsNot(v1, v2)
6666
self.assertEqual(v1, v2)
6767

68-
v3 = StringVar(self.root, name="abc")
68+
v3 = Variable(self.root, name="cba")
6969
self.assertNotEqual(v1, v3)
7070

71+
v4 = StringVar(self.root, name="abc")
72+
self.assertEqual(str(v1), str(v4))
73+
self.assertNotEqual(v1, v4)
74+
7175
V = type('Variable', (), {})
7276
self.assertNotEqual(v1, V())
7377

7478
self.assertNotEqual(v1, object())
7579
self.assertEqual(v1, ALWAYS_EQ)
7680

81+
root2 = tkinter.Tk()
82+
self.addCleanup(root2.destroy)
83+
v5 = Variable(root2, name="abc")
84+
self.assertEqual(str(v1), str(v5))
85+
self.assertNotEqual(v1, v5)
86+
7787
def test_invalid_name(self):
7888
with self.assertRaises(TypeError):
7989
Variable(self.root, name=123)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fixed equality comparison of :class:`tkinter.Variable` and
2+
:class:`tkinter.font.Font`. Objects which belong to different Tcl
3+
interpreters are now always different, even if they have the same name.

0 commit comments

Comments
 (0)