From d9b0719f02433b243a20fe705af4799c619f4e28 Mon Sep 17 00:00:00 2001 From: Rick Harris Date: Fri, 10 May 2013 23:07:27 +0000 Subject: 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 --- openstack/common/strutils.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'openstack') 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'): -- cgit