From 4fe4ff4f998cccdb923852642485dd5cc41ef7cd Mon Sep 17 00:00:00 2001 From: Shahar Naveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Mon, 14 Jul 2025 08:24:00 +0300 Subject: [PATCH] Update `test_{list,listcomps}.py` from 3.13.5 (#5965) --- Lib/test/test_list.py | 55 ++++++++++++++++++++++++++++++++++++-- Lib/test/test_listcomps.py | 13 ++++++--- 2 files changed, 63 insertions(+), 5 deletions(-) diff --git a/Lib/test/test_list.py b/Lib/test/test_list.py index c82bf5067..42d8dcbbe 100644 --- a/Lib/test/test_list.py +++ b/Lib/test/test_list.py @@ -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() diff --git a/Lib/test/test_listcomps.py b/Lib/test/test_listcomps.py index ad1c5053a..1380c08d2 100644 --- a/Lib/test/test_listcomps.py +++ b/Lib/test/test_listcomps.py @@ -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]