summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRick Harris <rconradharris@gmail.com>2013-05-10 23:07:27 +0000
committerRick Harris <rconradharris@gmail.com>2013-05-10 23:28:35 +0000
commitd9b0719f02433b243a20fe705af4799c619f4e28 (patch)
tree8bff94d4cfcaad1d6bbfb649ef26df0cf0123580
parent89d1f8b51d05f3282d11b3928443897e28459c7a (diff)
downloadoslo-d9b0719f02433b243a20fe705af4799c619f4e28.tar.gz
oslo-d9b0719f02433b243a20fe705af4799c619f4e28.tar.xz
oslo-d9b0719f02433b243a20fe705af4799c619f4e28.zip
Handle ints passed to `boolean_from_string`
The existing code would return True for '1' but False for int(1), unlike Nova's version which would return True for both inputs. Nova's version feels safer (i.e. it will do the most-likely-correct thing if an int is passed in), so this patch changes Openstack-Common's version to match. Note that this patch does not make Openstack-Common's `boolean_from_string` match Nova's `bool_from_string` exactly though. Nova's version would return True for both '42' and 42, while Openstack-Common's returns False. Though Nova's version, again, seems safer, the presence of unit-tests in Openstack-Common asserting the defined-correctness of this behavior makes me reluctant to change it. Fixes bug 1178760 Change-Id: I8b2a31c4852884187bcbda3a4e78c8513b1bb8c8
-rw-r--r--openstack/common/strutils.py15
-rw-r--r--tests/unit/test_strutils.py12
2 files changed, 21 insertions, 6 deletions
diff --git a/openstack/common/strutils.py b/openstack/common/strutils.py
index fe8418e..d6dfb13 100644
--- a/openstack/common/strutils.py
+++ b/openstack/common/strutils.py
@@ -49,12 +49,15 @@ def bool_from_string(subject):
Useful for JSON-decoded stuff and config file parsing
"""
- if isinstance(subject, bool):
- return subject
- if isinstance(subject, basestring):
- if subject.strip().lower() in ('true', 'on', 'yes', '1'):
- return True
- return False
+ try:
+ # True or 1 or '1' -> True
+ # False or < 1 or > 1 or '0' -> False
+ return int(subject) == 1
+ except TypeError:
+ # None -> False
+ return False
+ except ValueError:
+ return subject.strip().lower() in ('true', 'on', 'yes')
def safe_decode(text, incoming=None, errors='strict'):
diff --git a/tests/unit/test_strutils.py b/tests/unit/test_strutils.py
index 1863c40..b0af958 100644
--- a/tests/unit/test_strutils.py
+++ b/tests/unit/test_strutils.py
@@ -48,6 +48,10 @@ class StrUtilsTest(utils.BaseTestCase):
self.assertFalse(strutils.bool_from_string(c(
'This should not be True')))
+ # Whitespace should be stripped
+ self.assertTrue(strutils.bool_from_string(c(' true ')))
+ self.assertFalse(strutils.bool_from_string(c(' false ')))
+
def test_bool_from_string(self):
self._test_bool_from_string(lambda s: s)
@@ -55,8 +59,16 @@ class StrUtilsTest(utils.BaseTestCase):
self._test_bool_from_string(six.text_type)
def test_other_bool_from_string(self):
+ self.assertFalse(strutils.bool_from_string(None))
self.assertFalse(strutils.bool_from_string(mock.Mock()))
+ def test_int_bool_from_string(self):
+ self.assertTrue(strutils.bool_from_string(1))
+
+ self.assertFalse(strutils.bool_from_string(-1))
+ self.assertFalse(strutils.bool_from_string(0))
+ self.assertFalse(strutils.bool_from_string(2))
+
def test_int_from_bool_as_string(self):
self.assertEqual(1, strutils.int_from_bool_as_string(True))
self.assertEqual(0, strutils.int_from_bool_as_string(False))