Merge pull request #734 from davidism/dont-inline-random
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Prevent random filter from being folded
authored by David Lord · committed by GitHub · parents 47ef6a3, 2e15e52 · Browse files · 3 files·+25 added−10 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+25 added−10 removed
- tests:+16 added−5 removed
- other:+9 added−5 removed
Provenance
- Commit
ecba702- Tree
2306411- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
47ef6a3- Parent 2
2e15e52
3 files changed+25 added−10 removed
modified:jinja2/filters.py+5 added-5 removed
8 unmodified lines9 :license: BSD, see LICENSE for more details.10 """11 import re12 import math13+import random14 14-from random import choice15 from itertools import groupby16 from collections import namedtuple17 from jinja2.utils import Markup, escape, pformat, urlize, soft_unicode, \18 unicode_urlencode, htmlsafe_json_dumps339 unmodified lines358 except StopIteration:359 return environment.undefined('No last item, sequence was empty.')360 361 362-@environmentfilter363-def do_random(environment, seq):362+@contextfilter363+def do_random(context, seq):364 """Return a random item from the sequence."""365 try:366- return choice(seq)366+ return random.choice(seq)367 except IndexError:368- return environment.undefined('No random item, sequence was empty.')368+ return context.environment.undefined('No random item, sequence was empty.')369 370 371 def do_filesizeformat(value, binary=False):372 """Format the value like a 'human-readable' file size (i.e. 13 kB,704 unmodified linesmodified:tests/test_filters.py+16 added-5 removed
6 unmodified lines7 8 :copyright: (c) 2017 by the Jinja Team.9 :license: BSD, see LICENSE for more details.10 """11+import random12 import pytest13 from jinja2 import Markup, Environment14 from jinja2._compat import text_type, implements_to_string15 166 unmodified lines182 tmpl = env.from_string('''{{ data|pprint }}''')183 data = list(range(1000))184 assert tmpl.render(data=data) == pformat(data)185 185- def test_random(self, env):186- tmpl = env.from_string('''{{ seq|random }}''')187- seq = list(range(100))188- for _ in range(10):189- assert int(tmpl.render(seq=seq)) in seq186+ def test_random(self, env, request):187+ # restore the random state when the test ends188+ state = random.getstate()189+ request.addfinalizer(lambda: random.setstate(state))190+ # generate the random values from a known seed191+ random.seed('jinja')192+ expected = [random.choice('1234567890') for _ in range(10)]193 194+ # check that the random sequence is generated again by a template195+ # ensures that filter result is not constant folded196+ random.seed('jinja')197+ t = env.from_string('{{ "1234567890"|random }}')198+199+ for value in expected:200+ assert t.render() == value201+202 def test_reverse(self, env):203 tmpl = env.from_string('{{ "foobar"|reverse|join }}|'204 '{{ [1, 2, 3]|reverse|list }}')205 assert tmpl.render() == 'raboof|[3, 2, 1]'397 unmodified linesmodified:CHANGES+4 added
20 unmodified lines21 across scopes, e.g. from a loop body to code that comes after the loop.22 - Added a `trimmed` modifier to `{% trans %}` to strip linebreaks and23 surrounding whitespace. Also added a new policy to enable this for all24 `trans` blocks.25+- The ``random`` filter is no longer incorrectly constant folded and will26+ produce a new random choice each time the template is rendered. (`#478`_)27 28+.. _#478: https://github.com/pallets/jinja/pull/47829+30 Version 2.9.631 -------------32 (bugfix release, released on April 3rd 2017)33 503 unmodified lines