Commits

David Beitey committed a8d0eb1

Ensure coerce_string_conf coerces the sign of an integer.

Comments (0)

Files changed (3)

docs/build/changelog.rst

     .. change::
         :tags: bug
 
+      The :func:`.util.coerce_string_conf` method now correctly coerces
+      negative integers and those with a leading + sign. This previously
+      prevented configuring a :class:`.CacheRegion` with an ``expiration_time``
+      of ``'-1'``. Courtesy David Beitey.
+
+    .. change::
+        :tags: bug
+
       The ``refresh()`` method on :meth:`.CacheRegion.get_or_create_multi`
       now supports the ``asdict`` flag.
 
       ``to_str``, defaults to ``str()``.  Can be replaced with ``unicode()``
       or other functions to support caching of functions that
       accept non-unicode arguments.  Initial patch courtesy Lx Yu.
-
+/
     .. change::
         :tags: feature
 

dogpile/cache/util.py

             continue
 
         v = v.strip()
-        if re.match(r'^\d+$', v):
+        if re.match(r'^[-+]?\d+$', v):
             result[k] = int(v)
         elif v.lower() in ('false', 'true'):
             result[k] = v.lower() == 'true'

tests/cache/test_utils.py

+from unittest import TestCase
+
+from dogpile.cache import util
+
+
+class UtilsTest(TestCase):
+    """ Test the relevant utils functionality.
+    """
+
+    def test_coerce_string_conf(self):
+        settings = {'expiration_time': '-1'}
+        coerced = util.coerce_string_conf(settings)
+        self.assertEqual(coerced['expiration_time'], -1)
+
+        settings = {'expiration_time': '+1'}
+        coerced = util.coerce_string_conf(settings)
+        self.assertEqual(coerced['expiration_time'], 1)