Skip to content

Commit 67b908e

Browse files
authored
Merge pull request python-hyper#54 from python-hyper/self-dependency
Forbid stream self-dependency.
2 parents ad45291 + edfff54 commit 67b908e

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

HISTORY.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ Changelog
66

77
**API Changes**
88

9+
- Throw ``PriorityLoop`` when inserting or reprioritising a stream that
10+
depends on itself.
911
- Throw ``BadWeightError`` when creating or reprioritising a stream with a
1012
weight that is not an integer between 1 and 256, inclusive.
1113
- Throw ``PseudoStreamError`` when trying to reprioritise, remove, block or

src/priority/priority.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,10 @@ def insert_stream(self,
348348

349349
if not depends_on:
350350
depends_on = 0
351+
elif depends_on == stream_id:
352+
raise PriorityLoop(
353+
"Stream %d must not depend on itself." % stream_id
354+
)
351355

352356
if exclusive:
353357
parent_stream = self._get_or_insert_parent(depends_on)
@@ -389,6 +393,11 @@ def reprioritize(self,
389393
# own dependents. Then, we remove this stream from its current parent
390394
# and move it to its new parent, taking its children with it.
391395
if depends_on:
396+
if depends_on == stream_id:
397+
raise PriorityLoop(
398+
"Stream %d must not depend on itself" % stream_id
399+
)
400+
392401
new_parent = self._get_or_insert_parent(depends_on)
393402
cycle = _stream_cycle(new_parent, current_stream)
394403
else:

test/test_priority.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,31 @@ def test_stream_with_out_of_bounds_weight_is_error(self, weight):
455455
err.value.args[0] ==
456456
'Stream weight must be between 1 and 256 (inclusive)')
457457

458+
@pytest.mark.parametrize('exclusive', (True, False))
459+
@pytest.mark.parametrize('stream_id', (1, 5, 20, 32, 256))
460+
def test_stream_depending_on_self_is_error(self, stream_id, exclusive):
461+
"""
462+
Inserting a stream that is dependent on itself is rejected.
463+
"""
464+
p = priority.PriorityTree()
465+
with pytest.raises(priority.PriorityLoop):
466+
p.insert_stream(
467+
stream_id=stream_id, depends_on=stream_id, exclusive=exclusive
468+
)
469+
470+
@pytest.mark.parametrize('exclusive', (True, False))
471+
@pytest.mark.parametrize('stream_id', (1, 5, 20, 32, 256))
472+
def test_reprioritize_depend_on_self_is_error(self, stream_id, exclusive):
473+
"""
474+
Reprioritizing a stream to make it dependent on itself is an error.
475+
"""
476+
p = priority.PriorityTree()
477+
p.insert_stream(stream_id=stream_id)
478+
with pytest.raises(priority.PriorityLoop):
479+
p.reprioritize(
480+
stream_id=stream_id, depends_on=stream_id, exclusive=exclusive
481+
)
482+
458483

459484
class TestPriorityTreeOutput(object):
460485
"""

0 commit comments

Comments
 (0)