From 26812e34bd9460a63aebdcbdc1ca27b243f91660 Mon Sep 17 00:00:00 2001 From: Zane Bitter Date: Mon, 17 Jun 2013 17:42:04 +0200 Subject: [PATCH] scheduler: Improve task descriptions in debug logs Tasks that were simply defined as ordinary functions (not methods) were identified by their repr() in the logs, which is only really desirable as a last resort and for objects (where the __call__() method is the task). Use the function name instead. Change-Id: Ie34a3602deb968979118b0961bcddf226cf5983f --- heat/engine/scheduler.py | 7 +++++-- heat/tests/test_scheduler.py | 39 ++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/heat/engine/scheduler.py b/heat/engine/scheduler.py index b4f75069..6857c9bb 100644 --- a/heat/engine/scheduler.py +++ b/heat/engine/scheduler.py @@ -31,11 +31,14 @@ def task_description(task): Return a human-readable string description of a task suitable for logging the status of the task. """ + name = getattr(task, '__name__', None) if isinstance(task, types.MethodType): - name = getattr(task, '__name__') - obj = getattr(task, '__self__') + obj = getattr(task, '__self__', None) if name is not None and obj is not None: return '%s from %s' % (name, obj) + elif isinstance(task, types.FunctionType): + if name is not None: + return str(name) return repr(task) diff --git a/heat/tests/test_scheduler.py b/heat/tests/test_scheduler.py index 95b0caeb..b7c54f6e 100644 --- a/heat/tests/test_scheduler.py +++ b/heat/tests/test_scheduler.py @@ -595,6 +595,45 @@ class TaskTest(mox.MoxTestBase): self.mox.VerifyAll() +class DescriptionTest(mox.MoxTestBase): + def test_func(self): + def f(): + pass + + self.assertEqual(scheduler.task_description(f), 'f') + + def test_lambda(self): + l = lambda: None + + self.assertEqual(scheduler.task_description(l), '') + + def test_method(self): + class C(object): + def __str__(self): + return 'C "o"' + + def __repr__(self): + return 'o' + + def m(self): + pass + + self.assertEqual(scheduler.task_description(C().m), 'm from C "o"') + + def test_object(self): + class C(object): + def __str__(self): + return 'C "o"' + + def __repr__(self): + return 'o' + + def __call__(self): + pass + + self.assertEqual(scheduler.task_description(C()), 'o') + + class WrapperTaskTest(mox.MoxTestBase): def test_wrap(self): -- 2.45.2