Add support for rosidl::Buffer in rosidl Python path for rclpy#250
Open
Add support for rosidl::Buffer in rosidl Python path for rclpy#250
Conversation
Signed-off-by: CY Chen <cyc@nvidia.com>
hidmic
reviewed
Mar 18, 2026
Contributor
hidmic
left a comment
There was a problem hiding this comment.
First pass. Ownership is bit fuzzy.
| Py_DECREF(backend_attr); | ||
| Py_DECREF(field); | ||
| // Sentinel is set, skip normal conversion for this field | ||
| goto @(member.name)__done; |
Contributor
There was a problem hiding this comment.
@nvcyc why not simply return true? Is this missing some cleanup?
| @(bi) if (length > 0) { | ||
| @(bi) PyObject * pop = PyObject_GetAttrString(field, "pop"); | ||
| @(bi) assert(pop != NULL); | ||
| @(bi) for (Py_ssize_t i = 0; i < length; ++i) { |
Contributor
There was a problem hiding this comment.
@nvcyc meta: I know this code predates your implementation but wow. I can see looping over pop instead of clear having a massive impact in runtime performance.
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.
Description
This pull request adds
rosidl::Buffersupport for uint8[] fields in the Python code generation path.This pull request consists of the following key changes:
_msg.py.em): Foruint8[]fields, the generated setters now allow assigning fromrosidl_buffer.Bufferor plainarray.array. Existing code that setsmsg.data = array.array('B', ...)continues to work unchanged._msg_support.c.em): Messages withuint8[]fields use the sequence'sis_rosidl_bufferflag to determine the conversion path:rosidl_buffer.Bufferobjects, calls_get_buffer_ptr()and sets the C++ Buffer pointer plusis_rosidl_buffer = trueso the RMW path uses the existing buffer without copying. Forarray.arrayobjects, uses the normal byte-copy path.is_rosidl_buffer == true), calls_take_buffer_from_ptr()to wrap the pointer in a PythonBuffer(taking ownership) and set it on the message. CPU-based data is converted toarray.arrayas before.Is this user-facing behavior change?
This pull request does not change existing rclpy behavior. Existing code using
array.arrayforuint8[]fields continues to work. Therosidl_buffer.Buffertype is only encountered when a subscriber receives data from a non-CPU buffer backend (and has opted in viaacceptable_buffer_backends).Did you use Generative AI?
Yes. Claude (claude-4.6-opus) via Cursor was used to assist with creating an initial prototype version of the changes contained in this PR.
Additional Information
This PR is part of the broader ROS 2 native buffer feature introduced in this post.