diff --git a/test/dynamo/test_functions.py b/test/dynamo/test_functions.py index 0771e2ce779..e46fd94d787 100644 --- a/test/dynamo/test_functions.py +++ b/test/dynamo/test_functions.py @@ -4505,6 +4505,14 @@ class DefaultsTests(torch._dynamo.test_case.TestCase): self.assertEqual(ref, res) self.assertTrue(isinstance(res, tuple)) + def test_sys_recursionlimit(self): + def fn(x): + return x.sin() * sys.getrecursionlimit() + + opt_fn = torch.compile(fn, backend="eager", fullgraph=True) + x = torch.randn(4) + self.assertEqual(fn(x), opt_fn(x)) + instantiate_parametrized_tests(FunctionTests) diff --git a/torch/_dynamo/polyfills/sys.py b/torch/_dynamo/polyfills/sys.py index 83ace5d4489..2504d2b6fca 100644 --- a/torch/_dynamo/polyfills/sys.py +++ b/torch/_dynamo/polyfills/sys.py @@ -11,9 +11,15 @@ from ..decorators import substitute_in_graph __all__ = [ "intern", + "getrecursionlimit", ] @substitute_in_graph(sys.intern, can_constant_fold_through=True) def intern(string: str, /) -> str: return string + + +@substitute_in_graph(sys.getrecursionlimit, can_constant_fold_through=True) +def getrecursionlimit() -> int: + return sys.getrecursionlimit()