Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compile_templates is non-deterministic when using filters #1453

Closed
atetubou opened this issue May 26, 2021 · 1 comment · Fixed by #1454
Closed

compile_templates is non-deterministic when using filters #1453

atetubou opened this issue May 26, 2021 · 1 comment · Fixed by #1454
Milestone

Comments

@atetubou
Copy link
Contributor

Following code generates non-deterministic compiled output.

from jinja2 import loaders
from jinja2.environment import Environment

env = Environment(loader=loaders.DictLoader({"foo": """
{{ 1|filter1 }}
{{ 2|filter2 }}
"""}))

env.filters["filter1"]=lambda value: value
env.filters["filter2"]=lambda value: value

env.compile_templates('archive', zip=None, ignore_errors=False)
 from __future__ import generator_stop
 from jinja2.runtime import LoopContext, Macro, Markup, Namespace, TemplateNotFound, TemplateReference, TemplateRuntimeError, Undefined, concat, escape, identity, internalcode, markup_join, missing, str_join
 name = 'foo'

 def root(context, missing=missing):
     resolve = context.resolve_or_missing
     undefined = environment.undefined
     cond_expr_undefined = Undefined
     if 0: yield None
     try:
-        t_1 = environment.filters['filter1']
+        t_1 = environment.filters['filter2']
     except KeyError:
         @internalcode
         def t_1(*unused):
-            raise TemplateRuntimeError("No filter named 'filter1' found.")
+            raise TemplateRuntimeError("No filter named 'filter2' found.")
     try:
-        t_2 = environment.filters['filter2']
+        t_2 = environment.filters['filter1']
     except KeyError:
         @internalcode
         def t_2(*unused):
-            raise TemplateRuntimeError("No filter named 'filter2' found.")
+            raise TemplateRuntimeError("No filter named 'filter1' found.")
     pass
     yield '\n1\n2'

 blocks = {}
 debug_info = ''

Chromium uses jinja2 for code generator during build, and this breaks Reproducible builds when we are switching to python3.

Some more context is in https://crbug.com/1194274

Environment:

  • Python version: Python 3.9.2
  • Jinja version: 3.0.1
@atetubou
Copy link
Contributor Author

I'll send a PR to fix this.

@davidism davidism added this to the 3.0.2 milestone May 26, 2021
@davidism davidism changed the title compile_templates has non-determinisity around filters compile_templates is non-deterministic when using filters May 26, 2021
@davidism davidism closed this as completed Aug 9, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 24, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants