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
This commit is contained in:
Zane Bitter 2013-06-17 17:42:04 +02:00
parent afbd6c9d99
commit 26812e34bd
2 changed files with 44 additions and 2 deletions

View File

@ -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)

View File

@ -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), '<lambda>')
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):