Skip to content

Commit 4fe4ff4

Browse files
authored
Update test_{list,listcomps}.py from 3.13.5 (#5965)
1 parent 5ab64b7 commit 4fe4ff4

File tree

2 files changed

+63
-5
lines changed

2 files changed

+63
-5
lines changed

Lib/test/test_list.py

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import sys
2+
import textwrap
23
from test import list_tests
34
from test.support import cpython_only
5+
from test.support.script_helper import assert_python_ok
46
import pickle
57
import unittest
68

@@ -98,8 +100,13 @@ def imul(a, b): a *= b
98100
self.assertRaises((MemoryError, OverflowError), mul, lst, n)
99101
self.assertRaises((MemoryError, OverflowError), imul, lst, n)
100102

103+
def test_empty_slice(self):
104+
x = []
105+
x[:] = x
106+
self.assertEqual(x, [])
107+
101108
# TODO: RUSTPYTHON
102-
@unittest.skip("Crashes on windows debug build")
109+
@unittest.skip("TODO: RUSTPYTHON crash")
103110
def test_list_resize_overflow(self):
104111
# gh-97616: test new_allocated * sizeof(PyObject*) overflow
105112
# check in list_resize()
@@ -113,13 +120,28 @@ def test_list_resize_overflow(self):
113120
with self.assertRaises((MemoryError, OverflowError)):
114121
lst *= size
115122

123+
# TODO: RUSTPYTHON
124+
@unittest.skip("TODO: RUSTPYTHON hangs")
125+
def test_repr_mutate(self):
126+
class Obj:
127+
@staticmethod
128+
def __repr__():
129+
try:
130+
mylist.pop()
131+
except IndexError:
132+
pass
133+
return 'obj'
134+
135+
mylist = [Obj() for _ in range(5)]
136+
self.assertEqual(repr(mylist), '[obj, obj, obj]')
137+
116138
def test_repr_large(self):
117139
# Check the repr of large list objects
118140
def check(n):
119141
l = [0] * n
120142
s = repr(l)
121143
self.assertEqual(s,
122-
'[' + ', '.join(['0'] * n) + ']')
144+
'[' + ', '.join(['0'] * n) + ']')
123145
check(10) # check our checking code
124146
check(1000000)
125147

@@ -302,6 +324,35 @@ def __eq__(self, other):
302324
lst = [X(), X()]
303325
X() in lst
304326

327+
def test_tier2_invalidates_iterator(self):
328+
# GH-121012
329+
for _ in range(100):
330+
a = [1, 2, 3]
331+
it = iter(a)
332+
for _ in it:
333+
pass
334+
a.append(4)
335+
self.assertEqual(list(it), [])
336+
337+
def test_deopt_from_append_list(self):
338+
# gh-132011: it used to crash, because
339+
# of `CALL_LIST_APPEND` specialization failure.
340+
code = textwrap.dedent("""
341+
l = []
342+
def lappend(l, x, y):
343+
l.append((x, y))
344+
for x in range(3):
345+
lappend(l, None, None)
346+
try:
347+
lappend(list, None, None)
348+
except TypeError:
349+
pass
350+
else:
351+
raise AssertionError
352+
""")
353+
354+
rc, _, _ = assert_python_ok("-c", code)
355+
self.assertEqual(rc, 0)
305356

306357
if __name__ == "__main__":
307358
unittest.main()

Lib/test/test_listcomps.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ def test_references___class___defined(self):
177177
res = [__class__ for x in [1]]
178178
"""
179179
self._check_in_scopes(
180-
code, outputs={"res": [2]}, scopes=["module", "function"])
180+
code, outputs={"res": [2]}, scopes=["module", "function"])
181181
self._check_in_scopes(code, raises=NameError, scopes=["class"])
182182

183183
def test_references___class___enclosing(self):
@@ -648,11 +648,18 @@ def test_exception_in_post_comp_call(self):
648648
"""
649649
self._check_in_scopes(code, {"value": [1, None]})
650650

651+
# TODO: RUSTPYTHON
652+
@unittest.expectedFailure
651653
def test_frame_locals(self):
652654
code = """
653-
val = [sys._getframe().f_locals for a in [0]][0]["a"]
655+
val = "a" in [sys._getframe().f_locals for a in [0]][0]
654656
"""
655657
import sys
658+
self._check_in_scopes(code, {"val": False}, ns={"sys": sys})
659+
660+
code = """
661+
val = [sys._getframe().f_locals["a"] for a in [0]][0]
662+
"""
656663
self._check_in_scopes(code, {"val": 0}, ns={"sys": sys})
657664

658665
def _recursive_replace(self, maybe_code):
@@ -736,7 +743,7 @@ def iter_raises():
736743
for func, expected in [(init_raises, "BrokenIter(init_raises=True)"),
737744
(next_raises, "BrokenIter(next_raises=True)"),
738745
(iter_raises, "BrokenIter(iter_raises=True)"),
739-
]:
746+
]:
740747
with self.subTest(func):
741748
exc = func()
742749
f = traceback.extract_tb(exc.__traceback__)[0]

0 commit comments

Comments
 (0)