mirror of
https://github.com/python/cpython.git
synced 2025-08-03 16:39:00 +00:00
bpo-37685: Fixed __eq__, __lt__ etc implementations in some classes. (GH-14952)
They now return NotImplemented for unsupported type of the other operand.
This commit is contained in:
parent
4c69be22df
commit
662db125cd
23 changed files with 1295 additions and 1150 deletions
|
@ -484,6 +484,8 @@ class Variable:
|
|||
Note: if the Variable's master matters to behavior
|
||||
also compare self._master == other._master
|
||||
"""
|
||||
if not isinstance(other, Variable):
|
||||
return NotImplemented
|
||||
return self.__class__.__name__ == other.__class__.__name__ \
|
||||
and self._name == other._name
|
||||
|
||||
|
|
|
@ -101,7 +101,9 @@ class Font:
|
|||
return self.name
|
||||
|
||||
def __eq__(self, other):
|
||||
return isinstance(other, Font) and self.name == other.name
|
||||
if not isinstance(other, Font):
|
||||
return NotImplemented
|
||||
return self.name == other.name
|
||||
|
||||
def __getitem__(self, key):
|
||||
return self.cget(key)
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import unittest
|
||||
import tkinter
|
||||
from tkinter import font
|
||||
from test.support import requires, run_unittest, gc_collect
|
||||
from test.support import requires, run_unittest, gc_collect, ALWAYS_EQ
|
||||
from tkinter.test.support import AbstractTkTest
|
||||
|
||||
requires('gui')
|
||||
|
@ -70,6 +70,7 @@ class FontTest(AbstractTkTest, unittest.TestCase):
|
|||
self.assertEqual(font1, font2)
|
||||
self.assertNotEqual(font1, font1.copy())
|
||||
self.assertNotEqual(font1, 0)
|
||||
self.assertEqual(font1, ALWAYS_EQ)
|
||||
|
||||
def test_measure(self):
|
||||
self.assertIsInstance(self.font.measure('abc'), int)
|
||||
|
|
|
@ -2,6 +2,7 @@ import unittest
|
|||
import gc
|
||||
from tkinter import (Variable, StringVar, IntVar, DoubleVar, BooleanVar, Tcl,
|
||||
TclError)
|
||||
from test.support import ALWAYS_EQ
|
||||
|
||||
|
||||
class Var(Variable):
|
||||
|
@ -59,11 +60,17 @@ class TestVariable(TestBase):
|
|||
# values doesn't matter, only class and name are checked
|
||||
v1 = Variable(self.root, name="abc")
|
||||
v2 = Variable(self.root, name="abc")
|
||||
self.assertIsNot(v1, v2)
|
||||
self.assertEqual(v1, v2)
|
||||
|
||||
v3 = Variable(self.root, name="abc")
|
||||
v4 = StringVar(self.root, name="abc")
|
||||
self.assertNotEqual(v3, v4)
|
||||
v3 = StringVar(self.root, name="abc")
|
||||
self.assertNotEqual(v1, v3)
|
||||
|
||||
V = type('Variable', (), {})
|
||||
self.assertNotEqual(v1, V())
|
||||
|
||||
self.assertNotEqual(v1, object())
|
||||
self.assertEqual(v1, ALWAYS_EQ)
|
||||
|
||||
def test_invalid_name(self):
|
||||
with self.assertRaises(TypeError):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue