From: Ryan Siemens Date: Mon, 23 Apr 2018 07:26:22 +0000 (-0700) Subject: LRUCache.copy initializes queue methods X-Git-Tag: 2.11.0~31^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=39d2e2254af6c0d829fc98b021f840aed2b53ea1;p=thirdparty%2Fjinja.git LRUCache.copy initializes queue methods --- diff --git a/CHANGES.rst b/CHANGES.rst index d53a4db2..6a35fd85 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -55,6 +55,8 @@ Unreleased - :class:`~nativetypes.NativeTemplate` correctly handles quotes between expressions. ``"'{{ a }}', '{{ b }}'"`` renders as the tuple ``('1', '2')`` rather than the string ``'1, 2'``. :issue:`1020` +- ``LRUCache.copy()`` correctly re-initializes the queue methods + after copying. :issue:`843` Version 2.10.3 diff --git a/jinja2/utils.py b/jinja2/utils.py index 55187937..220edcf1 100644 --- a/jinja2/utils.py +++ b/jinja2/utils.py @@ -341,6 +341,7 @@ class LRUCache(object): rv = self.__class__(self.capacity) rv._mapping.update(self._mapping) rv._queue = deque(self._queue) + rv._postinit() return rv def get(self, key, default=None): diff --git a/tests/test_utils.py b/tests/test_utils.py index 64c2b3a6..56ca5dcd 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -10,7 +10,7 @@ """ from collections import deque -import gc +from copy import copy as shallow_copy import pickle import random @@ -68,6 +68,17 @@ class TestLRUCache(object): assert copy._mapping == cache._mapping assert copy._queue == cache._queue + @pytest.mark.parametrize("copy_func", [LRUCache.copy, shallow_copy]) + def test_copy(self, copy_func): + cache = LRUCache(2) + cache['a'] = 1 + cache['b'] = 2 + copy = copy_func(cache) + assert copy._queue == cache._queue + copy['c'] = 3 + assert copy._queue != cache._queue + assert 'a' not in copy and 'b' in copy and 'c' in copy + def test_clear(self): d = LRUCache(3) d["a"] = 1