##// END OF EJS Templates
Merge pull request #10867 from tacaswell/fix_bare_function_unregister...
Thomas Kluyver -
r24015:dfb45863 merge
parent child Browse files
Show More
@@ -69,8 +69,11 b' class EventManager(object):'
69
69
70 # Remove callback in case ``function`` was adapted by `backcall`.
70 # Remove callback in case ``function`` was adapted by `backcall`.
71 for callback in self.callbacks[event]:
71 for callback in self.callbacks[event]:
72 if callback.__wrapped__ is function:
72 try:
73 return self.callbacks[event].remove(callback)
73 if callback.__wrapped__ is function:
74 return self.callbacks[event].remove(callback)
75 except AttributeError:
76 pass
74
77
75 raise ValueError('Function {!r} is not registered as a {} callback'.format(function, event))
78 raise ValueError('Function {!r} is not registered as a {} callback'.format(function, event))
76
79
@@ -1,6 +1,7 b''
1 from backcall import callback_prototype
1 from backcall import callback_prototype
2 import unittest
2 import unittest
3 from unittest.mock import Mock
3 from unittest.mock import Mock
4 import nose.tools as nt
4
5
5 from IPython.core import events
6 from IPython.core import events
6 import IPython.testing.tools as tt
7 import IPython.testing.tools as tt
@@ -18,8 +19,10 b' def event_with_argument(argument):'
18
19
19 class CallbackTests(unittest.TestCase):
20 class CallbackTests(unittest.TestCase):
20 def setUp(self):
21 def setUp(self):
21 self.em = events.EventManager(get_ipython(), {'ping_received': ping_received, 'event_with_argument': event_with_argument})
22 self.em = events.EventManager(get_ipython(),
22
23 {'ping_received': ping_received,
24 'event_with_argument': event_with_argument})
25
23 def test_register_unregister(self):
26 def test_register_unregister(self):
24 cb = Mock()
27 cb = Mock()
25
28
@@ -30,7 +33,18 b' class CallbackTests(unittest.TestCase):'
30 self.em.unregister('ping_received', cb)
33 self.em.unregister('ping_received', cb)
31 self.em.trigger('ping_received')
34 self.em.trigger('ping_received')
32 self.assertEqual(cb.call_count, 1)
35 self.assertEqual(cb.call_count, 1)
33
36
37 def test_bare_function_missed_unregister(self):
38 def cb1():
39 ...
40
41 def cb2():
42 ...
43
44 self.em.register('ping_received', cb1)
45 nt.assert_raises(ValueError, self.em.unregister, 'ping_received', cb2)
46 self.em.unregister('ping_received', cb1)
47
34 def test_cb_error(self):
48 def test_cb_error(self):
35 cb = Mock(side_effect=ValueError)
49 cb = Mock(side_effect=ValueError)
36 self.em.register('ping_received', cb)
50 self.em.register('ping_received', cb)
General Comments 0
You need to be logged in to leave comments. Login now