Convert serialize/deserialize to pybind11#712
Conversation
41b3514 to
ab53412
Compare
|
Rebased on master |
cottsay
left a comment
There was a problem hiding this comment.
Needs some fixes for error handling.
| if (RMW_RET_OK != rmw_ret) { | ||
| throw RCLError("Failed to serialize ROS message"); | ||
| } |
There was a problem hiding this comment.
This isn't the right way to handle RMW errors. There are explicit functions defined for it, but they're all just typedefs to rcutils error handling. It would be OK with me if you just switched this to rcutils error handling, but I think the more correct way to do it would be to introduce an RMWError exception class like you did for RCUtilsError.
There was a problem hiding this comment.
I'll update this to use RMWError from #709 once that's merged.
rclpy/src/rclpy/serialization.cpp
Outdated
| auto message_deleter = [&](void * ptr) {destroy_ros_message(ptr);}; | ||
| auto deserialized_ros_msg = std::unique_ptr<void, decltype(message_deleter)>( | ||
| deserialized_ros_msg_c, message_deleter); |
There was a problem hiding this comment.
I think that in this case, you can pass destroy_ros_message directly as the deleter. Up to you.
| auto message_deleter = [&](void * ptr) {destroy_ros_message(ptr);}; | |
| auto deserialized_ros_msg = std::unique_ptr<void, decltype(message_deleter)>( | |
| deserialized_ros_msg_c, message_deleter); | |
| auto deserialized_ros_msg = std::unique_ptr<void, decltype(destroy_ros_message)>( | |
| deserialized_ros_msg_c, destroy_ros_message); |
rclpy/src/rclpy/serialization.cpp
Outdated
| rmw_ret_t rmw_ret = rmw_deserialize(&serialized_msg, ts, deserialized_ros_msg.get()); | ||
|
|
||
| if (RMW_RET_OK != rmw_ret) { | ||
| throw RCLError("failed to deserialize ROS message"); |
There was a problem hiding this comment.
Again with the RMW error handling.
|
|
||
| extern "C" | ||
| { | ||
| #include "rclpy_common/common.h" |
rclpy/src/rclpy/serialization.cpp
Outdated
| { | ||
| rcutils_ret_t ret = rmw_serialized_message_fini(&rcl_msg_); | ||
| if (RCUTILS_RET_OK != ret) { | ||
| fprintf( |
rclpy/src/rclpy/serialization.hpp
Outdated
|
|
||
| /// Deserialize a ROS message | ||
| /** | ||
| * Raises RCLError on serialization failure |
rclpy/src/rclpy/serialization.hpp
Outdated
| /// Serialize a ROS message | ||
| /** | ||
| * Raises RCLError on serialization failure | ||
| * Raises TypeError if an argument has the wrong type |
There was a problem hiding this comment.
@sloretz meta: isn't this true for any Python code, native or interpreted?
There was a problem hiding this comment.
Hmm maybe it's not true in this case since the args are both py::object 😅 . It's true for any method we bind with a specific type using pybind11, so probably it can be assumed rather than documented. I removed it in 3fa151c.
| } | ||
| serialized_msg.buffer_capacity = length; | ||
| serialized_msg.buffer_length = length; | ||
| serialized_msg.buffer = reinterpret_cast<uint8_t *>(serialized_buffer); |
There was a problem hiding this comment.
@sloretz nit: why not using serialized_msg members in place instead of having these serialized_buffer and length temporary variables?
There was a problem hiding this comment.
Unsigned/signed conversion - speaking of which I added a check that length is greater than 0 before casting in 9efb9cc.
rclpy/src/rclpy/serialization.cpp
Outdated
| serialized_msg.buffer_length = length; | ||
| serialized_msg.buffer = reinterpret_cast<uint8_t *>(serialized_buffer); | ||
|
|
||
| destroy_ros_message_signature * destroy_ros_message = NULL; |
rclpy/src/rclpy/serialization.cpp
Outdated
| } | ||
|
|
||
| void | ||
| init(rcutils_allocator_t allocator) |
There was a problem hiding this comment.
@sloretz why a two-stage construction? Why not merging SerializedMessage::init with the constructor?
There was a problem hiding this comment.
I think I had it as two-stage originally so I could reuse it in the deserialize method, but ended up not going with it to avoid a copy. I merged init and the constructor in d2d3c29
rclpy/src/rclpy/serialization.cpp
Outdated
rclpy/src/rclpy/serialization.cpp
Outdated
| } | ||
| } | ||
|
|
||
| rcl_serialized_message_t rcl_msg_; |
There was a problem hiding this comment.
@sloretz meta: given it's a public member, shall we drop the trailing underscore?
There was a problem hiding this comment.
I didn't find anything in the style guide about trailing underscore's only being for private member variables, but it says structs don't have them. I converted it to a struct and removed the trailing underscore in 5fc0e07
ab53412 to
801d7db
Compare
Signed-off-by: Shane Loretz <sloretz@osrfoundation.org>
Signed-off-by: Shane Loretz <sloretz@osrfoundation.org>
Signed-off-by: Shane Loretz <sloretz@osrfoundation.org>
Signed-off-by: Shane Loretz <sloretz@osrfoundation.org>
Signed-off-by: Shane Loretz <sloretz@osrfoundation.org>
Signed-off-by: Shane Loretz <sloretz@osrfoundation.org>
Signed-off-by: Shane Loretz <sloretz@osrfoundation.org>
Signed-off-by: Shane Loretz <sloretz@osrfoundation.org>
Signed-off-by: Shane Loretz <sloretz@osrfoundation.org>
Signed-off-by: Shane Loretz <sloretz@osrfoundation.org>
Signed-off-by: Shane Loretz <sloretz@osrfoundation.org>
b51f10f to
787d3a6
Compare
part of #665