mavgen_python: accept str for char and char[] message fields - #1238
Open
MuhammadFathy wants to merge 1 commit into
Open
mavgen_python: accept str for char and char[] message fields#1238MuhammadFathy wants to merge 1 commit into
MuhammadFathy wants to merge 1 commit into
Conversation
Passing a str for a char/char[] field raised
TypeError: must be str or None, not bytes
from the generated message constructor, e.g.
mav.statustext_send(mavutil.mavlink.MAV_SEVERITY_NOTICE, "hello")
as MAVProxy's example module does. Commit 667cfcd started splitting the
raw value on b"\x00" in __init__, which only works for bytes-like input,
so every caller that had been passing a str broke.
Add a char_field_to_bytes() helper to the generated module and route
char field assignment through it, so str, bytes and bytearray are all
accepted and normalised to bytes before the NUL split and before being
handed to struct.pack. str is encoded as ascii with errors="replace",
matching the decode already performed on the same line.
The constructor and *_encode/*_send annotations for char fields are
widened from bytes to the new CharFieldLike alias so the generated
dialects still type-check.
The second case mentioned in the issue, mavftp.py's
op.payload.split(b"\x00"), is not affected: op.payload is a bytearray
and bytearray.split() accepts a bytes separator.
Adds tests/test_mavgen_python.py, which generates the common dialect and
exercises str/bytes/bytearray input, NUL truncation, non-ascii handling
and a full pack/parse round trip.
Fixes ArduPilot#1225
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Passing a str for a char/char[] field raised
from the generated message constructor, e.g.
as MAVProxy's example module does. Commit 667cfcd started splitting the raw value on b"\x00" in init, which only works for bytes-like input, so every caller that had been passing a str broke.
Add a char_field_to_bytes() helper to the generated module and route char field assignment through it, so str, bytes and bytearray are all accepted and normalised to bytes before the NUL split and before being handed to struct.pack. str is encoded as ascii with errors="replace", matching the decode already performed on the same line.
The constructor and _encode/_send annotations for char fields are widened from bytes to the new CharFieldLike alias so the generated dialects still type-check.
The second case mentioned in the issue, mavftp.py's op.payload.split(b"\x00"), is not affected: op.payload is a bytearray and bytearray.split() accepts a bytes separator.
Adds tests/test_mavgen_python.py, which generates the common dialect and exercises str/bytes/bytearray input, NUL truncation, non-ascii handling and a full pack/parse round trip.
Fixes #1225