Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for 0-length dynamic arrays of simple types #55

Merged
merged 1 commit into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions structured/serializers/arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ def __init__(
) -> None:
self.count_serializer = count_serializer
self.item_serializer = item_serializer
self.num_values = 1
self.num_values = 0
self.size = 0

def with_byte_order(self, byte_order: ByteOrder) -> Self:
Expand All @@ -247,7 +247,11 @@ def with_byte_order(self, byte_order: ByteOrder) -> Self:
def _packer(self, values: tuple[list[T]]) -> tuple[Serializer, list[T]]:
items = values[0]
count = len(items)
serializer = self.count_serializer + (self.item_serializer * count)
if count == 0:
# Since we'll be modifying its .num_values, we want a copy
serializer = StructSerializer(self.count_serializer.format)
else:
serializer = self.count_serializer + (self.item_serializer * count)
serializer.num_values -= 1
self.size = serializer.size
return serializer, items
Expand Down
8 changes: 5 additions & 3 deletions structured/serializers/structs.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
Ts,
Unpack,
)
from .api import Serializer
from .api import NullSerializer, Serializer


def noop_action(x: T) -> T:
Expand Down Expand Up @@ -187,8 +187,10 @@ def __matmul__(self, other: int) -> Self:
def _mul_impl(self, other: int, combine_strings: bool = False) -> Self:
if not isinstance(other, int):
return NotImplemented
elif other <= 0:
raise ValueError('count must be positive')
elif other == 0:
return NullSerializer()
elif other < 0:
raise ValueError('count must be non-negative')
elif other == 1:
return self
byte_order, fmt = self._split_format
Expand Down
10 changes: 10 additions & 0 deletions tests/test_arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,16 @@ class Compound(Structured):
standard_tests(target_obj, target_data)


def test_zero_length():
class EmptyList(Structured):
a: array[Header[uint32], uint8]

target_obj = EmptyList([])
target_data = struct.pack('I', 0)

standard_tests(target_obj, target_data)


def test_dynamic_structured(items: list[Item]):
class Compound(Structured):
a: array[Header[uint32], Item]
Expand Down
2 changes: 1 addition & 1 deletion tests/test_base_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def test_counted() -> None:
with pytest.raises(TypeError):
pad['']
with pytest.raises(ValueError):
pad[0]
pad[-1]


class TestCustomType:
Expand Down
Loading