Skip to content

Use Pybind11 for name functions#709

Merged
sloretz merged 10 commits intomasterfrom
pybind11_names
Mar 17, 2021
Merged

Use Pybind11 for name functions#709
sloretz merged 10 commits intomasterfrom
pybind11_names

Conversation

@sloretz
Copy link
Copy Markdown
Contributor

@sloretz sloretz commented Mar 10, 2021

Part of #665

This uses pybind11 for name-related functions. I also added RCUtilsError since it was useful for one of the functions, and relaxed test expectations on the TypeError human readable text since the text output by pybind11 is a little bit different when an argument is the wrong type.

@sloretz sloretz self-assigned this Mar 10, 2021
@sloretz sloretz mentioned this pull request Mar 10, 2021
34 tasks
@sloretz
Copy link
Copy Markdown
Contributor Author

sloretz commented Mar 10, 2021

CI (build: --packages-up-to rclpy test: --packages-select rclpy)

  • Linux Build Status
  • Linux-aarch64 Build Status
  • macOS Build Status
  • Windows Build Status

@sloretz sloretz requested review from azeey and cottsay March 11, 2021 16:07
@sloretz
Copy link
Copy Markdown
Contributor Author

sloretz commented Mar 11, 2021

Rebased on master

Copy link
Copy Markdown
Member

@cottsay cottsay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few quick comments to address.

It's worth noting that several of the return types are changing from List to Tuple in this PR, though it's probably the right thing to do since it matches the documentation and is more performant.

Comment on lines +136 to +143
if (rcutils_ret == RCUTILS_RET_BAD_ALLOC) {
rcl_reset_error();
throw std::bad_alloc();
} else if (rcutils_ret != RCUTILS_RET_OK) {
throw RCLError("failed to initialize string map");
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be rcutils error handling, not rcl.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docs should be updated to reflect the other exception type, too.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated docs with RMWError, RCUtilsError, or RCLError in c269b6a

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, actually fixed this one in d7fd3b0

/// Validate a node name and return error message and index of invalidation.
/**
* Raises MemoryError if memory could not be allocated
* Raises RuntimeError if an unexpected error happened while validating the node name
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Raises RuntimeError if an unexpected error happened while validating the node name
* Raises RCLError if an unexpected error happened while validating the node name

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this doc got fixed in c269b6a too


/// Expand a topic name
/**
* Raises ValueError if the topic name, node name, or namespace are not valid.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These last three functions can raise RCLError. Is that omitted intentionally?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added note about raising RCLError in c269b6a

Copy link
Copy Markdown
Contributor

@hidmic hidmic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks great!

int validation_result;
size_t invalid_index;
rmw_ret_t ret = rmw_validate_namespace(namespace_, &validation_result, &invalid_index);
if (ret == RCL_RET_BAD_ALLOC) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sloretz in the same vein as @cottsay comments about rcutils error handling vs. rcl error handling, it'd be good to deal with rmw error handling using rmw API.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there a similar issue in get_validation_error_for_full_topic_name, get_validation_error_for_node_name.

On further investigation, there seems to be some handling for this in rcl. They seem to be translated to rcl error codes though?
https://github.com/ros2/rcl/blob/0ca8c6162136594e490ba69fbf288de1b5f7073d/rcl/src/rcl/expand_topic_name.c#L79

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both rmw and rcl error handling APIs are aliases of rcutils'. That's why people tend to use them interchangeably. Which means we'll have a huge body of code to migrate if that stops being true at some point.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regardless, I think it is worthwhile to use the correct types (even if they are aliases of each other). Besides the fact that it makes migration possible (though that is unlikely), there is no question for future readers of the code on whether this is correct or not.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added RMWError in fc32a0a and used rcl, rcutuils, or rmw constants and functions for each case in c269b6a

// finalize the string map before returning
rcutils_ret = rcutils_string_map_fini(&substitutions_map);
if (rcutils_ret != RCUTILS_RET_OK) {
fprintf(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sloretz FYI there's RCUTILS_SAFE_FWRITE_TO_STDERR too.

Copy link
Copy Markdown
Contributor

@hidmic hidmic Mar 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, I skimmed over the fact you are actually formatting. Either disregard or drop the integer formatting and use two safe writes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uses two calls to RCUTILS_SAFE_FWRITE_TO_STDERR in 90f1052

throw RCLError("failed to get node options");
}

char * output_cstr = NULL;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sloretz nit:

Suggested change
char * output_cstr = NULL;
char * output_cstr = nullptr;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NULL -> nullptr in dcfc7e2

sloretz added 6 commits March 16, 2021 18:04
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>
sloretz added 3 commits March 16, 2021 18:58
Signed-off-by: Shane Loretz <sloretz@osrfoundation.org>
Signed-off-by: Shane Loretz <sloretz@osrfoundation.org>
Signed-off-by: Shane Loretz <sloretz@osrfoundation.org>
@sloretz
Copy link
Copy Markdown
Contributor Author

sloretz commented Mar 17, 2021

CI (build: --packages-up-to rclpy test: --packages-select rclpy)

  • Linux Build Status
  • Linux-aarch64 Build Status
  • macOS Build Status
  • Windows Build Status

Copy link
Copy Markdown
Contributor

@hidmic hidmic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM but for a few minor nits I won't block on.

auto resolved_name = std::unique_ptr<char, decltype(name_deleter)>(
output_cstr, name_deleter);

if (ret != RCL_RET_OK) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sloretz nit^N: flip operands, here and elsewhere.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flipped conditionals in 193a026

node_options->allocator.deallocate(name, node_options->allocator.state);
};
auto resolved_name = std::unique_ptr<char, decltype(name_deleter)>(
output_cstr, name_deleter);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sloretz FYI, nit^N: RCPPUTILS_SCOPE_EXIT may be handy in these cases.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted, I'll use this on the next PR once the rcpputils dependency is added.

Signed-off-by: Shane Loretz <sloretz@osrfoundation.org>
@sloretz
Copy link
Copy Markdown
Contributor Author

sloretz commented Mar 17, 2021

CI (build: --packages-up-to rclpy test: --packages-select rclpy)

  • Linux Build Status
  • Linux-aarch64 Build Status
  • macOS Build Status
  • Windows Build Status

@sloretz sloretz requested a review from hidmic March 17, 2021 16:55
@sloretz sloretz merged commit ea26f5f into master Mar 17, 2021
@delete-merged-branch delete-merged-branch bot deleted the pybind11_names branch March 17, 2021 18:57
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.

5 participants