MCPcopy Index your code
hub / github.com/python/cpython / test_unsafe_object_compare

Method test_unsafe_object_compare

Lib/test/test_sort.py:329–372  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

327 check_against_PyObject_RichCompareBool(self, L)
328
329 def test_unsafe_object_compare(self):
330
331 # This test is by ppperry. It ensures that unsafe_object_compare is
332 # verifying ms->key_richcompare == tp->richcompare before comparing.
333
334 class WackyComparator(int):
335 def __lt__(self, other):
336 elem.__class__ = WackyList2
337 return int.__lt__(self, other)
338
339 class WackyList1(list):
340 pass
341
342 class WackyList2(list):
343 def __lt__(self, other):
344 raise ValueError
345
346 L = [WackyList1([WackyComparator(i), i]) for i in range(10)]
347 elem = L[-1]
348 with self.assertRaises(ValueError):
349 L.sort()
350
351 L = [WackyList1([WackyComparator(i), i]) for i in range(10)]
352 elem = L[-1]
353 with self.assertRaises(ValueError):
354 [(x,) for x in L].sort()
355
356 # The following test is also by ppperry. It ensures that
357 # unsafe_object_compare handles Py_NotImplemented appropriately.
358 class PointlessComparator:
359 def __lt__(self, other):
360 return NotImplemented
361 L = [PointlessComparator(), PointlessComparator()]
362 self.assertRaises(TypeError, L.sort)
363 self.assertRaises(TypeError, [(x,) for x in L].sort)
364
365 # The following tests go through various types that would trigger
366 # ms->key_compare = unsafe_object_compare
367 lists = [list(range(100)) + [(1<<70)],
368 [str(x) for x in range(100)] + ['\uffff'],
369 [bytes(x) for x in range(100)],
370 [cmp_to_key(lambda x,y: x<y)(x) for x in range(100)]]
371 for L in lists:
372 check_against_PyObject_RichCompareBool(self, L)
373
374 def test_unsafe_latin_compare(self):
375 check_against_PyObject_RichCompareBool(self, [str(x) for

Callers

nothing calls this directly

Calls 9

cmp_to_keyFunction · 0.90
WackyList1Class · 0.85
WackyComparatorClass · 0.85
PointlessComparatorClass · 0.85
listClass · 0.85
strFunction · 0.85
assertRaisesMethod · 0.45
sortMethod · 0.45

Tested by

no test coverage detected