Skip to content

Commit adc1720

Browse files
committed
Add MissingStreamError exception
1 parent 55bdb08 commit adc1720

File tree

4 files changed

+47
-5
lines changed

4 files changed

+47
-5
lines changed

docs/source/api.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ Exceptions
1515
.. autoclass:: priority.PriorityLoop
1616

1717
.. autoclass:: priority.DuplicateStreamError
18+
19+
.. autoclass:: priority.MissingStreamError

src/priority/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
priority: HTTP/2 priority implementation for Python
44
"""
55
from .priority import ( # noqa
6-
Stream, PriorityTree, DeadlockError, PriorityLoop, DuplicateStreamError
6+
Stream, PriorityTree, DeadlockError, PriorityLoop, DuplicateStreamError,
7+
MissingStreamError
78
)

src/priority/priority.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ class DuplicateStreamError(Exception):
4040
pass
4141

4242

43+
class MissingStreamError(KeyError, Exception):
44+
"""
45+
An operation was attempted on a stream that is not present in the tree.
46+
"""
47+
pass
48+
49+
4350
class Stream(object):
4451
"""
4552
Priority information for a given stream.
@@ -282,7 +289,10 @@ def stream_cycle(new_parent, current):
282289
"Stream %d is in a priority loop." % new_parent.stream_id
283290
) # pragma: no cover
284291

285-
current_stream = self._streams[stream_id]
292+
try:
293+
current_stream = self._streams[stream_id]
294+
except KeyError:
295+
raise MissingStreamError("Stream %d not in tree" % stream_id)
286296

287297
# Update things in a specific order to make sure the calculation
288298
# behaves properly. Specifically, we first update the weight. Then,
@@ -321,7 +331,11 @@ def remove_stream(self, stream_id):
321331
322332
:param stream_id: The ID of the stream to remove.
323333
"""
324-
child = self._streams.pop(stream_id)
334+
try:
335+
child = self._streams.pop(stream_id)
336+
except KeyError:
337+
raise MissingStreamError("Stream %d not in tree" % stream_id)
338+
325339
parent = child.parent
326340
parent.remove_child(child)
327341

@@ -331,7 +345,10 @@ def block(self, stream_id):
331345
332346
:param stream_id: The ID of the stream to block.
333347
"""
334-
self._streams[stream_id].active = False
348+
try:
349+
self._streams[stream_id].active = False
350+
except KeyError:
351+
raise MissingStreamError("Stream %d not in tree" % stream_id)
335352

336353
def unblock(self, stream_id):
337354
"""
@@ -340,7 +357,10 @@ def unblock(self, stream_id):
340357
:param stream_id: The ID of the stream to unblock.
341358
"""
342359
# When a stream becomes unblocked,
343-
self._streams[stream_id].active = True
360+
try:
361+
self._streams[stream_id].active = True
362+
except KeyError:
363+
raise MissingStreamError("Stream %d not in tree" % stream_id)
344364

345365
# The iterator protocol
346366
def __iter__(self): # pragma: no cover

test/test_priority.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,25 @@ def test_priority_tree_raises_error_inserting_duplicate(self):
273273
with pytest.raises(priority.DuplicateStreamError):
274274
p.insert_stream(1)
275275

276+
def test_priority_raises_good_errors_for_missing_streams(self):
277+
"""
278+
Attempting operations on absent streams raises a MissingStreamError.
279+
"""
280+
p = priority.PriorityTree()
281+
p.insert_stream(1)
282+
283+
with pytest.raises(priority.MissingStreamError):
284+
p.reprioritize(3)
285+
286+
with pytest.raises(priority.MissingStreamError):
287+
p.block(3)
288+
289+
with pytest.raises(priority.MissingStreamError):
290+
p.unblock(3)
291+
292+
with pytest.raises(priority.MissingStreamError):
293+
p.remove_stream(3)
294+
276295

277296
class TestPriorityTreeOutput(object):
278297
"""

0 commit comments

Comments
 (0)