Skip to content

Use soft_str in do_wordcount, to trigger undefined#1160

Merged
davidism merged 1 commit intopallets:2.11.xfrom
sivel:wordcount-soft-str
Feb 27, 2020
Merged

Use soft_str in do_wordcount, to trigger undefined#1160
davidism merged 1 commit intopallets:2.11.xfrom
sivel:wordcount-soft-str

Conversation

@sivel
Copy link
Copy Markdown
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
Copy Markdown
Member

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
Copy Markdown
Contributor Author

sivel commented Feb 27, 2020

I can make that happen soon

@sivel sivel force-pushed the wordcount-soft-str branch from 6ff7135 to df6c38f Compare February 27, 2020 18:47
@sivel sivel changed the base branch from master to 2.11.x February 27, 2020 18:47
@sivel
Copy link
Copy Markdown
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
@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.

2 participants