Skip to content

Commit

Permalink
Merge eaf8672 into 1b67cdd
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianWilhelm committed May 18, 2019
2 parents 1b67cdd + eaf8672 commit 783a835
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
16 changes: 15 additions & 1 deletion cachetools/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,22 @@ def __add__(self, other, add=tuple.__add__):
def __radd__(self, other, add=tuple.__add__):
return _HashedTuple(add(other, self))

def __getstate__(self):
return {} # Don't pickle the runtime-dependent hashvalue

_kwmark = (object(),)

class _KeyWordMark(object):
# defines this class as singleton
def __new__(cls, *args, **kwargs):
it = cls.__dict__.get("__it__")
if it is not None:
return it
cls.__it__ = it = object.__new__(cls)
it.__init__(*args, **kwargs)
return it


_kwmark = (_KeyWordMark(),)


def hashkey(*args, **kwargs):
Expand Down
39 changes: 39 additions & 0 deletions tests/test_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

from cachetools import LRUCache, cachedmethod, keys

import pytest


class Cached(object):

Expand Down Expand Up @@ -44,6 +46,18 @@ def __exit__(self, *exc):
pass


class Pickled(object):
def __init__(self):
self.cache = {}

@cachedmethod(operator.attrgetter('cache'))
def calc(self, text, x, times_two=False):
result = 42*x
if times_two:
result *= 2
return result


class CachedMethodTest(unittest.TestCase):

def test_dict(self):
Expand Down Expand Up @@ -166,3 +180,28 @@ def test_locked_nospace(self):
self.assertEqual(cached.get(1), 5)
self.assertEqual(cached.get(1.0), 7)
self.assertEqual(cached.get(1.0), 9)

def _call_pickle_obj(self, obj):
obj.calc("abc", 69)
obj.calc("abc", 72, times_two=True)

def test_pickle_hashvalue_store(self):
import pickle

obj = Pickled()
self._call_pickle_obj(obj)
with open('./pickle_test.pickle', 'bw') as fh:
pickle.dump(obj, fh)
assert len(obj.cache) == 2

@pytest.mark.pickle_restore()
def test_pickle_hashvalue_restore(self):
import pickle

with open('./pickle_test.pickle', 'br') as fh:
obj = pickle.load(fh)

# do the same calculations as in `test_pickle_hashvalue_store`
self._call_pickle_obj(obj)
# assure that there a no duplications due to new hash values
assert len(obj.cache) == 2
7 changes: 6 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ deps =
pytest
pytest-cov
commands =
py.test --basetemp={envtmpdir} --cov=cachetools {posargs}
py.test --basetemp={envtmpdir} --cov=cachetools {posargs} -m "not pickle_restore"
py.test --basetemp={envtmpdir} --cov=cachetools {posargs} -m pickle_restore

[testenv:check-manifest]
deps =
Expand Down Expand Up @@ -36,3 +37,7 @@ deps =
commands =
flake8
skip_install = true

[pytest]
markers =
pickle_restore: restoring a pickle from previous run

0 comments on commit 783a835

Please sign in to comment.