From eec8a9731b4ee3255ee8426c73e4087dc38693d9 Mon Sep 17 00:00:00 2001 From: Paul Brown Date: Thu, 22 Jun 2017 22:20:50 -0400 Subject: [PATCH] Produce consistent string for __repr__() of NoValue The :attr:`.api.NO_VALUE` constant now has a fixed ``__repr__()`` output, so that scenarios where this constant's string value ends up being used as a cache key do not create multiple values. Pull request courtesy Paul Brown. Change-Id: Iecc9633d6be562bc6821bf4ee2ff3f29d1b31e80 Pull-request: https://bitbucket.org/zzzeek/dogpile.cache/pull-requests/61 --- docs/build/changelog.rst | 8 ++++++++ dogpile/cache/api.py | 8 ++++++++ tests/cache/test_region.py | 5 +++++ 3 files changed, 21 insertions(+) diff --git a/docs/build/changelog.rst b/docs/build/changelog.rst index c9652e4..0ba4e2d 100644 --- a/docs/build/changelog.rst +++ b/docs/build/changelog.rst @@ -14,6 +14,14 @@ Changelog problems for backends like that of Redis. Pull request courtesy Tobias Sauerwein. + .. change:: + :tags: bug + + The :attr:`.api.NO_VALUE` constant now has a fixed ``__repr__()`` + output, so that scenarios where this constant's string value + ends up being used as a cache key do not create multiple values. + Pull request courtesy Paul Brown. + .. changelog:: :version: 0.6.3 :released: Thu May 18, 2017 diff --git a/dogpile/cache/api.py b/dogpile/cache/api.py index dbab2db..d66e5a7 100644 --- a/dogpile/cache/api.py +++ b/dogpile/cache/api.py @@ -13,6 +13,13 @@ class NoValue(object): def payload(self): return self + def __repr__(self): + """Ensure __repr__ is a consistent value in case NoValue is used to + fill another cache key. + + """ + return '' + if py3k: def __bool__(self): # pragma NO COVERAGE return False @@ -20,6 +27,7 @@ class NoValue(object): def __nonzero__(self): # pragma NO COVERAGE return False + NO_VALUE = NoValue() """Value returned from ``get()`` that describes a key not present.""" diff --git a/tests/cache/test_region.py b/tests/cache/test_region.py index b08b1ad..1a69b27 100644 --- a/tests/cache/test_region.py +++ b/tests/cache/test_region.py @@ -17,6 +17,11 @@ def key_mangler(key): return "HI!" + key +class APITest(TestCase): + def test_no_value_str(self): + eq_(str(NO_VALUE), "") + + class RegionTest(TestCase): def _region(self, init_args={}, config_args={}, backend="mock"):