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.
This commit is contained in:
Serhiy Storchaka 2020-12-29 12:56:55 +02:00 committed by GitHub
parent 156b7f7052
commit 1df56bc059
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 27 additions and 11 deletions

View File

@ -516,15 +516,11 @@ class Variable:
self._tk.call("trace", "vinfo", self._name))] self._tk.call("trace", "vinfo", self._name))]
def __eq__(self, other): def __eq__(self, other):
"""Comparison for equality (==).
Note: if the Variable's master matters to behavior
also compare self._master == other._master
"""
if not isinstance(other, Variable): if not isinstance(other, Variable):
return NotImplemented return NotImplemented
return self.__class__.__name__ == other.__class__.__name__ \ return (self._name == other._name
and self._name == other._name and self.__class__.__name__ == other.__class__.__name__
and self._tk == other._tk)
class StringVar(Variable): class StringVar(Variable):

View File

@ -107,7 +107,7 @@ class Font:
def __eq__(self, other): def __eq__(self, other):
if not isinstance(other, Font): if not isinstance(other, Font):
return NotImplemented return NotImplemented
return self.name == other.name return self.name == other.name and self._tk == other._tk
def __getitem__(self, key): def __getitem__(self, key):
return self.cget(key) return self.cget(key)

View File

@ -63,15 +63,22 @@ class FontTest(AbstractTkTest, unittest.TestCase):
self.assertEqual(self.font.name, fontname) self.assertEqual(self.font.name, fontname)
self.assertEqual(str(self.font), fontname) self.assertEqual(str(self.font), fontname)
def test_eq(self): def test_equality(self):
font1 = font.Font(root=self.root, name=fontname, exists=True) font1 = font.Font(root=self.root, name=fontname, exists=True)
font2 = font.Font(root=self.root, name=fontname, exists=True) font2 = font.Font(root=self.root, name=fontname, exists=True)
self.assertIsNot(font1, font2) self.assertIsNot(font1, font2)
self.assertEqual(font1, font2) self.assertEqual(font1, font2)
self.assertNotEqual(font1, font1.copy()) self.assertNotEqual(font1, font1.copy())
self.assertNotEqual(font1, 0) self.assertNotEqual(font1, 0)
self.assertEqual(font1, ALWAYS_EQ) self.assertEqual(font1, ALWAYS_EQ)
root2 = tkinter.Tk()
self.addCleanup(root2.destroy)
font3 = font.Font(root=root2, name=fontname, exists=True)
self.assertEqual(str(font1), str(font3))
self.assertNotEqual(font1, font3)
def test_measure(self): def test_measure(self):
self.assertIsInstance(self.font.measure('abc'), int) self.assertIsInstance(self.font.measure('abc'), int)

View File

@ -58,22 +58,32 @@ class TestVariable(TestBase):
del v2 del v2
self.assertFalse(self.info_exists("name")) self.assertFalse(self.info_exists("name"))
def test___eq__(self): def test_equality(self):
# values doesn't matter, only class and name are checked # values doesn't matter, only class and name are checked
v1 = Variable(self.root, name="abc") v1 = Variable(self.root, name="abc")
v2 = Variable(self.root, name="abc") v2 = Variable(self.root, name="abc")
self.assertIsNot(v1, v2) self.assertIsNot(v1, v2)
self.assertEqual(v1, v2) self.assertEqual(v1, v2)
v3 = StringVar(self.root, name="abc") v3 = Variable(self.root, name="cba")
self.assertNotEqual(v1, v3) self.assertNotEqual(v1, v3)
v4 = StringVar(self.root, name="abc")
self.assertEqual(str(v1), str(v4))
self.assertNotEqual(v1, v4)
V = type('Variable', (), {}) V = type('Variable', (), {})
self.assertNotEqual(v1, V()) self.assertNotEqual(v1, V())
self.assertNotEqual(v1, object()) self.assertNotEqual(v1, object())
self.assertEqual(v1, ALWAYS_EQ) self.assertEqual(v1, ALWAYS_EQ)
root2 = tkinter.Tk()
self.addCleanup(root2.destroy)
v5 = Variable(root2, name="abc")
self.assertEqual(str(v1), str(v5))
self.assertNotEqual(v1, v5)
def test_invalid_name(self): def test_invalid_name(self):
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
Variable(self.root, name=123) Variable(self.root, name=123)

View File

@ -0,0 +1,3 @@
Fixed equality comparison of :class:`tkinter.Variable` and
:class:`tkinter.font.Font`. Objects which belong to different Tcl
interpreters are now always different, even if they have the same name.