Update test_{list,listcomps}.py from 3.13.5 (#5965)

This commit is contained in:
Shahar Naveh
2025-07-14 08:24:00 +03:00
committed by GitHub
parent 5ab64b7002
commit 4fe4ff4f99
2 changed files with 63 additions and 5 deletions

55
Lib/test/test_list.py vendored
View File

@@ -1,6 +1,8 @@
import sys
import textwrap
from test import list_tests
from test.support import cpython_only
from test.support.script_helper import assert_python_ok
import pickle
import unittest
@@ -98,8 +100,13 @@ class ListTest(list_tests.CommonTest):
self.assertRaises((MemoryError, OverflowError), mul, lst, n)
self.assertRaises((MemoryError, OverflowError), imul, lst, n)
def test_empty_slice(self):
x = []
x[:] = x
self.assertEqual(x, [])
# TODO: RUSTPYTHON
@unittest.skip("Crashes on windows debug build")
@unittest.skip("TODO: RUSTPYTHON crash")
def test_list_resize_overflow(self):
# gh-97616: test new_allocated * sizeof(PyObject*) overflow
# check in list_resize()
@@ -113,13 +120,28 @@ class ListTest(list_tests.CommonTest):
with self.assertRaises((MemoryError, OverflowError)):
lst *= size
# TODO: RUSTPYTHON
@unittest.skip("TODO: RUSTPYTHON hangs")
def test_repr_mutate(self):
class Obj:
@staticmethod
def __repr__():
try:
mylist.pop()
except IndexError:
pass
return 'obj'
mylist = [Obj() for _ in range(5)]
self.assertEqual(repr(mylist), '[obj, obj, obj]')
def test_repr_large(self):
# Check the repr of large list objects
def check(n):
l = [0] * n
s = repr(l)
self.assertEqual(s,
'[' + ', '.join(['0'] * n) + ']')
'[' + ', '.join(['0'] * n) + ']')
check(10) # check our checking code
check(1000000)
@@ -302,6 +324,35 @@ class ListTest(list_tests.CommonTest):
lst = [X(), X()]
X() in lst
def test_tier2_invalidates_iterator(self):
# GH-121012
for _ in range(100):
a = [1, 2, 3]
it = iter(a)
for _ in it:
pass
a.append(4)
self.assertEqual(list(it), [])
def test_deopt_from_append_list(self):
# gh-132011: it used to crash, because
# of `CALL_LIST_APPEND` specialization failure.
code = textwrap.dedent("""
l = []
def lappend(l, x, y):
l.append((x, y))
for x in range(3):
lappend(l, None, None)
try:
lappend(list, None, None)
except TypeError:
pass
else:
raise AssertionError
""")
rc, _, _ = assert_python_ok("-c", code)
self.assertEqual(rc, 0)
if __name__ == "__main__":
unittest.main()

View File

@@ -177,7 +177,7 @@ class ListComprehensionTest(unittest.TestCase):
res = [__class__ for x in [1]]
"""
self._check_in_scopes(
code, outputs={"res": [2]}, scopes=["module", "function"])
code, outputs={"res": [2]}, scopes=["module", "function"])
self._check_in_scopes(code, raises=NameError, scopes=["class"])
def test_references___class___enclosing(self):
@@ -648,11 +648,18 @@ class ListComprehensionTest(unittest.TestCase):
"""
self._check_in_scopes(code, {"value": [1, None]})
# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_frame_locals(self):
code = """
val = [sys._getframe().f_locals for a in [0]][0]["a"]
val = "a" in [sys._getframe().f_locals for a in [0]][0]
"""
import sys
self._check_in_scopes(code, {"val": False}, ns={"sys": sys})
code = """
val = [sys._getframe().f_locals["a"] for a in [0]][0]
"""
self._check_in_scopes(code, {"val": 0}, ns={"sys": sys})
def _recursive_replace(self, maybe_code):
@@ -736,7 +743,7 @@ class ListComprehensionTest(unittest.TestCase):
for func, expected in [(init_raises, "BrokenIter(init_raises=True)"),
(next_raises, "BrokenIter(next_raises=True)"),
(iter_raises, "BrokenIter(iter_raises=True)"),
]:
]:
with self.subTest(func):
exc = func()
f = traceback.extract_tb(exc.__traceback__)[0]