Skip to content
Myr

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

3 files+25 added−10 removed

modified:jinja2/filters.py+5 added-5 removed
8 unmodified lines
9 :license: BSD, see LICENSE for more details.10"""11import re12import math13import random1414from random import choice15from itertools import groupby16from collections import namedtuple17from jinja2.utils import Markup, escape, pformat, urlize, soft_unicode, \18 unicode_urlencode, htmlsafe_json_dumps
339 unmodified lines
358 except StopIteration:359 return environment.undefined('No last item, sequence was empty.')360361362@environmentfilter363def do_random(environment, seq):362@contextfilter363def 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.')369370371def do_filesizeformat(value, binary=False):372 """Format the value like a 'human-readable' file size (i.e. 13 kB,
704 unmodified lines
modified:tests/test_filters.py+16 added-5 removed
6 unmodified lines
78 :copyright: (c) 2017 by the Jinja Team.9 :license: BSD, see LICENSE for more details.10"""11import random12import pytest13from jinja2 import Markup, Environment14from jinja2._compat import text_type, implements_to_string15
166 unmodified lines
182 tmpl = env.from_string('''{{ data|pprint }}''')183 data = list(range(1000))184 assert tmpl.render(data=data) == pformat(data)185185 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)]193194 # 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 }}')198199 for value in expected:200 assert t.render() == value201202 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 lines
modified:CHANGES+4 added
20 unmodified lines
21 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`_)2728.. _#478: https://github.com/pallets/jinja/pull/4782930Version 2.9.631-------------32(bugfix release, released on April 3rd 2017)33
503 unmodified lines