Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 53 additions & 2 deletions Lib/test/test_list.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -98,8 +100,13 @@ def imul(a, b): a *= b
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()
Expand All @@ -113,13 +120,28 @@ def test_list_resize_overflow(self):
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)

Expand Down Expand Up @@ -302,6 +324,35 @@ def __eq__(self, other):
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()
13 changes: 10 additions & 3 deletions Lib/test/test_listcomps.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def test_references___class___defined(self):
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):
Expand Down Expand Up @@ -648,11 +648,18 @@ def test_exception_in_post_comp_call(self):
"""
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):
Expand Down Expand Up @@ -736,7 +743,7 @@ def iter_raises():
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]
Expand Down
Loading