mirror of
https://github.com/python/cpython.git
synced 2024-12-29 03:34:57 +08:00
47b9ff6ba1
*ordering* between objects; there is only a default equality test (defined by an object being equal to itself only). Read the comment in object.c. The current implementation never uses a three-way comparison to compute a rich comparison, but it does use a rich comparison to compute a three-way comparison. I'm not quite done ripping out all the calls to PyObject_Compare/Cmp, or replacing tp_compare implementations with tp_richcompare implementations; but much of that has happened (to make most unit tests pass). The following tests still fail, because I need help deciding or understanding: test_codeop -- depends on comparing code objects test_datetime -- need Tim Peters' opinion test_marshal -- depends on comparing code objects test_mutants -- need help understanding it The problem with test_codeop and test_marshal is this: these tests compare two different code objects and expect them to be equal. Is that still a feature we'd like to support? I've temporarily removed the comparison and hash code from code objects, so they use the default (equality by pointer only) comparison. For the other two tests, run them to see for yourself. (There may be more failing test with "-u all".) A general problem with getting lots of these tests to pass is the reality that for object types that have a natural total ordering, implementing __cmp__ is much more convenient than implementing __eq__, __ne__, __lt__, and so on. Should we go back to allowing __cmp__ to provide a total ordering? Should we provide some other way to implement rich comparison with a single method override? Alex proposed a __key__() method; I've considered a __richcmp__() method. Or perhaps __cmp__() just shouldn't be killed off...
78 lines
1.8 KiB
Python
78 lines
1.8 KiB
Python
# Python test set -- part 3, built-in operations.
|
|
|
|
|
|
print '3. Operations'
|
|
print 'XXX Mostly not yet implemented'
|
|
|
|
|
|
print '3.1 Dictionary lookups fail if __cmp__() raises an exception'
|
|
|
|
class BadDictKey:
|
|
|
|
def __hash__(self):
|
|
return hash(self.__class__)
|
|
|
|
def __eq__(self, other):
|
|
if isinstance(other, self.__class__):
|
|
print "raising error"
|
|
raise RuntimeError, "gotcha"
|
|
return other
|
|
|
|
d = {}
|
|
x1 = BadDictKey()
|
|
x2 = BadDictKey()
|
|
d[x1] = 1
|
|
for stmt in ['d[x2] = 2',
|
|
'z = d[x2]',
|
|
'x2 in d',
|
|
'd.get(x2)',
|
|
'd.setdefault(x2, 42)',
|
|
'd.pop(x2)',
|
|
'd.update({x2: 2})']:
|
|
try:
|
|
exec stmt
|
|
except RuntimeError:
|
|
print "%s: caught the RuntimeError outside" % (stmt,)
|
|
else:
|
|
print "%s: No exception passed through!" % (stmt,) # old CPython behavior
|
|
|
|
|
|
# Dict resizing bug, found by Jack Jansen in 2.2 CVS development.
|
|
# This version got an assert failure in debug build, infinite loop in
|
|
# release build. Unfortunately, provoking this kind of stuff requires
|
|
# a mix of inserts and deletes hitting exactly the right hash codes in
|
|
# exactly the right order, and I can't think of a randomized approach
|
|
# that would be *likely* to hit a failing case in reasonable time.
|
|
|
|
d = {}
|
|
for i in range(5):
|
|
d[i] = i
|
|
for i in range(5):
|
|
del d[i]
|
|
for i in range(5, 9): # i==8 was the problem
|
|
d[i] = i
|
|
|
|
|
|
# Another dict resizing bug (SF bug #1456209).
|
|
# This caused Segmentation faults or Illegal instructions.
|
|
|
|
class X(object):
|
|
def __hash__(self):
|
|
return 5
|
|
def __eq__(self, other):
|
|
if resizing:
|
|
d.clear()
|
|
return False
|
|
d = {}
|
|
resizing = False
|
|
d[X()] = 1
|
|
d[X()] = 2
|
|
d[X()] = 3
|
|
d[X()] = 4
|
|
d[X()] = 5
|
|
# now trigger a resize
|
|
resizing = True
|
|
d[9] = 6
|
|
|
|
print 'resize bugs not triggered.'
|