Skip to content

Commit 1bb57a4

Browse files
committed
Make unmap throw more verbose exception
1 parent 94de9cf commit 1bb57a4

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

pythonosc/dispatcher.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,14 @@ def unmap(self, address: str, handler: FunctionType, *args, needs_reply_address:
8686
pass
8787

8888
def unmap(self, address, handler, *args, needs_reply_address=False):
89-
if isinstance(handler, Handler):
90-
self._map[address].remove(handler)
91-
else:
92-
self._map[address].remove(Handler(handler, list(args), needs_reply_address))
89+
try:
90+
if isinstance(handler, Handler):
91+
self._map[address].remove(handler)
92+
else:
93+
self._map[address].remove(Handler(handler, list(args), needs_reply_address))
94+
except ValueError as e:
95+
if str(e) == "list.remove(x): x not in list":
96+
raise ValueError("Address '%s' doesn't have handler '%s' mapped to it" % (address, handler)) from e
9397

9498
def handlers_for_address(self, address_pattern):
9599
"""yields Handler namedtuples matching the given OSC pattern."""

pythonosc/test/test_dispatcher.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,5 +137,16 @@ def dummyhandler():
137137
self.dispatcher.unmap("/map/me/too", dummyhandler)
138138
self.sortAndAssertSequenceEqual([], self.dispatcher.handlers_for_address("/map/me/too"))
139139

140+
def test_unmap_exception(self):
141+
def dummyhandler():
142+
pass
143+
144+
with self.assertRaises(ValueError) as context:
145+
self.dispatcher.unmap("/unmap/exception", dummyhandler)
146+
147+
handlerobj = self.dispatcher.map("/unmap/somethingelse", dummyhandler())
148+
with self.assertRaises(ValueError) as context:
149+
self.dispatcher.unmap("/unmap/exception", handlerobj)
150+
140151
if __name__ == "__main__":
141152
unittest.main()

0 commit comments

Comments
 (0)