Skip to content

test_elif_deep sometimes fail with RecursionError #2079

@frenzymadness

Description

@frenzymadness

I'm running this test in our pipelines many times (during the build of RPM packages):

def test_elif_deep(self, env):
elifs = "\n".join(f"{{% elif a == {i} %}}{i}" for i in range(1, 1000))
tmpl = env.from_string(f"{{% if a == 0 %}}0{elifs}{{% else %}}x{{% endif %}}")
for x in (0, 10, 999):
assert tmpl.render(a=x).strip() == str(x)
assert tmpl.render(a=1000).strip() == "x"

and it sometimes fail with:

=================================== FAILURES ===================================
________________________ TestIfCondition.test_elif_deep ________________________
self = <test_core_tags.TestIfCondition object at 0x3ff7f196c40>
env = <jinja2.environment.Environment object at 0x3ff7edf8910>
    def test_elif_deep(self, env):
        elifs = "\n".join(f"{{% elif a == {i} %}}{i}" for i in range(1, 1000))
>       tmpl = env.from_string(f"{{% if a == 0 %}}0{elifs}{{% else %}}x{{% endif %}}")
tests/test_core_tags.py:315: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
../BUILDROOT/usr/lib/python3.13/site-packages/jinja2/environment.py:1111: in from_string
    return cls.from_code(self, self.compile(source), gs, None)
../BUILDROOT/usr/lib/python3.13/site-packages/jinja2/environment.py:769: in compile
    return self._compile(source, filename)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
self = <jinja2.environment.Environment object at 0x3ff7edf8910>
source = "from jinja2.runtime import LoopContext, Macro, Markup, Namespace, TemplateNotFound, TemplateReference, TemplateRuntim...&987=2973&988=2976&989=2979&990=2982&991=2985&992=2988&993=2991&994=2994&995=2997&996=3000&997=3003&998=3006&999=3009'"
filename = '<template>'
    def _compile(self, source: str, filename: str) -> CodeType:
        """Internal hook that can be overridden to hook a different compile
        method in.
    
        .. versionadded:: 2.5
        """
>       return compile(source, filename, "exec")
E       RecursionError: maximum recursion depth exceeded during compilation
../BUILDROOT/usr/lib/python3.13/site-packages/jinja2/environment.py:709: RecursionError
=========================== short test summary info ============================
FAILED tests/test_core_tags.py::TestIfCondition::test_elif_deep - RecursionEr...
======================== 1 failed, 908 passed in 2.13s =========================

I know it's probably not a problem in Jinja itself, but I don't know how to reproduce it and where to report it, so I thought I'd just let you know. The problem is not caused by the number of elifs because I can higher it to 5000 and the test still works fine.

The packages are built on many different architectures (x86, s390x, ppc64le, aarch64) and they don't influence the result.

Do you have an idea what might cause this?

Environment:

  • Python version: 3.12 and 3.13
  • Jinja version: 3.1.6

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions