MCPcopy
hub / github.com/tirth8205/code-review-graph / test_recursion_depth_guard

Method test_recursion_depth_guard

tests/test_parser.py:360–385  ·  view source on GitHub ↗

Parser should not crash on deeply nested code.

(self)

Source from the content-addressed store, hash-verified

358 assert len(tested_by) >= 1
359
360 def test_recursion_depth_guard(self):
361 """Parser should not crash on deeply nested code."""
362 # Generate Python code with many nested functions (> _MAX_AST_DEPTH)
363 depth = 200
364 lines = []
365 for i in range(depth):
366 indent = " " * i
367 lines.append(f"{indent}def func_{i}():")
368 lines.append(" " * depth + "pass")
369 source = "\n".join(lines).encode("utf-8")
370
371 import tempfile
372 with tempfile.NamedTemporaryFile(suffix=".py", delete=False) as f:
373 f.write(source)
374 f.flush()
375 path = Path(f.name)
376
377 try:
378 # Should NOT raise RecursionError
379 nodes, edges = self.parser.parse_bytes(path, source)
380 # We should get some functions but not all 200 due to depth cap
381 funcs = [n for n in nodes if n.kind == "Function"]
382 assert len(funcs) > 0
383 assert len(funcs) < depth # capped by _MAX_AST_DEPTH
384 finally:
385 path.unlink(missing_ok=True)
386
387 def test_module_file_cache_bounded(self):
388 """Module file cache should not grow unboundedly."""

Callers

nothing calls this directly

Calls 1

parse_bytesMethod · 0.80

Tested by

no test coverage detected