Skip to content

Commit 035a457

Browse files
committed
Recalculate ExtensionFrame body length when serializing
1 parent 632e309 commit 035a457

3 files changed

Lines changed: 16 additions & 1 deletion

File tree

CHANGELOG.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ dev
1919

2020
**Bugfixes**
2121

22-
-
22+
- Recalculate the body length when serializing an ExtensionFrame.
2323

2424
6.1.0 (2025-01-22)
2525
------------------

src/hyperframe/frame.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -905,6 +905,8 @@ def serialize(self) -> bytes:
905905
user code: it exists only as a helper method if frames need to be
906906
reconstituted.
907907
"""
908+
self.body_len = len(self.body)
909+
908910
# Build the frame header.
909911
# First, get the flags.
910912
flags = self.flag_byte

tests/test_frames.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -995,3 +995,16 @@ class TestExtensionFrame:
995995
def test_repr(self):
996996
f = ExtensionFrame(0xFF, 1, 42, b'hello')
997997
assert repr(f).endswith("type=255, flag_byte=42, body=<hex:68656c6c6f>")
998+
999+
1000+
@pytest.mark.parametrize('body', [b'', b'hello', b'x' * 256])
1001+
def test_extension_frame_serialized_body_length(body):
1002+
frame = ExtensionFrame(0xFF, 1, 42, body)
1003+
decoded = decode_frame(frame.serialize())
1004+
assert decoded.body == body
1005+
assert decoded.body_len == len(body)
1006+
assert decoded.flag_byte == 42
1007+
frame.body = b'replacement'
1008+
decoded = decode_frame(frame.serialize())
1009+
assert decoded.body == b'replacement'
1010+
assert decoded.body_len == len(frame.body)

0 commit comments

Comments
 (0)