Skip to content

Commit 16220d9

Browse files
committed
better exception handling in matcher
Signed-off-by: Yoch Melka <yoch.melka@gmail.com>
1 parent d45de37 commit 16220d9

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

src/paho/mqtt/matcher.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@ def __getitem__(self, key):
3030
node = self._root
3131
for sym in key.split('/'):
3232
node = node._children[sym]
33+
except KeyError as err:
34+
raise KeyError(key) from err
35+
else:
3336
if node._content is None:
3437
raise KeyError(key)
3538
return node._content
36-
except KeyError as ke:
37-
raise KeyError(key) from ke
3839

3940
def __delitem__(self, key):
4041
"""Delete the value associated with some topic filter :key"""
@@ -44,11 +45,13 @@ def __delitem__(self, key):
4445
for k in key.split('/'):
4546
parent, node = node, node._children[k]
4647
lst.append((parent, k, node))
47-
# TODO
48+
except KeyError as err:
49+
raise KeyError(key) from err
50+
else:
51+
if node._content is None:
52+
raise KeyError(key)
4853
node._content = None
49-
except KeyError as ke:
50-
raise KeyError(key) from ke
51-
else: # cleanup
54+
# cleanup
5255
for parent, k, node in reversed(lst):
5356
if node._children or node._content is not None:
5457
break
@@ -66,11 +69,9 @@ def rec(node, i=0):
6669
else:
6770
part = lst[i]
6871
if part in node._children:
69-
for content in rec(node._children[part], i + 1):
70-
yield content
72+
yield from rec(node._children[part], i + 1)
7173
if '+' in node._children and (normal or i > 0):
72-
for content in rec(node._children['+'], i + 1):
73-
yield content
74+
yield from rec(node._children['+'], i + 1)
7475
if '#' in node._children and (normal or i > 0):
7576
content = node._children['#']._content
7677
if content is not None:

tests/test_matcher.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class Test_client_function:
1717
("#", "/foo/bar"),
1818
("/#", "/foo/bar"),
1919
("$SYS/bar", "$SYS/bar"),
20+
("$SYS/#", "$SYS/foo/bar"),
2021
])
2122
def test_matching(self, sub, topic):
2223
assert client.topic_matches_sub(sub, topic)

0 commit comments

Comments
 (0)