From 182934f70825d0a7b6005a5d3c8e7ff5c4b6fae8 Mon Sep 17 00:00:00 2001 From: Pavel Kirienko Date: Tue, 6 Apr 2021 06:04:20 +0300 Subject: [PATCH] Adopt https://github.com/UAVCAN/pydsdl/pull/66 --- pyuavcan/application/heartbeat_publisher.py | 2 +- pyuavcan/application/plug_and_play.py | 6 +++--- pyuavcan/dsdl/_templates/deserialization.j2 | 6 +++--- pyuavcan/dsdl/_templates/serialization.j2 | 14 +++++++------- setup.cfg | 1 + tests/dsdl/_builtin_form.py | 2 +- 6 files changed, 16 insertions(+), 15 deletions(-) diff --git a/pyuavcan/application/heartbeat_publisher.py b/pyuavcan/application/heartbeat_publisher.py index 348b205de..003549655 100644 --- a/pyuavcan/application/heartbeat_publisher.py +++ b/pyuavcan/application/heartbeat_publisher.py @@ -44,7 +44,7 @@ class Mode(enum.IntEnum): VENDOR_SPECIFIC_STATUS_CODE_MASK = ( - 2 ** list(pyuavcan.dsdl.get_model(Heartbeat)["vendor_specific_status_code"].data_type.bit_length_set)[0] - 1 + 2 ** pyuavcan.dsdl.get_model(Heartbeat)["vendor_specific_status_code"].data_type.bit_length_set.max - 1 ) diff --git a/pyuavcan/application/plug_and_play.py b/pyuavcan/application/plug_and_play.py index e880cb6ca..a565722de 100644 --- a/pyuavcan/application/plug_and_play.py +++ b/pyuavcan/application/plug_and_play.py @@ -27,10 +27,10 @@ _PSEUDO_UNIQUE_ID_MASK = ( - 2 ** list(pyuavcan.dsdl.get_model(NodeIDAllocationData_1)["unique_id_hash"].data_type.bit_length_set)[0] - 1 + 2 ** pyuavcan.dsdl.get_model(NodeIDAllocationData_1)["unique_id_hash"].data_type.bit_length_set.max - 1 ) -_NODE_ID_MASK = 2 ** max(pyuavcan.dsdl.get_model(ID)["value"].data_type.bit_length_set) - 1 +_NODE_ID_MASK = 2 ** pyuavcan.dsdl.get_model(ID)["value"].data_type.bit_length_set.max - 1 _UNIQUE_ID_SIZE_BYTES = pyuavcan.application.NodeInfo().unique_id.size @@ -63,7 +63,7 @@ class Allocatee: DEFAULT_PRIORITY = pyuavcan.transport.Priority.SLOW - _MTU_THRESHOLD = max(pyuavcan.dsdl.get_model(NodeIDAllocationData_2).bit_length_set) // 8 + _MTU_THRESHOLD = pyuavcan.dsdl.get_model(NodeIDAllocationData_2).bit_length_set.max // 8 def __init__( self, diff --git a/pyuavcan/dsdl/_templates/deserialization.j2 b/pyuavcan/dsdl/_templates/deserialization.j2 index eea066335..c50e02629 100644 --- a/pyuavcan/dsdl/_templates/deserialization.j2 +++ b/pyuavcan/dsdl/_templates/deserialization.j2 @@ -10,7 +10,7 @@ {% set t = self.inner_type %} {% if t is StructureType %} {% set field_ref_map = {} %} - {% for f, offset in t.iterate_fields_with_offsets(0|bit_length_set) %} + {% for f, offset in t.iterate_fields_with_offsets() %} {% if f is not padding %} {% set field_ref = 'f'|to_template_unique_name %} {% do field_ref_map.update({f: field_ref}) %} @@ -33,7 +33,7 @@ {% elif t is UnionType %} {% set tag_ref = 'tag'|to_template_unique_name %} {{ _deserialize_integer(t.tag_field_type, tag_ref, 0|bit_length_set) }} - {% for f, offset in t.iterate_fields_with_offsets(0|bit_length_set) %} + {% for f, offset in t.iterate_fields_with_offsets() %} {# We generate new temporary for each variant to prevent MyPy from complaining. #} {% set field_ref = 'uni'|to_template_unique_name %} {{ 'if' if loop.first else 'elif' }} {{ tag_ref }} == {{ loop.index0 }}: @@ -45,7 +45,7 @@ {% else %}{% assert False %}{# Delimited type is not expected in this context. #} {% endif %} _des_.pad_to_alignment({{ self.alignment_requirement }}) - assert {{ t.bit_length_set|min }} <= (_des_.consumed_bit_length - _base_offset_) <= {{ t.bit_length_set|max }}, \ + assert {{ t.bit_length_set.min }} <= (_des_.consumed_bit_length - _base_offset_) <= {{ t.bit_length_set.max }}, \ 'Bad deserialization of {{ self }}' {%- endmacro %} diff --git a/pyuavcan/dsdl/_templates/serialization.j2 b/pyuavcan/dsdl/_templates/serialization.j2 index aadf8d266..3ac6b1ac6 100644 --- a/pyuavcan/dsdl/_templates/serialization.j2 +++ b/pyuavcan/dsdl/_templates/serialization.j2 @@ -9,11 +9,11 @@ _base_offset_ = _ser_.current_bit_length {% set t = self.inner_type %} {% if t is StructureType %} - {% for f, offset in t.iterate_fields_with_offsets(0|bit_length_set) %} + {% for f, offset in t.iterate_fields_with_offsets() %} {{ _serialize_any(f.data_type, 'self.' + (f|id), offset) }} {% endfor %} {% elif t is UnionType %} - {% for f, offset in t.iterate_fields_with_offsets(0|bit_length_set) %} + {% for f, offset in t.iterate_fields_with_offsets() %} {% set field_ref = 'self.' + (f|id) %} {{ 'if' if loop.first else 'elif' }} {{ field_ref }} is not None: # Union tag {{ loop.index0 }} {{ _serialize_integer(t.tag_field_type, loop.index0|string, 0|bit_length_set)|indent }} @@ -24,7 +24,7 @@ {% else %}{% assert False %}{# Delimited type is not expected in this context. #} {% endif %} _ser_.pad_to_alignment({{ self.alignment_requirement }}) - assert {{ t.bit_length_set|min }} <= (_ser_.current_bit_length - _base_offset_) <= {{ t.bit_length_set|max }}, \ + assert {{ t.bit_length_set.min }} <= (_ser_.current_bit_length - _base_offset_) <= {{ t.bit_length_set.max }}, \ 'Bad serialization of {{ self }}' {%- endmacro %} @@ -120,7 +120,7 @@ {%- elif t is VariableLengthArrayType -%} {{ _serialize_variable_length_array(t, ref, offset) }} {%- elif t is CompositeType -%} {% if t is DelimitedType %} - {% if (t.inner_type.bit_length_set | length) > 1 %} + {% if not t.inner_type.bit_length_set.fixed_length %} {# Instead of the outer extent, we use the inner extent, which equals the max bit length and is a # tighter bound than the user-defined extent. # This is safe because when serializing we always know the concrete type. @@ -136,14 +136,14 @@ {{ ref }}._serialize_(_nested_) _nested_length_ = _nested_.current_bit_length - {{ t.delimiter_header_type.bit_length }} del _nested_ - assert {{ t.inner_type.bit_length_set|min }} <= _nested_length_ <= {{ t.inner_type.bit_length_set|max }} + assert {{ t.inner_type.bit_length_set.min }} <= _nested_length_ <= {{ t.inner_type.bit_length_set.max }} assert _nested_length_ % 8 == 0 _ser_.add_aligned_u32(_nested_length_ // 8) # Jump back and serialize the delimiter header. _ser_.skip_bits(_nested_length_) # Return to the current offset. {% else %} {# Optional optimization: if the nested object is fixed-length, no need to fork the serializer. #} - {% set length_bits = t.inner_type.bit_length_set | first %} - {% assert [length_bits] == t.inner_type.bit_length_set | list %} + {% set length_bits = t.inner_type.bit_length_set.max %} + {% assert length_bits == t.inner_type.bit_length_set.min %} {% assert length_bits % 8 == 0 %} {% set length_bytes = length_bits // 8 %} # Delimited serialization of {{ t }}, fixed bit length {{ length_bits }} ({{ length_bytes }} bytes) diff --git a/setup.cfg b/setup.cfg index af6008597..8775bc604 100644 --- a/setup.cfg +++ b/setup.cfg @@ -57,6 +57,7 @@ packages = find: # The preferred long-term plan is to avoid adding any new required dependencies whatsoever for the project's lifetime. install_requires = nunavut ~= 1.0 + pydsdl @ git+https://github.com/UAVCAN/pydsdl@combinatorial-explosion numpy ~= 1.17, < 1.20 # TODO: allow NumPy v1.20 -- requires fixing type annotations and many meaningless false-positives. No runtime effects. diff --git a/tests/dsdl/_builtin_form.py b/tests/dsdl/_builtin_form.py index 3891f2320..38d85522c 100644 --- a/tests/dsdl/_builtin_form.py +++ b/tests/dsdl/_builtin_form.py @@ -102,7 +102,7 @@ def _unittest_slow_builtin_form_manual(compiled: typing.List[pyuavcan.dsdl.Gener def _unittest_slow_builtin_form_automatic(compiled: typing.List[pyuavcan.dsdl.GeneratedPackageInfo]) -> None: for info in compiled: for model in _util.expand_service_types(info.models): - if max(model.bit_length_set) / 8 > 1024 * 1024: + if model.bit_length_set.max / 8 > 1024 * 1024: _logger.info("Automatic test of %s skipped because the type is too large", model) continue # Skip large objects because they take forever to convert and test