@@ -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+
4350class 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
0 commit comments