diff --git a/test/dynamo/cpython/3_13/test_raise.py b/test/dynamo/cpython/3_13/test_raise.py index 6d26a61bee4..042d1ae3d7c 100644 --- a/test/dynamo/cpython/3_13/test_raise.py +++ b/test/dynamo/cpython/3_13/test_raise.py @@ -1,3 +1,58 @@ +# ======= 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/test_raise.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 ======= + # Copyright 2007 Google, Inc. All Rights Reserved. # Licensed to PSF under a Contributor Agreement. @@ -23,7 +78,7 @@ class Context: return True -class TestRaise(unittest.TestCase): +class TestRaise(__TestCase): def test_invalid_reraise(self): try: raise @@ -148,7 +203,7 @@ class TestRaise(unittest.TestCase): -class TestCause(unittest.TestCase): +class TestCause(__TestCase): def testCauseSyntax(self): try: @@ -221,7 +276,7 @@ class TestCause(unittest.TestCase): self.fail("No exception raised") -class TestTraceback(unittest.TestCase): +class TestTraceback(__TestCase): def test_sets_traceback(self): try: @@ -242,7 +297,7 @@ class TestTraceback(unittest.TestCase): self.fail("No exception raised") -class TestTracebackType(unittest.TestCase): +class TestTracebackType(__TestCase): def raiser(self): raise ValueError @@ -308,7 +363,7 @@ class TestTracebackType(unittest.TestCase): types.TracebackType(other_tb, frame, 1, "nuh-uh") -class TestContext(unittest.TestCase): +class TestContext(__TestCase): def test_instance_context_instance_raise(self): context = IndexError() try: @@ -498,7 +553,7 @@ class TestContext(unittest.TestCase): self.assertEqual(ZeroDivisionError, cm.unraisable.exc_type) -class TestRemovedFunctionality(unittest.TestCase): +class TestRemovedFunctionality(__TestCase): def test_tuples(self): try: raise (IndexError, KeyError) # This should be a tuple! @@ -517,4 +572,4 @@ class TestRemovedFunctionality(unittest.TestCase): if __name__ == "__main__": - unittest.main() + run_tests()