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

Do not ignore SyntaxError from jinja2.Environment.from_string #82607

Merged
merged 1 commit into from Jan 29, 2024

Conversation

mkrizek
Copy link
Contributor

@mkrizek mkrizek commented Jan 26, 2024

SUMMARY

Jinja may generate an invalid Python source code from a template. Trying to compile such source code into a Python code object results in SyntaxError being thrown. An example of such a template is providing the same keyword argument into a lookup twice, resulting in: SyntaxError: keyword argument repeated.

Since jinja2.exceptions.TemplateSyntaxError does not cover such a case, as it is not a Jinja parsing error, we need to catch SyntaxError explicitly ourselves.

Fixes #82606

ISSUE TYPE
  • Bugfix Pull Request

Jinja may generate an invalid Python source code from a template. Trying
to compile such source code into a Python code object results in
SyntaxError being thrown. An example of such a template is providing the
same keyword argument into a lookup twice, resulting in:
`SyntaxError: keyword argument repeated`.

Since `jinja2.exceptions.TemplateSyntaxError` does not cover such a
case, as it is not a Jinja parsing error, we need to catch SyntaxError
explicitly ourselves.

Fixes ansible#82606
@ansibot ansibot added bug This issue/PR relates to a bug. needs_triage Needs a first human triage before being processed. has_issue labels Jan 26, 2024
Copy link
Member

@nitzmahone nitzmahone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM- the code change is superfluous under DT since the way templating failures are handled is completely different, but it'll be good to have the test to ensure these non-Jinja error cases are getting properly handled. Thanks!

@mkrizek
Copy link
Contributor Author

mkrizek commented Jan 29, 2024

the code change is superfluous under DT

OK, I seem to have tested against outdated DT branch where this was still an issue and removing https://github.com/ansible/ansible/pull/82607/files#diff-8900ff472220a4c25fcc888ae62fdda2b036e5f20b343fb58819f97c6b82cc22R950 there recently fixed that.

Merging for the test and as a way for a simple backport.

@mkrizek mkrizek merged commit 6d34eb8 into ansible:devel Jan 29, 2024
62 checks passed
@mkrizek mkrizek deleted the issue-82606 branch January 29, 2024 09:33
mkrizek added a commit to mkrizek/ansible that referenced this pull request Jan 29, 2024
…e#82607)

Jinja may generate an invalid Python source code from a template. Trying
to compile such source code into a Python code object results in
SyntaxError being thrown. An example of such a template is providing the
same keyword argument into a lookup twice, resulting in:
`SyntaxError: keyword argument repeated`.

Since `jinja2.exceptions.TemplateSyntaxError` does not cover such a
case, as it is not a Jinja parsing error, we need to catch SyntaxError
explicitly ourselves.

Fixes ansible#82606

(cherry picked from commit 6d34eb8)
@bcoca bcoca removed the needs_triage Needs a first human triage before being processed. label Jan 30, 2024
sivel pushed a commit that referenced this pull request Feb 14, 2024
#82614)

Jinja may generate an invalid Python source code from a template. Trying
to compile such source code into a Python code object results in
SyntaxError being thrown. An example of such a template is providing the
same keyword argument into a lookup twice, resulting in:
`SyntaxError: keyword argument repeated`.

Since `jinja2.exceptions.TemplateSyntaxError` does not cover such a
case, as it is not a Jinja parsing error, we need to catch SyntaxError
explicitly ourselves.

Fixes #82606

(cherry picked from commit 6d34eb8)
@ansible ansible locked and limited conversation to collaborators Feb 27, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug This issue/PR relates to a bug. has_issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

No warnings/errors and no interpolation for templates with double lookup args
4 participants