Change string comparison so that it applies even when one (or both)

arguments are subclasses of str, as long as they don't override rich
comparison.
This commit is contained in:
Guido van Rossum 2001-09-24 16:51:54 +00:00
parent ff0e6d6ef5
commit bb77e6801e
2 changed files with 10 additions and 9 deletions

View File

@ -1555,7 +1555,7 @@ def inherits():
self._rev = self.__class__("".join(L)) self._rev = self.__class__("".join(L))
return self._rev return self._rev
s = madstring("abcdefghijklmnopqrstuvwxyz") s = madstring("abcdefghijklmnopqrstuvwxyz")
#XXX verify(s == "abcdefghijklmnopqrstuvwxyz") verify(s == "abcdefghijklmnopqrstuvwxyz")
verify(s.rev() == madstring("zyxwvutsrqponmlkjihgfedcba")) verify(s.rev() == madstring("zyxwvutsrqponmlkjihgfedcba"))
verify(s.rev().rev() == madstring("abcdefghijklmnopqrstuvwxyz")) verify(s.rev().rev() == madstring("abcdefghijklmnopqrstuvwxyz"))
for i in range(256): for i in range(256):
@ -1569,12 +1569,12 @@ def inherits():
base = "\x00" * 5 base = "\x00" * 5
s = madstring(base) s = madstring(base)
#XXX verify(s == base) verify(s == base)
verify(str(s) == base) verify(str(s) == base)
verify(str(s).__class__ is str) verify(str(s).__class__ is str)
verify(hash(s) == hash(base)) verify(hash(s) == hash(base))
#XXX verify({s: 1}[base] == 1) verify({s: 1}[base] == 1)
#XXX verify({base: 1}[s] == 1) verify({base: 1}[s] == 1)
verify((s + "").__class__ is str) verify((s + "").__class__ is str)
verify(s + "" == base) verify(s + "" == base)
verify(("" + s).__class__ is str) verify(("" + s).__class__ is str)
@ -1613,14 +1613,14 @@ def inherits():
verify(s.lower() == base) verify(s.lower() == base)
s = madstring("x y") s = madstring("x y")
#XXX verify(s == "x y") verify(s == "x y")
verify(intern(s).__class__ is str) verify(intern(s).__class__ is str)
verify(intern(s) is intern("x y")) verify(intern(s) is intern("x y"))
verify(intern(s) == "x y") verify(intern(s) == "x y")
i = intern("y x") i = intern("y x")
s = madstring("y x") s = madstring("y x")
#XXX verify(s == i) verify(s == i)
verify(intern(s).__class__ is str) verify(intern(s).__class__ is str)
verify(intern(s) is i) verify(intern(s) is i)

View File

@ -824,9 +824,10 @@ string_richcompare(PyStringObject *a, PyStringObject *b, int op)
int min_len; int min_len;
PyObject *result; PyObject *result;
/* One of the objects is a string object. Make sure the /* May sure both arguments use string comparison.
other one is one, too. */ This implies PyString_Check(a) && PyString_Check(b). */
if (a->ob_type != b->ob_type) { if (a->ob_type->tp_richcompare != (richcmpfunc)string_richcompare ||
b->ob_type->tp_richcompare != (richcmpfunc)string_richcompare) {
result = Py_NotImplemented; result = Py_NotImplemented;
goto out; goto out;
} }