diff --git a/test/dynamo/cpython/3_13/list_tests.py b/test/dynamo/cpython/3_13/list_tests.py index dbc5ef4f9f2..af717703053 100644 --- a/test/dynamo/cpython/3_13/list_tests.py +++ b/test/dynamo/cpython/3_13/list_tests.py @@ -1,3 +1,56 @@ +# ======= BEGIN Dynamo patch ======= +# Owner(s): ["module: dynamo"] + +# ruff: noqa +# flake8: noqa + +# Test copied from +# https://raw.githubusercontent.com/python/cpython/refs/tags/v3.13.5/Lib/test/list_tests.py + +import sys +import torch +import torch._dynamo.test_case +import unittest +from torch._dynamo.test_case import CPythonTestCase +from torch.testing._internal.common_utils import run_tests + +__TestCase = CPythonTestCase + +# redirect import statements +import sys +import importlib.abc + +redirect_imports = ( + "test.mapping_tests", + "test.typinganndata", + "test.test_grammar", + "test.test_math", + "test.test_iter", + "test.typinganndata.ann_module", +) + +class RedirectImportFinder(importlib.abc.MetaPathFinder): + def find_spec(self, fullname, path, target=None): + # Check if the import is the problematic one + if fullname in redirect_imports: + try: + # Attempt to import the standalone module + name = fullname.removeprefix("test.") + r = importlib.import_module(name) + # Redirect the module in sys.modules + sys.modules[fullname] = r + # Return a module spec from the found module + return importlib.util.find_spec(name) + except ImportError: + return None + return None + +# Add the custom finder to sys.meta_path +sys.meta_path.insert(0, RedirectImportFinder()) + + +# ======= END DYNAMO PATCH ======= + """ Tests common to list and UserList.UserList """ @@ -5,7 +58,7 @@ Tests common to list and UserList.UserList import sys from functools import cmp_to_key -from test import seq_tests +import seq_tests from test.support import ALWAYS_EQ, NEVER_EQ, get_c_recursion_limit @@ -119,10 +172,6 @@ class CommonTest(seq_tests.CommonTest): a[-1] = 9 self.assertEqual(a, self.type2test([5,6,7,8,9])) - msg = "list indices must be integers or slices" - with self.assertRaisesRegex(TypeError, msg): - a['a'] = "python" - def test_delitem(self): a = self.type2test([0, 1]) del a[1] @@ -270,13 +319,14 @@ class CommonTest(seq_tests.CommonTest): self.assertRaises(TypeError, a.extend) # overflow test. issue1621 - class CustomIter: - def __iter__(self): - return self - def __next__(self): - raise StopIteration - def __length_hint__(self): - return sys.maxsize + with torch._dynamo.error_on_graph_break(False): + class CustomIter: + def __iter__(self): + return self + def __next__(self): + raise StopIteration + def __length_hint__(self): + return sys.maxsize a = self.type2test([1,2,3,4]) a.extend(CustomIter()) self.assertEqual(a, [1,2,3,4]) @@ -337,21 +387,23 @@ class CommonTest(seq_tests.CommonTest): a = self.type2test([NEVER_EQ]) self.assertRaises(ValueError, a.remove, ALWAYS_EQ) - class BadExc(Exception): - pass + with torch._dynamo.error_on_graph_break(False): + class BadExc(Exception): + pass - class BadCmp: - def __eq__(self, other): - if other == 2: - raise BadExc() - return False + class BadCmp: + def __eq__(self, other): + if other == 2: + raise BadExc() + return False a = self.type2test([0, 1, 2, 3]) self.assertRaises(BadExc, a.remove, BadCmp()) - class BadCmp2: - def __eq__(self, other): - raise BadExc() + with torch._dynamo.error_on_graph_break(False): + class BadCmp2: + def __eq__(self, other): + raise BadExc() d = self.type2test('abcdefghcij') d.remove('c') @@ -376,13 +428,14 @@ class CommonTest(seq_tests.CommonTest): self.assertRaises(ValueError, a.index, 2, 0, 4) self.assertEqual(a, self.type2test([-2, -1, 0, 1, 2])) - # Test modifying the list during index's iteration - class EvilCmp: - def __init__(self, victim): - self.victim = victim - def __eq__(self, other): - del self.victim[:] - return False + with torch._dynamo.error_on_graph_break(False): + # Test modifying the list during index's iteration + class EvilCmp: + def __init__(self, victim): + self.victim = victim + def __eq__(self, other): + del self.victim[:] + return False a = self.type2test() a[:] = [EvilCmp(a) for _ in range(100)] # This used to seg fault before patch #1005778