Skip to content

Commit 5b18683

Browse files
CPython developersyouknowone
CPython developers
authored andcommitted
Update list/tuple tests from CPython 3.10.5
1 parent c737713 commit 5b18683

File tree

4 files changed

+100
-26
lines changed

4 files changed

+100
-26
lines changed

Lib/test/list_tests.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44

55
import sys
66
import os
7-
import unittest
87
from functools import cmp_to_key
98

109
from test import support, seq_tests
11-
from test.support import os_helper
10+
from test.support import ALWAYS_EQ, NEVER_EQ
1211

1312

1413
class CommonTest(seq_tests.CommonTest):
@@ -67,20 +66,6 @@ def test_repr_deep(self):
6766
a = self.type2test([a])
6867
self.assertRaises(RecursionError, repr, a)
6968

70-
def test_print(self):
71-
d = self.type2test(range(200))
72-
d.append(d)
73-
d.extend(range(200,400))
74-
d.append(d)
75-
d.append(400)
76-
try:
77-
with open(os_helper.TESTFN, "w") as fo:
78-
fo.write(str(d))
79-
with open(os_helper.TESTFN, "r") as fo:
80-
self.assertEqual(fo.read(), repr(d))
81-
finally:
82-
os.remove(os_helper.TESTFN)
83-
8469
def test_set_subscript(self):
8570
a = self.type2test(range(20))
8671
self.assertRaises(ValueError, a.__setitem__, slice(0, 10, 0), [1,2,3])
@@ -331,6 +316,20 @@ def test_remove(self):
331316

332317
self.assertRaises(TypeError, a.remove)
333318

319+
a = self.type2test([1, 2])
320+
self.assertRaises(ValueError, a.remove, NEVER_EQ)
321+
self.assertEqual(a, [1, 2])
322+
a.remove(ALWAYS_EQ)
323+
self.assertEqual(a, [2])
324+
a = self.type2test([ALWAYS_EQ])
325+
a.remove(1)
326+
self.assertEqual(a, [])
327+
a = self.type2test([ALWAYS_EQ])
328+
a.remove(NEVER_EQ)
329+
self.assertEqual(a, [])
330+
a = self.type2test([NEVER_EQ])
331+
self.assertRaises(ValueError, a.remove, ALWAYS_EQ)
332+
334333
class BadExc(Exception):
335334
pass
336335

Lib/test/seq_tests.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import sys
77
import pickle
88
from test import support
9+
from test.support import ALWAYS_EQ, NEVER_EQ
910

1011
# Various iterables
1112
# This is used for checking the constructor (here and in test_deque.py)
@@ -209,6 +210,7 @@ def test_getslice(self):
209210
a = self.type2test([0,1,2,3,4])
210211
self.assertEqual(a[ -pow(2,128): 3 ], self.type2test([0,1,2]))
211212
self.assertEqual(a[ 3: pow(2,145) ], self.type2test([3,4]))
213+
self.assertEqual(a[3::sys.maxsize], self.type2test([3]))
212214

213215
def test_contains(self):
214216
u = self.type2test([0, 1, 2])
@@ -220,15 +222,15 @@ def test_contains(self):
220222
self.assertRaises(TypeError, u.__contains__)
221223

222224
def test_contains_fake(self):
223-
class AllEq:
224-
# Sequences must use rich comparison against each item
225-
# (unless "is" is true, or an earlier item answered)
226-
# So instances of AllEq must be found in all non-empty sequences.
227-
def __eq__(self, other):
228-
return True
229-
__hash__ = None # Can't meet hash invariant requirements
230-
self.assertNotIn(AllEq(), self.type2test([]))
231-
self.assertIn(AllEq(), self.type2test([1]))
225+
# Sequences must use rich comparison against each item
226+
# (unless "is" is true, or an earlier item answered)
227+
# So ALWAYS_EQ must be found in all non-empty sequences.
228+
self.assertNotIn(ALWAYS_EQ, self.type2test([]))
229+
self.assertIn(ALWAYS_EQ, self.type2test([1]))
230+
self.assertIn(1, self.type2test([ALWAYS_EQ]))
231+
self.assertNotIn(NEVER_EQ, self.type2test([]))
232+
self.assertNotIn(ALWAYS_EQ, self.type2test([NEVER_EQ]))
233+
self.assertIn(NEVER_EQ, self.type2test([ALWAYS_EQ]))
232234

233235
def test_contains_order(self):
234236
# Sequences must test in-order. If a rich comparison has side
@@ -349,6 +351,11 @@ def test_count(self):
349351
self.assertEqual(a.count(1), 3)
350352
self.assertEqual(a.count(3), 0)
351353

354+
self.assertEqual(a.count(ALWAYS_EQ), 9)
355+
self.assertEqual(self.type2test([ALWAYS_EQ, ALWAYS_EQ]).count(1), 2)
356+
self.assertEqual(self.type2test([ALWAYS_EQ, ALWAYS_EQ]).count(NEVER_EQ), 2)
357+
self.assertEqual(self.type2test([NEVER_EQ, NEVER_EQ]).count(ALWAYS_EQ), 0)
358+
352359
self.assertRaises(TypeError, a.count)
353360

354361
class BadExc(Exception):
@@ -377,6 +384,11 @@ def test_index(self):
377384
self.assertEqual(u.index(0, 3, 4), 3)
378385
self.assertRaises(ValueError, u.index, 2, 0, -10)
379386

387+
self.assertEqual(u.index(ALWAYS_EQ), 0)
388+
self.assertEqual(self.type2test([ALWAYS_EQ, ALWAYS_EQ]).index(1), 0)
389+
self.assertEqual(self.type2test([ALWAYS_EQ, ALWAYS_EQ]).index(NEVER_EQ), 0)
390+
self.assertRaises(ValueError, self.type2test([NEVER_EQ, NEVER_EQ]).index, ALWAYS_EQ)
391+
380392
self.assertRaises(TypeError, u.index)
381393

382394
class BadExc(Exception):

Lib/test/test_list.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,11 @@ def test_reversed_pickle(self):
151151
a[:] = data
152152
self.assertEqual(list(it), [])
153153

154+
def test_step_overflow(self):
155+
a = [0, 1, 2, 3, 4]
156+
a[1::sys.maxsize] = [0]
157+
self.assertEqual(a[3::sys.maxsize], [3])
158+
154159
def test_no_comdat_folding(self):
155160
# Issue 8847: In the PGO build, the MSVC linker's COMDAT folding
156161
# optimization causes failures in code that relies on distinct
@@ -159,6 +164,31 @@ class L(list): pass
159164
with self.assertRaises(TypeError):
160165
(3,) + L([1,2])
161166

167+
def test_equal_operator_modifying_operand(self):
168+
# test fix for seg fault reported in bpo-38588 part 2.
169+
class X:
170+
def __eq__(self,other) :
171+
list2.clear()
172+
return NotImplemented
173+
174+
class Y:
175+
def __eq__(self, other):
176+
list1.clear()
177+
return NotImplemented
178+
179+
class Z:
180+
def __eq__(self, other):
181+
list3.clear()
182+
return NotImplemented
183+
184+
list1 = [X()]
185+
list2 = [Y()]
186+
self.assertTrue(list1 == list2)
187+
188+
list3 = [Z()]
189+
list4 = [1]
190+
self.assertFalse(list3 == list4)
191+
162192
@cpython_only
163193
def test_preallocation(self):
164194
iterable = [0] * 10
@@ -167,5 +197,38 @@ def test_preallocation(self):
167197
self.assertEqual(iter_size, sys.getsizeof(list([0] * 10)))
168198
self.assertEqual(iter_size, sys.getsizeof(list(range(10))))
169199

200+
def test_count_index_remove_crashes(self):
201+
# bpo-38610: The count(), index(), and remove() methods were not
202+
# holding strong references to list elements while calling
203+
# PyObject_RichCompareBool().
204+
class X:
205+
def __eq__(self, other):
206+
lst.clear()
207+
return NotImplemented
208+
209+
lst = [X()]
210+
with self.assertRaises(ValueError):
211+
lst.index(lst)
212+
213+
class L(list):
214+
def __eq__(self, other):
215+
str(other)
216+
return NotImplemented
217+
218+
lst = L([X()])
219+
lst.count(lst)
220+
221+
lst = L([X()])
222+
with self.assertRaises(ValueError):
223+
lst.remove(lst)
224+
225+
# bpo-39453: list.__contains__ was not holding strong references
226+
# to list elements while calling PyObject_RichCompareBool().
227+
lst = [X(), X()]
228+
3 in lst
229+
lst = [X(), X()]
230+
X() in lst
231+
232+
170233
if __name__ == "__main__":
171234
unittest.main()

Lib/test/test_tuple.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from test import support, seq_tests
22
import unittest
33

4-
# import gc
4+
import gc
55
import pickle
66

77
# For tuple hashes, we normally only run a test to ensure that we get

0 commit comments

Comments
 (0)