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

Function check_against_PyObject_RichCompareBool

Lib/test/test_sort.py:284–310  ·  view source on GitHub ↗
(self, L)

Source from the content-addressed store, hash-verified

282
283#==============================================================================
284def check_against_PyObject_RichCompareBool(self, L):
285 ## The idea here is to exploit the fact that unsafe_tuple_compare uses
286 ## PyObject_RichCompareBool for the second elements of tuples. So we have,
287 ## for (most) L, sorted(L) == [y[1] for y in sorted([(0,x) for x in L])]
288 ## This will work as long as __eq__ => not __lt__ for all the objects in L,
289 ## which holds for all the types used below.
290 ##
291 ## Testing this way ensures that the optimized implementation remains consistent
292 ## with the naive implementation, even if changes are made to any of the
293 ## richcompares.
294 ##
295 ## This function tests sorting for three lists (it randomly shuffles each one):
296 ## 1. L
297 ## 2. [(x,) for x in L]
298 ## 3. [((x,),) for x in L]
299
300 random.seed(0)
301 random.shuffle(L)
302 L_1 = L[:]
303 L_2 = [(x,) for x in L]
304 L_3 = [((x,),) for x in L]
305 for L in [L_1, L_2, L_3]:
306 optimized = sorted(L)
307 reference = [y[1] for y in sorted([(0,x) for x in L])]
308 for (opt, ref) in zip(optimized, reference):
309 self.assertIs(opt, ref)
310 #note: not assertEqual! We want to ensure *identical* behavior.
311
312class TestOptimizedCompares(unittest.TestCase):
313 def test_safe_object_compare(self):

Calls 3

shuffleMethod · 0.80
seedMethod · 0.45
assertIsMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…