| 1 |
import sys |
| 2 |
import textwrap |
| 3 |
from test import list_tests |
| 4 |
from test.support import cpython_only |
| 5 |
from test.support.script_helper import assert_python_ok |
| 6 |
import pickle |
| 7 |
import unittest |
| 8 |
|
| 9 |
class ListTest(list_tests.CommonTest): |
| 10 |
type2test = list |
| 11 |
|
| 12 |
def test_basic(self): |
| 13 |
self.assertEqual(list([]), []) |
| 14 |
l0_3 = [0, 1, 2, 3] |
| 15 |
l0_3_bis = list(l0_3) |
| 16 |
self.assertEqual(l0_3, l0_3_bis) |
| 17 |
self.assertTrue(l0_3 is not l0_3_bis) |
| 18 |
self.assertEqual(list(()), []) |
| 19 |
self.assertEqual(list((0, 1, 2, 3)), [0, 1, 2, 3]) |
| 20 |
self.assertEqual(list(''), []) |
| 21 |
self.assertEqual(list('spam'), ['s', 'p', 'a', 'm']) |
| 22 |
self.assertEqual(list(x for x in range(10) if x % 2), |
| 23 |
[1, 3, 5, 7, 9]) |
| 24 |
|
| 25 |
if sys.maxsize == 0x7fffffff: |
| 26 |
# This test can currently only work on 32-bit machines. |
| 27 |
# XXX If/when PySequence_Length() returns a ssize_t, it should be |
| 28 |
# XXX re-enabled. |
| 29 |
# Verify clearing of bug #556025. |
| 30 |
# This assumes that the max data size (sys.maxint) == max |
| 31 |
# address size this also assumes that the address size is at |
| 32 |
# least 4 bytes with 8 byte addresses, the bug is not well |
| 33 |
# tested |
| 34 |
# |
| 35 |
# Note: This test is expected to SEGV under Cygwin 1.3.12 or |
| 36 |
# earlier due to a newlib bug. See the following mailing list |
| 37 |
# thread for the details: |
| 38 |
|
| 39 |
# http://sources.redhat.com/ml/newlib/2002/msg00369.html |
| 40 |
self.assertRaises(MemoryError, list, range(sys.maxsize // 2)) |
| 41 |
|
| 42 |
# This code used to segfault in Py2.4a3 |
| 43 |
x = [] |
| 44 |
x.extend(-y for y in x) |
| 45 |
self.assertEqual(x, []) |
| 46 |
|
| 47 |
def test_keyword_args(self): |
| 48 |
with self.assertRaisesRegex(TypeError, 'keyword argument'): |
| 49 |
list(sequence=[]) |
| 50 |
|
| 51 |
def test_keywords_in_subclass(self): |
| 52 |
class subclass(list): |
| 53 |
pass |
| 54 |
u = subclass([1, 2]) |
| 55 |
self.assertIs(type(u), subclass) |
| 56 |
self.assertEqual(list(u), [1, 2]) |
| 57 |
with self.assertRaises(TypeError): |
| 58 |
subclass(sequence=()) |
| 59 |
|
| 60 |
class subclass_with_init(list): |
| 61 |
def __init__(self, seq, newarg=None): |
| 62 |
super().__init__(seq) |
| 63 |
self.newarg = newarg |
| 64 |
u = subclass_with_init([1, 2], newarg=3) |
| 65 |
self.assertIs(type(u), subclass_with_init) |
| 66 |
self.assertEqual(list(u), [1, 2]) |
| 67 |
self.assertEqual(u.newarg, 3) |
| 68 |
|
| 69 |
class subclass_with_new(list): |
| 70 |
def __new__(cls, seq, newarg=None): |
| 71 |
self = super().__new__(cls, seq) |
| 72 |
self.newarg = newarg |
| 73 |
return self |
| 74 |
u = subclass_with_new([1, 2], newarg=3) |
| 75 |
self.assertIs(type(u), subclass_with_new) |
| 76 |
self.assertEqual(list(u), [1, 2]) |
| 77 |
self.assertEqual(u.newarg, 3) |
| 78 |
|
| 79 |
def test_truth(self): |
| 80 |
super().test_truth() |
| 81 |
self.assertTrue(not []) |
| 82 |
self.assertTrue([42]) |
| 83 |
|
| 84 |
def test_identity(self): |
| 85 |
self.assertTrue([] is not []) |
| 86 |
|
| 87 |
def test_len(self): |
| 88 |
super().test_len() |
| 89 |
self.assertEqual(len([]), 0) |
| 90 |
self.assertEqual(len([0]), 1) |
| 91 |
self.assertEqual(len([0, 1, 2]), 3) |
| 92 |
|
| 93 |
def test_overflow(self): |
| 94 |
lst = [4, 5, 6, 7] |
| 95 |
n = int((sys.maxsize*2+2) // len(lst)) |
| 96 |
def mul(a, b): return a * b |
| 97 |
def imul(a, b): a *= b |
| 98 |
self.assertRaises((MemoryError, OverflowError), mul, lst, n) |
| 99 |
self.assertRaises((MemoryError, OverflowError), imul, lst, n) |
| 100 |
|
| 101 |
def test_empty_slice(self): |
| 102 |
x = [] |
| 103 |
x[:] = x |
| 104 |
self.assertEqual(x, []) |
| 105 |
|
| 106 |
def test_list_resize_overflow(self): |
| 107 |
# gh-97616: test new_allocated * sizeof(PyObject*) overflow |
| 108 |
# check in list_resize() |
| 109 |
lst = [0] * 65 |
| 110 |
del lst[1:] |
| 111 |
self.assertEqual(len(lst), 1) |
| 112 |
|
| 113 |
size = sys.maxsize |
| 114 |
with self.assertRaises((MemoryError, OverflowError)): |
| 115 |
lst * size |
| 116 |
with self.assertRaises((MemoryError, OverflowError)): |
| 117 |
lst *= size |
| 118 |
|
| 119 |
def test_repr_mutate(self): |
| 120 |
class Obj: |
| 121 |
@staticmethod |
| 122 |
def __repr__(): |
| 123 |
try: |
| 124 |
mylist.pop() |
| 125 |
except IndexError: |
| 126 |
pass |
| 127 |
return 'obj' |
| 128 |
|
| 129 |
mylist = [Obj() for _ in range(5)] |
| 130 |
self.assertEqual(repr(mylist), '[obj, obj, obj]') |
| 131 |
|
| 132 |
def test_repr_large(self): |
| 133 |
# Check the repr of large list objects |
| 134 |
def check(n): |
| 135 |
l = [0] * n |
| 136 |
s = repr(l) |
| 137 |
self.assertEqual(s, |
| 138 |
'[' + ', '.join(['0'] * n) + ']') |
| 139 |
check(10) # check our checking code |
| 140 |
check(1000000) |
| 141 |
|
| 142 |
def test_iterator_pickle(self): |
| 143 |
orig = self.type2test([4, 5, 6, 7]) |
| 144 |
data = [10, 11, 12, 13, 14, 15] |
| 145 |
for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
| 146 |
# initial iterator |
| 147 |
itorig = iter(orig) |
| 148 |
d = pickle.dumps((itorig, orig), proto) |
| 149 |
it, a = pickle.loads(d) |
| 150 |
a[:] = data |
| 151 |
self.assertEqual(type(it), type(itorig)) |
| 152 |
self.assertEqual(list(it), data) |
| 153 |
|
| 154 |
# running iterator |
| 155 |
next(itorig) |
| 156 |
d = pickle.dumps((itorig, orig), proto) |
| 157 |
it, a = pickle.loads(d) |
| 158 |
a[:] = data |
| 159 |
self.assertEqual(type(it), type(itorig)) |
| 160 |
self.assertEqual(list(it), data[1:]) |
| 161 |
|
| 162 |
# empty iterator |
| 163 |
for i in range(1, len(orig)): |
| 164 |
next(itorig) |
| 165 |
d = pickle.dumps((itorig, orig), proto) |
| 166 |
it, a = pickle.loads(d) |
| 167 |
a[:] = data |
| 168 |
self.assertEqual(type(it), type(itorig)) |
| 169 |
self.assertEqual(list(it), data[len(orig):]) |
| 170 |
|
| 171 |
# exhausted iterator |
| 172 |
self.assertRaises(StopIteration, next, itorig) |
| 173 |
d = pickle.dumps((itorig, orig), proto) |
| 174 |
it, a = pickle.loads(d) |
| 175 |
a[:] = data |
| 176 |
self.assertEqual(list(it), []) |
| 177 |
|
| 178 |
def test_reversed_pickle(self): |
| 179 |
orig = self.type2test([4, 5, 6, 7]) |
| 180 |
data = [10, 11, 12, 13, 14, 15] |
| 181 |
for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
| 182 |
# initial iterator |
| 183 |
itorig = reversed(orig) |
| 184 |
d = pickle.dumps((itorig, orig), proto) |
| 185 |
it, a = pickle.loads(d) |
| 186 |
a[:] = data |
| 187 |
self.assertEqual(type(it), type(itorig)) |
| 188 |
self.assertEqual(list(it), data[len(orig)-1::-1]) |
| 189 |
|
| 190 |
# running iterator |
| 191 |
next(itorig) |
| 192 |
d = pickle.dumps((itorig, orig), proto) |
| 193 |
it, a = pickle.loads(d) |
| 194 |
a[:] = data |
| 195 |
self.assertEqual(type(it), type(itorig)) |
| 196 |
self.assertEqual(list(it), data[len(orig)-2::-1]) |
| 197 |
|
| 198 |
# empty iterator |
| 199 |
for i in range(1, len(orig)): |
| 200 |
next(itorig) |
| 201 |
d = pickle.dumps((itorig, orig), proto) |
| 202 |
it, a = pickle.loads(d) |
| 203 |
a[:] = data |
| 204 |
self.assertEqual(type(it), type(itorig)) |
| 205 |
self.assertEqual(list(it), []) |
| 206 |
|
| 207 |
# exhausted iterator |
| 208 |
self.assertRaises(StopIteration, next, itorig) |
| 209 |
d = pickle.dumps((itorig, orig), proto) |
| 210 |
it, a = pickle.loads(d) |
| 211 |
a[:] = data |
| 212 |
self.assertEqual(list(it), []) |
| 213 |
|
| 214 |
def test_step_overflow(self): |
| 215 |
a = [0, 1, 2, 3, 4] |
| 216 |
a[1::sys.maxsize] = [0] |
| 217 |
self.assertEqual(a[3::sys.maxsize], [3]) |
| 218 |
|
| 219 |
def test_no_comdat_folding(self): |
| 220 |
# Issue 8847: In the PGO build, the MSVC linker's COMDAT folding |
| 221 |
# optimization causes failures in code that relies on distinct |
| 222 |
# function addresses. |
| 223 |
class L(list): pass |
| 224 |
with self.assertRaises(TypeError): |
| 225 |
(3,) + L([1,2]) |
| 226 |
|
| 227 |
def test_equal_operator_modifying_operand(self): |
| 228 |
# test fix for seg fault reported in bpo-38588 part 2. |
| 229 |
class X: |
| 230 |
def __eq__(self,other) : |
| 231 |
list2.clear() |
| 232 |
return NotImplemented |
| 233 |
|
| 234 |
class Y: |
| 235 |
def __eq__(self, other): |
| 236 |
list1.clear() |
| 237 |
return NotImplemented |
| 238 |
|
| 239 |
class Z: |
| 240 |
def __eq__(self, other): |
| 241 |
list3.clear() |
| 242 |
return NotImplemented |
| 243 |
|
| 244 |
list1 = [X()] |
| 245 |
list2 = [Y()] |
| 246 |
self.assertTrue(list1 == list2) |
| 247 |
|
| 248 |
list3 = [Z()] |
| 249 |
list4 = [1] |
| 250 |
self.assertFalse(list3 == list4) |
| 251 |
|
| 252 |
def test_lt_operator_modifying_operand(self): |
| 253 |
# See gh-120298 |
| 254 |
class evil: |
| 255 |
def __lt__(self, other): |
| 256 |
other.clear() |
| 257 |
return NotImplemented |
| 258 |
|
| 259 |
a = [[evil()]] |
| 260 |
with self.assertRaises(TypeError): |
| 261 |
a[0] < a |
| 262 |
|
| 263 |
def test_list_index_modifing_operand(self): |
| 264 |
# See gh-120384 |
| 265 |
class evil: |
| 266 |
def __init__(self, lst): |
| 267 |
self.lst = lst |
| 268 |
def __iter__(self): |
| 269 |
yield from self.lst |
| 270 |
self.lst.clear() |
| 271 |
|
| 272 |
lst = list(range(5)) |
| 273 |
operand = evil(lst) |
| 274 |
with self.assertRaises(ValueError): |
| 275 |
lst[::-1] = operand |
| 276 |
|
| 277 |
@cpython_only |
| 278 |
def test_preallocation(self): |
| 279 |
iterable = [0] * 10 |
| 280 |
iter_size = sys.getsizeof(iterable) |
| 281 |
|
| 282 |
self.assertEqual(iter_size, sys.getsizeof(list([0] * 10))) |
| 283 |
self.assertEqual(iter_size, sys.getsizeof(list(range(10)))) |
| 284 |
|
| 285 |
def test_count_index_remove_crashes(self): |
| 286 |
# bpo-38610: The count(), index(), and remove() methods were not |
| 287 |
# holding strong references to list elements while calling |
| 288 |
# PyObject_RichCompareBool(). |
| 289 |
class X: |
| 290 |
def __eq__(self, other): |
| 291 |
lst.clear() |
| 292 |
return NotImplemented |
| 293 |
|
| 294 |
lst = [X()] |
| 295 |
with self.assertRaises(ValueError): |
| 296 |
lst.index(lst) |
| 297 |
|
| 298 |
class L(list): |
| 299 |
def __eq__(self, other): |
| 300 |
str(other) |
| 301 |
return NotImplemented |
| 302 |
|
| 303 |
lst = L([X()]) |
| 304 |
lst.count(lst) |
| 305 |
|
| 306 |
lst = L([X()]) |
| 307 |
with self.assertRaises(ValueError): |
| 308 |
lst.remove(lst) |
| 309 |
|
| 310 |
# bpo-39453: list.__contains__ was not holding strong references |
| 311 |
# to list elements while calling PyObject_RichCompareBool(). |
| 312 |
lst = [X(), X()] |
| 313 |
3 in lst |
| 314 |
lst = [X(), X()] |
| 315 |
X() in lst |
| 316 |
|
| 317 |
def test_tier2_invalidates_iterator(self): |
| 318 |
# GH-121012 |
| 319 |
for _ in range(100): |
| 320 |
a = [1, 2, 3] |
| 321 |
it = iter(a) |
| 322 |
for _ in it: |
| 323 |
pass |
| 324 |
a.append(4) |
| 325 |
self.assertEqual(list(it), []) |
| 326 |
|
| 327 |
def test_deopt_from_append_list(self): |
| 328 |
# gh-132011: it used to crash, because |
| 329 |
# of `CALL_LIST_APPEND` specialization failure. |
| 330 |
code = textwrap.dedent(""" |
| 331 |
l = [] |
| 332 |
def lappend(l, x, y): |
| 333 |
l.append((x, y)) |
| 334 |
for x in range(3): |
| 335 |
lappend(l, None, None) |
| 336 |
try: |
| 337 |
lappend(list, None, None) |
| 338 |
except TypeError: |
| 339 |
pass |
| 340 |
else: |
| 341 |
raise AssertionError |
| 342 |
""") |
| 343 |
|
| 344 |
rc, _, _ = assert_python_ok("-c", code) |
| 345 |
self.assertEqual(rc, 0) |
| 346 |
|
| 347 |
if __name__ == "__main__": |
| 348 |
unittest.main() |