[dynamo][polyfills]Support getrecursionlimit (#145989)

Pull Request resolved: https://github.com/pytorch/pytorch/pull/145989
Approved by: https://github.com/StrongerXi, https://github.com/jansel
ghstack dependencies: #145986, #145987, #145994
This commit is contained in:
Animesh Jain 2025-01-30 11:05:41 -08:00 committed by PyTorch MergeBot
parent e7bb608d02
commit 1e3d1738a4
2 changed files with 14 additions and 0 deletions

View File

@ -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)

View File

@ -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()