annotate Lib/test/test_sort.py @ 87690:66c1c9f32567 default tip

Add use_weakref option to functools.lru_cache The use_weakref option makes lru_cache create weak references to the function arguments when creating the cache key, which prevents them from staying in the memory forever even after all other references to them are gone. This is a proof of concept for http://bugs.python.org/issue19859
author Radomir Dopieralski <devel@sheep.art.pl>
date Tue, 03 Dec 2013 16:02:48 +0100
parents fefb7d355361
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 45061
diff changeset
1 from test import support
24457
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2 import random
35937
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
3 import sys
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
4 import unittest
71550
fefb7d355361 Remove duplicates of cmp_to_key (#12542, reviewed by Raymond Hettinger)
Éric Araujo <merwok@netwok.org>
parents: 69836
diff changeset
5 from functools import cmp_to_key
24457
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
6
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 45061
diff changeset
7 verbose = support.verbose
24457
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
8 nerrors = 0
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
9
45061
b9ee31a26f38 Issue #1771: Remove cmp parameter from list.sort() and builtin.sorted().
Raymond Hettinger <python@rcn.com>
parents: 41717
diff changeset
10
24457
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
11 def check(tag, expected, raw, compare=None):
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
12 global nerrors
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
13
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
14 if verbose:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 39644
diff changeset
15 print(" checking", tag)
24457
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
16
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
17 orig = raw[:] # save input in case of error
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
18 if compare:
71550
fefb7d355361 Remove duplicates of cmp_to_key (#12542, reviewed by Raymond Hettinger)
Éric Araujo <merwok@netwok.org>
parents: 69836
diff changeset
19 raw.sort(key=cmp_to_key(compare))
24457
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
20 else:
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
21 raw.sort()
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
22
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
23 if len(expected) != len(raw):
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 39644
diff changeset
24 print("error in", tag)
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 39644
diff changeset
25 print("length mismatch;", len(expected), len(raw))
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 39644
diff changeset
26 print(expected)
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 39644
diff changeset
27 print(orig)
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 39644
diff changeset
28 print(raw)
24457
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
29 nerrors += 1
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
30 return
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
31
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
32 for i, good in enumerate(expected):
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
33 maybe = raw[i]
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
34 if good is not maybe:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 39644
diff changeset
35 print("error in", tag)
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 39644
diff changeset
36 print("out of order at index", i, good, maybe)
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 39644
diff changeset
37 print(expected)
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 39644
diff changeset
38 print(orig)
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 39644
diff changeset
39 print(raw)
24457
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
40 nerrors += 1
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
41 return
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
42
35937
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
43 class TestBase(unittest.TestCase):
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
44 def testStressfully(self):
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
45 # Try a variety of sizes at and around powers of 2, and at powers of 10.
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
46 sizes = [0]
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
47 for power in range(1, 10):
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
48 n = 2 ** power
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
49 sizes.extend(range(n-1, n+2))
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
50 sizes.extend([10, 100, 1000])
24457
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
51
35937
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
52 class Complains(object):
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
53 maybe_complain = True
24457
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
54
35937
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
55 def __init__(self, i):
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
56 self.i = i
24457
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
57
35937
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
58 def __lt__(self, other):
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
59 if Complains.maybe_complain and random.random() < 0.001:
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
60 if verbose:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 39644
diff changeset
61 print(" complaining at", self, other)
35937
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
62 raise RuntimeError
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
63 return self.i < other.i
24457
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
64
35937
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
65 def __repr__(self):
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
66 return "Complains(%d)" % self.i
24457
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
67
35937
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
68 class Stable(object):
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
69 def __init__(self, key, i):
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
70 self.key = key
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
71 self.index = i
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
72
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 35937
diff changeset
73 def __lt__(self, other):
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 35937
diff changeset
74 return self.key < other.key
24457
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
75
35937
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
76 def __repr__(self):
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
77 return "Stable(%d, %d)" % (self.key, self.index)
24457
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
78
35937
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
79 for n in sizes:
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
80 x = list(range(n))
35937
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
81 if verbose:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 39644
diff changeset
82 print("Testing size", n)
24457
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
83
35937
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
84 s = x[:]
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
85 check("identity", x, s)
24457
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
86
35937
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
87 s = x[:]
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
88 s.reverse()
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
89 check("reversed", x, s)
24457
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
90
35937
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
91 s = x[:]
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
92 random.shuffle(s)
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
93 check("random permutation", x, s)
24457
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
94
35937
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
95 y = x[:]
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
96 y.reverse()
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
97 s = x[:]
51122
adbd77a90bde Merged revisions 69025 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 47201
diff changeset
98 check("reversed via function", y, s, lambda a, b: (b>a)-(b<a))
24457
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
99
35937
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
100 if verbose:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 39644
diff changeset
101 print(" Checking against an insane comparison function.")
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 39644
diff changeset
102 print(" If the implementation isn't careful, this may segfault.")
35937
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
103 s = x[:]
71550
fefb7d355361 Remove duplicates of cmp_to_key (#12542, reviewed by Raymond Hettinger)
Éric Araujo <merwok@netwok.org>
parents: 69836
diff changeset
104 s.sort(key=cmp_to_key(lambda a, b: int(random.random() * 3) - 1))
35937
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
105 check("an insane function left some permutation", x, s)
24457
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
106
69836
52fb7dc721ed #11335: Fix memory leak after key function failure in sort
Daniel Stutzbach <daniel.stutzbach@google.com>
parents: 51122
diff changeset
107 if len(x) >= 2:
52fb7dc721ed #11335: Fix memory leak after key function failure in sort
Daniel Stutzbach <daniel.stutzbach@google.com>
parents: 51122
diff changeset
108 def bad_key(x):
52fb7dc721ed #11335: Fix memory leak after key function failure in sort
Daniel Stutzbach <daniel.stutzbach@google.com>
parents: 51122
diff changeset
109 raise RuntimeError
52fb7dc721ed #11335: Fix memory leak after key function failure in sort
Daniel Stutzbach <daniel.stutzbach@google.com>
parents: 51122
diff changeset
110 s = x[:]
52fb7dc721ed #11335: Fix memory leak after key function failure in sort
Daniel Stutzbach <daniel.stutzbach@google.com>
parents: 51122
diff changeset
111 self.assertRaises(RuntimeError, s.sort, key=bad_key)
52fb7dc721ed #11335: Fix memory leak after key function failure in sort
Daniel Stutzbach <daniel.stutzbach@google.com>
parents: 51122
diff changeset
112
35937
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
113 x = [Complains(i) for i in x]
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
114 s = x[:]
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
115 random.shuffle(s)
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
116 Complains.maybe_complain = True
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
117 it_complained = False
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
118 try:
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
119 s.sort()
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
120 except RuntimeError:
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
121 it_complained = True
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
122 if it_complained:
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
123 Complains.maybe_complain = False
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
124 check("exception during sort left some permutation", x, s)
24457
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
125
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
126 s = [Stable(random.randrange(10), i) for i in range(n)]
35937
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
127 augmented = [(e, e.index) for e in s]
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
128 augmented.sort() # forced stable because ties broken by index
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
129 x = [e for e, i in augmented] # a stable sort of s
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
130 check("stability", x, s)
30697
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
131
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
132 #==============================================================================
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
133
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
134 class TestBugs(unittest.TestCase):
26161
dd10d97d329a SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents: 24514
diff changeset
135
30697
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
136 def test_bug453523(self):
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
137 # bug 453523 -- list.sort() crasher.
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
138 # If this fails, the most likely outcome is a core dump.
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
139 # Mutations during a list sort should raise a ValueError.
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
140
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
141 class C:
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
142 def __lt__(self, other):
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
143 if L and random.random() < 0.75:
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
144 L.pop()
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
145 else:
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
146 L.append(3)
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
147 return random.random() < 0.5
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
148
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
149 L = [C() for i in range(50)]
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
150 self.assertRaises(ValueError, L.sort)
26161
dd10d97d329a SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents: 24514
diff changeset
151
32858
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 31389
diff changeset
152 def test_undetected_mutation(self):
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 31389
diff changeset
153 # Python 2.4a1 did not always detect mutation
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 31389
diff changeset
154 memorywaster = []
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 31389
diff changeset
155 for i in range(20):
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 31389
diff changeset
156 def mutating_cmp(x, y):
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 31389
diff changeset
157 L.append(3)
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 31389
diff changeset
158 L.pop()
51122
adbd77a90bde Merged revisions 69025 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 47201
diff changeset
159 return (x > y) - (x < y)
32858
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 31389
diff changeset
160 L = [1,2]
71550
fefb7d355361 Remove duplicates of cmp_to_key (#12542, reviewed by Raymond Hettinger)
Éric Araujo <merwok@netwok.org>
parents: 69836
diff changeset
161 self.assertRaises(ValueError, L.sort, key=cmp_to_key(mutating_cmp))
32858
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 31389
diff changeset
162 def mutating_cmp(x, y):
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 31389
diff changeset
163 L.append(3)
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 31389
diff changeset
164 del L[:]
51122
adbd77a90bde Merged revisions 69025 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 47201
diff changeset
165 return (x > y) - (x < y)
71550
fefb7d355361 Remove duplicates of cmp_to_key (#12542, reviewed by Raymond Hettinger)
Éric Araujo <merwok@netwok.org>
parents: 69836
diff changeset
166 self.assertRaises(ValueError, L.sort, key=cmp_to_key(mutating_cmp))
32858
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 31389
diff changeset
167 memorywaster = [memorywaster]
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 31389
diff changeset
168
30697
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
169 #==============================================================================
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
170
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
171 class TestDecorateSortUndecorate(unittest.TestCase):
26161
dd10d97d329a SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents: 24514
diff changeset
172
30697
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
173 def test_decorated(self):
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
174 data = 'The quick Brown fox Jumped over The lazy Dog'.split()
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
175 copy = data[:]
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
176 random.shuffle(data)
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
177 data.sort(key=str.lower)
51122
adbd77a90bde Merged revisions 69025 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 47201
diff changeset
178 def my_cmp(x, y):
adbd77a90bde Merged revisions 69025 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 47201
diff changeset
179 xlower, ylower = x.lower(), y.lower()
adbd77a90bde Merged revisions 69025 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 47201
diff changeset
180 return (xlower > ylower) - (xlower < ylower)
71550
fefb7d355361 Remove duplicates of cmp_to_key (#12542, reviewed by Raymond Hettinger)
Éric Araujo <merwok@netwok.org>
parents: 69836
diff changeset
181 copy.sort(key=cmp_to_key(my_cmp))
30697
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
182
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
183 def test_baddecorator(self):
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
184 data = 'The quick Brown fox Jumped over The lazy Dog'.split()
45061
b9ee31a26f38 Issue #1771: Remove cmp parameter from list.sort() and builtin.sorted().
Raymond Hettinger <python@rcn.com>
parents: 41717
diff changeset
185 self.assertRaises(TypeError, data.sort, key=lambda x,y: 0)
30697
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
186
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
187 def test_stability(self):
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
188 data = [(random.randrange(100), i) for i in range(200)]
30697
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
189 copy = data[:]
41717
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41642
diff changeset
190 data.sort(key=lambda t: t[0]) # sort on the random first field
30697
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
191 copy.sort() # sort using both fields
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
192 self.assertEqual(data, copy) # should get the same result
26161
dd10d97d329a SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents: 24514
diff changeset
193
31041
6f01700c1808 Make sure the list.sort's decorate step unwinds itself before returning
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
194 def test_key_with_exception(self):
6f01700c1808 Make sure the list.sort's decorate step unwinds itself before returning
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
195 # Verify that the wrapper has been removed
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
196 data = list(range(-2, 2))
31041
6f01700c1808 Make sure the list.sort's decorate step unwinds itself before returning
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
197 dup = data[:]
45061
b9ee31a26f38 Issue #1771: Remove cmp parameter from list.sort() and builtin.sorted().
Raymond Hettinger <python@rcn.com>
parents: 41717
diff changeset
198 self.assertRaises(ZeroDivisionError, data.sort, key=lambda x: 1/x)
31041
6f01700c1808 Make sure the list.sort's decorate step unwinds itself before returning
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
199 self.assertEqual(data, dup)
6f01700c1808 Make sure the list.sort's decorate step unwinds itself before returning
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
200
31093
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
201 def test_key_with_mutation(self):
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
202 data = list(range(10))
31093
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
203 def k(x):
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
204 del data[:]
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
205 data[:] = range(20)
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
206 return x
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
207 self.assertRaises(ValueError, data.sort, key=k)
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
208
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
209 def test_key_with_mutating_del(self):
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
210 data = list(range(10))
31093
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
211 class SortKiller(object):
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
212 def __init__(self, x):
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
213 pass
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
214 def __del__(self):
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
215 del data[:]
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
216 data[:] = range(20)
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 35937
diff changeset
217 def __lt__(self, other):
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 35937
diff changeset
218 return id(self) < id(other)
31093
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
219 self.assertRaises(ValueError, data.sort, key=SortKiller)
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
220
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
221 def test_key_with_mutating_del_and_exception(self):
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
222 data = list(range(10))
31093
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
223 ## dup = data[:]
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
224 class SortKiller(object):
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
225 def __init__(self, x):
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
226 if x > 2:
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
227 raise RuntimeError
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
228 def __del__(self):
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
229 del data[:]
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
230 data[:] = list(range(20))
31093
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
231 self.assertRaises(RuntimeError, data.sort, key=SortKiller)
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
232 ## major honking subtlety: we *can't* do:
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
233 ##
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
234 ## self.assertEqual(data, dup)
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
235 ##
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
236 ## because there is a reference to a SortKiller in the
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
237 ## traceback and by the time it dies we're outside the call to
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
238 ## .sort() and so the list protection gimmicks are out of
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
239 ## date (this cost some brain cells to figure out...).
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
240
30697
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
241 def test_reverse(self):
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
242 data = list(range(100))
30697
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
243 random.shuffle(data)
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
244 data.sort(reverse=True)
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
245 self.assertEqual(data, list(range(99,-1,-1)))
27036
41f7726e3e51 Allow list sort's comparison function to explicitly be None. See SF patch
Skip Montanaro <skip@pobox.com>
parents: 26161
diff changeset
246
30697
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
247 def test_reverse_stability(self):
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
248 data = [(random.randrange(100), i) for i in range(200)]
30697
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
249 copy1 = data[:]
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
250 copy2 = data[:]
51122
adbd77a90bde Merged revisions 69025 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 47201
diff changeset
251 def my_cmp(x, y):
adbd77a90bde Merged revisions 69025 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 47201
diff changeset
252 x0, y0 = x[0], y[0]
adbd77a90bde Merged revisions 69025 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 47201
diff changeset
253 return (x0 > y0) - (x0 < y0)
adbd77a90bde Merged revisions 69025 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 47201
diff changeset
254 def my_cmp_reversed(x, y):
adbd77a90bde Merged revisions 69025 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 47201
diff changeset
255 x0, y0 = x[0], y[0]
adbd77a90bde Merged revisions 69025 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 47201
diff changeset
256 return (y0 > x0) - (y0 < x0)
71550
fefb7d355361 Remove duplicates of cmp_to_key (#12542, reviewed by Raymond Hettinger)
Éric Araujo <merwok@netwok.org>
parents: 69836
diff changeset
257 data.sort(key=cmp_to_key(my_cmp), reverse=True)
fefb7d355361 Remove duplicates of cmp_to_key (#12542, reviewed by Raymond Hettinger)
Éric Araujo <merwok@netwok.org>
parents: 69836
diff changeset
258 copy1.sort(key=cmp_to_key(my_cmp_reversed))
30697
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
259 self.assertEqual(data, copy1)
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
260 copy2.sort(key=lambda x: x[0], reverse=True)
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
261 self.assertEqual(data, copy2)
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
262
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
263 #==============================================================================
27036
41f7726e3e51 Allow list sort's comparison function to explicitly be None. See SF patch
Skip Montanaro <skip@pobox.com>
parents: 26161
diff changeset
264
30697
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
265 def test_main(verbose=None):
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
266 test_classes = (
35937
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
267 TestBase,
30697
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
268 TestDecorateSortUndecorate,
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
269 TestBugs,
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
270 )
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
271
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 45061
diff changeset
272 support.run_unittest(*test_classes)
27036
41f7726e3e51 Allow list sort's comparison function to explicitly be None. See SF patch
Skip Montanaro <skip@pobox.com>
parents: 26161
diff changeset
273
30697
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
274 # verify reference counting
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
275 if verbose and hasattr(sys, "gettotalrefcount"):
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
276 import gc
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
277 counts = [None] * 5
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
278 for i in range(len(counts)):
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 45061
diff changeset
279 support.run_unittest(*test_classes)
30697
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
280 gc.collect()
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
281 counts[i] = sys.gettotalrefcount()
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 39644
diff changeset
282 print(counts)
30697
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
283
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
284 if __name__ == "__main__":
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
285 test_main(verbose=True)