Merge "Raise value errors instead of asserts"

This commit is contained in:
Jenkins 2014-12-04 08:28:31 +00:00 committed by Gerrit Code Review
commit a57c811922
7 changed files with 38 additions and 14 deletions

View File

@ -42,7 +42,8 @@ class TypeDispatcher(object):
filter should return a truthy object if the message should be requeued
and a falsey object if it should not.
"""
assert six.callable(callback), "Callback must be callable"
if not six.callable(callback):
raise ValueError("Requeue filter callback must be callable")
self._requeue_filters.append(callback)
def _collect_requeue_votes(self, data, message):

View File

@ -187,7 +187,8 @@ class BaseTask(atom.Atom):
if event not in self.TASK_EVENTS:
raise ValueError("Unknown task event '%s', can only bind"
" to events %s" % (event, self.TASK_EVENTS))
assert six.callable(handler), "Handler must be callable"
if not six.callable(handler):
raise ValueError("Event handler callback must be callable")
self._events_listeners[event].append((handler, kwargs))
def unbind(self, event, handler=None):
@ -251,11 +252,13 @@ class FunctorTask(BaseTask):
def __init__(self, execute, name=None, provides=None,
requires=None, auto_extract=True, rebind=None, revert=None,
version=None, inject=None):
assert six.callable(execute), ("Function to use for executing must be"
" callable")
if revert:
assert six.callable(revert), ("Function to use for reverting must"
" be callable")
if not six.callable(execute):
raise ValueError("Function to use for executing must be"
" callable")
if revert is not None:
if not six.callable(revert):
raise ValueError("Function to use for reverting must"
" be callable")
if name is None:
name = reflection.get_callable_name(execute)
super(FunctorTask, self).__init__(name, provides=provides,

View File

@ -79,6 +79,11 @@ class NotifierTest(test.TestCase):
nt.Notifier.ANY, call_me,
kwargs={'details': 5})
def test_not_callable(self):
notifier = nt.Notifier()
self.assertRaises(ValueError, notifier.register,
nt.Notifier.ANY, 2)
def test_selective_notify(self):
call_counts = collections.defaultdict(list)

View File

@ -282,6 +282,10 @@ class TaskTest(test.TestCase):
self.assertFalse(task.unbind('update_progress', handler2))
self.assertEqual(len(task._events_listeners), 1)
def test_bind_not_callable(self):
task = MyTask()
self.assertRaises(ValueError, task.bind, 'update_progress', 2)
class FunctorTaskTest(test.TestCase):
@ -289,3 +293,10 @@ class FunctorTaskTest(test.TestCase):
version = (2, 0)
f_task = task.FunctorTask(lambda: None, version=version)
self.assertEqual(f_task.version, version)
def test_execute_not_callable(self):
self.assertRaises(ValueError, task.FunctorTask, 2)
def test_revert_not_callable(self):
self.assertRaises(ValueError, task.FunctorTask, lambda: None,
revert=2)

View File

@ -400,5 +400,5 @@ class FSMTest(test.TestCase):
m = fsm.FSM('working')
m.add_state('working')
m.add_state('broken')
self.assertRaises(AssertionError, m.add_state, 'b', on_enter=2)
self.assertRaises(AssertionError, m.add_state, 'b', on_exit=2)
self.assertRaises(ValueError, m.add_state, 'b', on_enter=2)
self.assertRaises(ValueError, m.add_state, 'b', on_exit=2)

View File

@ -101,9 +101,11 @@ class FSM(object):
if state in self._states:
raise excp.Duplicate("State '%s' already defined" % state)
if on_enter is not None:
assert six.callable(on_enter), "On enter callback must be callable"
if not six.callable(on_enter):
raise ValueError("On enter callback must be callable")
if on_exit is not None:
assert six.callable(on_exit), "On exit callback must be callable"
if not six.callable(on_exit):
raise ValueError("On exit callback must be callable")
self._states[state] = {
'terminal': bool(terminal),
'reactions': {},
@ -137,7 +139,8 @@ class FSM(object):
if state not in self._states:
raise excp.NotFound("Can not add a reaction to event '%s' for an"
" undefined state '%s'" % (event, state))
assert six.callable(reaction), "Reaction callback must be callable"
if not six.callable(reaction):
raise ValueError("Reaction callback must be callable")
if event not in self._states[state]['reactions']:
self._states[state]['reactions'][event] = (reaction, args, kwargs)
else:

View File

@ -99,9 +99,10 @@ class Notifier(object):
``details``, that will hold event details provided to the
:meth:`.notify` method.
"""
assert six.callable(callback), "Callback must be callable"
if not six.callable(callback):
raise ValueError("Notification callback must be callable")
if self.is_registered(event_type, callback):
raise ValueError("Callback %s already registered" % (callback))
raise ValueError("Notification callback already registered")
if kwargs:
for k in self.RESERVED_KEYS:
if k in kwargs: