Skip to content

mavgen_python: accept str for char and char[] message fields - #1238

Open
MuhammadFathy wants to merge 1 commit into
ArduPilot:masterfrom
MuhammadFathy:fix-1225-char-fields-accept-str
Open

mavgen_python: accept str for char and char[] message fields#1238
MuhammadFathy wants to merge 1 commit into
ArduPilot:masterfrom
MuhammadFathy:fix-1225-char-fields-accept-str

Conversation

@MuhammadFathy

Copy link
Copy Markdown

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 #1225

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

str.split() does not accept bytes

1 participant