From 45ad7f3a2889b0234e8215cf0b2507a51542c00a Mon Sep 17 00:00:00 2001 From: Balazs Gibizer Date: Thu, 11 May 2017 13:31:33 +0200 Subject: [PATCH] remove null_safe_int from module scope The null_safe_int implementation is not a fortunate one as it returns empty string when called with None. This can be dangerous as it is not a int. However there is no generic good value to return so this cannot be really fixed. Currently the null_safe_int is only used to convert the progress field of the Instance object into the legacy instance notifications. This patch proposes no to break that legacy behavior but also discurage the usage of this convesion in general by removing null_safe_int from the module scope and inlining to the the only place it is used. Change-Id: I58b1c128c27d5ae7922eead95f808ea77b3fcd06 --- nova/notifications/base.py | 10 +++++----- nova/tests/unit/notifications/test_base.py | 8 -------- 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/nova/notifications/base.py b/nova/notifications/base.py index 05808ceea286..1d2c2a0687de 100644 --- a/nova/notifications/base.py +++ b/nova/notifications/base.py @@ -391,10 +391,6 @@ def null_safe_str(s): return str(s) if s else '' -def null_safe_int(s): - return int(s) if s else '' - - def null_safe_isotime(s): if isinstance(s, datetime.datetime): return utils.strtime(s) @@ -475,7 +471,11 @@ def info_from_instance(context, instance, network_info, # Status properties state=instance.vm_state, state_description=null_safe_str(instance.task_state), - progress=null_safe_int(instance.progress), + # NOTE(gibi): It might seems wrong to default the progress to an empty + # string but this is how legacy work and this code only used by the + # legacy notification so try to keep the compatibility here but also + # keep it contained. + progress=int(instance.progress) if instance.progress else '', # accessIPs access_ip_v4=instance.access_ip_v4, diff --git a/nova/tests/unit/notifications/test_base.py b/nova/tests/unit/notifications/test_base.py index 2136c489261a..09afb44a65a9 100644 --- a/nova/tests/unit/notifications/test_base.py +++ b/nova/tests/unit/notifications/test_base.py @@ -39,14 +39,6 @@ class TestNullSafeUtils(test.NoDBTestCase): line = 'test' self.assertEqual(line, base.null_safe_str(line)) - def test_null_safe_int(self): - number = None - # TODO(gibi): Fix null_safe_int to return 0 as default instead of empty - # string - self.assertEqual('', base.null_safe_int(number)) - number = 10 - self.assertEqual(number, base.null_safe_int(number)) - class TestSendInstanceUpdateNotification(test.NoDBTestCase):