Get func.__class__.__name__ if no attribute __name__
In call_until_true, if func is wrapped with functools.partial, getattr(func, '__name__') will get "AttributeError: 'functools.partial' object has no attribute '__name__'" Now call_until_true supports args and kwargs that are passed to func, so functools.partial is no longer needed, but it's better for call_until_true to get func.__class__.__name__ if func has not attritube __name__. Change-Id: Icc734e44af925655a31e7dcac04620352093cbeb Closes-Bug: #1744210
This commit is contained in:
parent
1555a2b090
commit
effafa3230
|
@ -102,13 +102,13 @@ def call_until_true(func, duration, sleep_for, *args, **kwargs):
|
||||||
now = time.time()
|
now = time.time()
|
||||||
begin_time = now
|
begin_time = now
|
||||||
timeout = now + duration
|
timeout = now + duration
|
||||||
|
func_name = getattr(func, '__name__', getattr(func.__class__, '__name__'))
|
||||||
while now < timeout:
|
while now < timeout:
|
||||||
if func(*args, **kwargs):
|
if func(*args, **kwargs):
|
||||||
LOG.debug("Call %s returns true in %f seconds",
|
LOG.debug("Call %s returns true in %f seconds",
|
||||||
getattr(func, '__name__'), time.time() - begin_time)
|
func_name, time.time() - begin_time)
|
||||||
return True
|
return True
|
||||||
time.sleep(sleep_for)
|
time.sleep(sleep_for)
|
||||||
now = time.time()
|
now = time.time()
|
||||||
LOG.debug("Call %s returns false in %f seconds",
|
LOG.debug("Call %s returns false in %f seconds", func_name, duration)
|
||||||
getattr(func, '__name__'), duration)
|
|
||||||
return False
|
return False
|
||||||
|
|
Loading…
Reference in New Issue