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

Use soft_str in do_wordcount, to trigger undefined #1160

Merged
merged 1 commit into from Feb 27, 2020

Conversation

sivel
Copy link
Contributor

@sivel sivel commented Feb 27, 2020

Currently the wordcount filter doesn't properly fail when passed an undefined variable.

This PR updates do_wordcount to utilize soft_str so that the StrictUndefined methods are invoked.

Before:

>>> from jinja2 import Environment, StrictUndefined
>>> e = Environment(undefined=StrictUndefined)
>>> t = e.from_string('{{ undef | wordcount }}')
>>> t.render()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/matt/venvs/ansibledev/lib/python3.8/site-packages/jinja2/environment.py", line 1090, in render
    self.environment.handle_exception()
  File "/Users/matt/venvs/ansibledev/lib/python3.8/site-packages/jinja2/environment.py", line 832, in handle_exception
    reraise(*rewrite_traceback_stack(source=source))
  File "/Users/matt/venvs/ansibledev/lib/python3.8/site-packages/jinja2/_compat.py", line 28, in reraise
    raise value.with_traceback(tb)
  File "<template>", line 1, in top-level template code
  File "/Users/matt/venvs/ansibledev/lib/python3.8/site-packages/jinja2/filters.py", line 764, in do_wordcount
    return len(_word_re.findall(s))
TypeError: expected string or bytes-like object

After:

>>> from jinja2 import Environment, StrictUndefined
>>> e = Environment(undefined=StrictUndefined)
>>> t = e.from_string('{{ undef | wordcount }}')
>>> t.render()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/matt/venvs/ansibledev/lib/python3.8/site-packages/jinja2/environment.py", line 1090, in render
    self.environment.handle_exception()
  File "/Users/matt/venvs/ansibledev/lib/python3.8/site-packages/jinja2/environment.py", line 832, in handle_exception
    reraise(*rewrite_traceback_stack(source=source))
  File "/Users/matt/venvs/ansibledev/lib/python3.8/site-packages/jinja2/_compat.py", line 28, in reraise
    raise value.with_traceback(tb)
  File "<template>", line 1, in top-level template code
  File "/Users/matt/venvs/ansibledev/lib/python3.8/site-packages/jinja2/filters.py", line 764, in do_wordcount
    return len(_word_re.findall(soft_str(s)))
jinja2.exceptions.UndefinedError: 'undef' is undefined

@davidism
Copy link
Member

davidism commented Feb 27, 2020

This seems fine for a 2.11.x release, if you can rebase it to that branch.

Needs a changelog and test.

@sivel
Copy link
Contributor Author

sivel commented Feb 27, 2020

I can make that happen soon

@sivel sivel changed the base branch from master to 2.11.x Feb 27, 2020
@sivel
Copy link
Contributor Author

sivel commented Feb 27, 2020

Rebased, retargeted, changelog and tests added.

@davidism davidism added this to the 2.11.2 milestone Feb 27, 2020
@davidism davidism merged commit be0e0b0 into pallets:2.11.x Feb 27, 2020
11 checks passed
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 13, 2020
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 this pull request may close these issues.

None yet

2 participants