more thread safety in LRUCache
Before adoption: this commit came in with the history the project adopted, so no landing records it.
--HG-- branch : trunk
authored by Armin Ronacher · committed by Armin Ronacher · parent 24db451 · Browse files · 7 files·+83 added−16 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
7 files+83 added−16 removed
- tests:+56 added−13 removed
- other:+27 added−3 removed
Provenance
- Commit
e25f24d- Tree
89ff48c- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
24db451
7 files changed+83 added−16 removed
modified:docs/templates.rst+1 added-1 removed
1017 unmodified lines1018 ~~1019 1020 If the expression-statement extension is loaded a tag called `do` is available1021 that works exactly like the regular variable expression (``{{ ... }}``) just1022-that it doesn't print anything. This can be used to modify lists:1022+that it doesn't print anything. This can be used to modify lists::1023 1024 {% do navigation.append('a string') %}modified:jinja2/utils.py+26 added-2 removed
463 unmodified lines464 self._remove(key)465 finally:466 self._wlock.release()467 468- def __iter__(self):469- """Iterate over all values in the cache dict, ordered by468+ def items(self):469+ """Return a list of items."""470+ result = [(key, self._mapping[key]) for key in list(self._queue)]471+ result.reverse()472+ return result473+474+ def iteritems(self):475+ """Iterate over all items."""476+ return iter(self.items())477+478+ def values(self):479+ """Return a list of all values."""480+ return [x[1] for x in self.items()]481+482+ def itervalue(self):483+ """Iterate over all values."""484+ return iter(self.values())485+486+ def keys(self):487+ """Return a list of all keys ordered by most recent usage."""488+ return list(self)489+490+ def iterkeys(self):491+ """Iterate over all keys in the cache dict, ordered by492 the most recent usage.493 """494 return reversed(tuple(self._queue))495 496+ __iter__ = iterkeys497+498 def __reversed__(self):499 """Iterate over the values in the cache dict, oldest items500 coming first.501 """46 unmodified linesmodified:tests/test_filters.py+11 added-1 removed
11 unmodified lines12 13 :copyright: 2007 by Armin Ronacher.14 :license: BSD, see LICENSE for more details.15 """16+from jinja2 import Markup17 18 CAPITALIZE = '''{{ "foo bar"|capitalize }}'''19 CENTER = '''{{ "foo"|center(9) }}'''20 DEFAULT = '''{{ missing|default("no") }}|{{ false|default('no') }}|\48 unmodified lines69 {% endfor %}'''70 FILTERTAG = '''{% filter upper|replace('FOO', 'foo') %}foobar{% endfilter %}'''71 72 72-73 def test_capitalize(env):74 tmpl = env.from_string(CAPITALIZE)75 assert tmpl.render() == 'Foo bar'76 213 unmodified lines290 291 def test_filtertag(env):292 tmpl = env.from_string(FILTERTAG)293 assert tmpl.render() == 'fooBAR'294+295+296+def test_replace(env):297+ tmpl = env.from_string('{{ "foo"|replace("o", 42)}}')298+ assert tmpl.render() == 'f4242'299+300+301+def test_forceescape(env):302+ tmpl = env.from_string('{{ x|forceescape }}')303+ assert tmpl.render(x=Markup('<div />')) == u'<div />'modified:tests/test_imports.py+15 added
44 unmodified lines45 test_env.from_string('{% from "foo" import bar, baz, with context %}')46 test_env.from_string('{% from "foo" import bar, with context %}')47 test_env.from_string('{% from "foo" import bar, with, context %}')48 test_env.from_string('{% from "foo" import bar, with with context %}')49+50+51+def test_exports():52+ m = test_env.from_string('''53+ {% macro toplevel() %}...{% endmacro %}54+ {% macro __private() %}...{% endmacro %}55+ {% set variable = 42 %}56+ {% for item in [1] %}57+ {% macro notthere() %}{% endmacro %}58+ {% endfor %}59+ ''').module60+ assert m.toplevel() == '...'61+ assert not hasattr(m, '__missing')62+ assert m.variable == 4263+ assert not hasattr(m, 'notthere')modified:tests/test_inheritance.py+5 added
91 unmodified lines92 93 94 def test_working(env):95 tmpl = env.get_template('working')96+97+98+def test_reuse_blocks(env):99+ tmpl = env.from_string('{{ self.foo() }}|{% block foo %}42{% endblock %}|{{ self.foo() }}')100+ assert tmpl.render() == '42|42|42'added:tests/test_lrucache.py+25 added
1+# -*- coding: utf-8 -*-2+"""3+ Tests the LRUCache4+ ~~~~~~~~~~~~~~~~~~5+6+ This module tests the LRU Cache7+8+ :copyright: Copyright 2008 by Armin Ronacher.9+ :license: GNU GPL.10+"""11+import thread12+import time13+import random14+from jinja2.utils import LRUCache15+16+17+def test_simple():18+ d = LRUCache(3)19+ d["a"] = 120+ d["b"] = 221+ d["c"] = 322+ d["a"]23+ d["d"] = 424+ assert len(d) == 325+ assert 'a' in d and 'c' in d and 'd' in d and 'b' not in dmodified:tests/test_various.py-12 removed
28 unmodified lines29 tmpl = env.from_string(RAW)30 assert tmpl.render() == '{{ FOO }} and {% BAR %}'31 32 33-def test_lru_cache():34- from jinja2.utils import LRUCache35- d = LRUCache(3)36- d["a"] = 137- d["b"] = 238- d["c"] = 339- d["a"]40- d["d"] = 441- assert len(d) == 342- assert 'a' in d and 'c' in d and 'd' in d and 'b' not in d43-44-33 def test_const(env):34 tmpl = env.from_string(CONST)35 assert tmpl.render() == 'True|False|None|True|False'36 14 unmodified lines