Skip to content

Commit

Permalink
better exception handling in matcher
Browse files Browse the repository at this point in the history
Signed-off-by: Yoch Melka <[email protected]>
  • Loading branch information
yoch committed Nov 5, 2024
1 parent d45de37 commit 16220d9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
21 changes: 11 additions & 10 deletions src/paho/mqtt/matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ def __getitem__(self, key):
node = self._root
for sym in key.split('/'):
node = node._children[sym]
except KeyError as err:
raise KeyError(key) from err
else:
if node._content is None:
raise KeyError(key)
return node._content
except KeyError as ke:
raise KeyError(key) from ke

def __delitem__(self, key):
"""Delete the value associated with some topic filter :key"""
Expand All @@ -44,11 +45,13 @@ def __delitem__(self, key):
for k in key.split('/'):
parent, node = node, node._children[k]
lst.append((parent, k, node))
# TODO
except KeyError as err:
raise KeyError(key) from err
else:
if node._content is None:
raise KeyError(key)
node._content = None
except KeyError as ke:
raise KeyError(key) from ke
else: # cleanup
# cleanup
for parent, k, node in reversed(lst):
if node._children or node._content is not None:
break
Expand All @@ -66,11 +69,9 @@ def rec(node, i=0):
else:
part = lst[i]
if part in node._children:
for content in rec(node._children[part], i + 1):
yield content
yield from rec(node._children[part], i + 1)
if '+' in node._children and (normal or i > 0):
for content in rec(node._children['+'], i + 1):
yield content
yield from rec(node._children['+'], i + 1)
if '#' in node._children and (normal or i > 0):
content = node._children['#']._content
if content is not None:
Expand Down
1 change: 1 addition & 0 deletions tests/test_matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Test_client_function:
("#", "/foo/bar"),
("/#", "/foo/bar"),
("$SYS/bar", "$SYS/bar"),
("$SYS/#", "$SYS/foo/bar"),
])
def test_matching(self, sub, topic):
assert client.topic_matches_sub(sub, topic)
Expand Down

0 comments on commit 16220d9

Please sign in to comment.