Skip to content

Convert more node accessors to pybind11#730

Merged
gbalke merged 12 commits intoros2:masterfrom
gbalke:more_node_accessors
Mar 24, 2021
Merged

Convert more node accessors to pybind11#730
gbalke merged 12 commits intoros2:masterfrom
gbalke:more_node_accessors

Conversation

@gbalke
Copy link
Copy Markdown
Contributor

@gbalke gbalke commented Mar 22, 2021

Part of #665.

Handles:

rclpy_get_node_logger_name
rclpy_get_node_names_and_namespaces
rclpy_get_node_names_and_namespaces_with_enclaves
rclpy_node_get_fully_qualified_name

@gbalke gbalke requested review from cottsay and sloretz March 22, 2021 19:21
@gbalke gbalke self-assigned this Mar 22, 2021
Greg Balke added 4 commits March 22, 2021 12:21
Signed-off-by: Greg Balke <greg@openrobotics.org>
Signed-off-by: Greg Balke <greg@openrobotics.org>
Signed-off-by: Greg Balke <greg@openrobotics.org>
Signed-off-by: Greg Balke <greg@openrobotics.org>
@gbalke gbalke force-pushed the more_node_accessors branch from 65b3947 to 865536e Compare March 22, 2021 19:22
Signed-off-by: Greg Balke <greg@openrobotics.org>
@gbalke
Copy link
Copy Markdown
Contributor Author

gbalke commented Mar 22, 2021

CI up to rclpy:

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

Greg Balke added 4 commits March 22, 2021 15:46
Signed-off-by: Greg Balke <greg@openrobotics.org>
Signed-off-by: Greg Balke <greg@openrobotics.org>
Signed-off-by: Greg Balke <greg@openrobotics.org>
Signed-off-by: Greg Balke <greg@openrobotics.org>
"failed to fini node names during error handling: ");
RCUTILS_SAFE_FWRITE_TO_STDERR(rcl_get_error_string().str);
RCUTILS_SAFE_FWRITE_TO_STDERR("\n");
rcl_reset_error();
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.

@gbalke if an error was set by function A, resetting it before calling function B avoids warnings about error overwrite if function B happens to set an error as well. That is, consider moving each of these if clauses after the corresponding function call. It's pure printing, so no risk of an exception getting thrown there.

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 I got mixed up because of #730 (comment). I'll change it back since I'm not throwing now.

@sloretz sloretz mentioned this pull request Mar 23, 2021
34 tasks
Signed-off-by: Greg Balke <greg@openrobotics.org>
@gbalke gbalke requested a review from hidmic March 23, 2021 17:37
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 one comment, pending green CI

Comment on lines +130 to +145

RCPPUTILS_SCOPE_EXIT(
{
fini_names_ret = rcutils_string_array_fini(&node_names);
fini_namespaces_ret = rcutils_string_array_fini(&node_namespaces);
fini_enclaves_ret = rcutils_string_array_fini(&enclaves);
if (RCUTILS_RET_OK != fini_names_ret) {
for (size_t idx = 0; idx < node_names.size; ++idx) {
if (get_enclaves) {
pynode_names_and_namespaces[idx] = py::make_tuple(
py::str(node_names.data[idx]),
py::str(node_namespaces.data[idx]),
py::str(enclaves.data[idx]));
} else {
pynode_names_and_namespaces[idx] = py::make_tuple(
py::str(node_names.data[idx]),
py::str(node_namespaces.data[idx]));
}
}

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.

@gbalke this won't have the intended effect. If it throws, it'll throw from a destructor and C++ will terminate the program :)

If I may, you may want to do something like:

RCPPUTILS_SCOPE_EXIT({  // setup cleanup block to be run on scope exit
  rcutils_ret_t fini_ret = rcutils_string_array_fini(&node_names);
  if (RCUTILS_RET_OK != fini_ret) {
    RCUTILS_SAFE_FWRITE_TO_STDERR(
      "[rclpy|" RCUTILS_STRINGIFY(__FILE__) ":" RCUTILS_STRINGIFY(__LINE__) "]: "
      "failed to fini node names during error handling: ");
    RCUTILS_SAFE_FWRITE_TO_STDERR(rcl_get_error_string().str);
    RCUTILS_SAFE_FWRITE_TO_STDERR("\n");
    rcl_reset_error();
  }
  fini_ret = rcutils_string_array_fini(&node_namespaces);
  if (RCUTILS_RET_OK != fini_ret) {
    RCUTILS_SAFE_FWRITE_TO_STDERR(
      "[rclpy|" RCUTILS_STRINGIFY(__FILE__) ":" RCUTILS_STRINGIFY(__LINE__) "]: "
      "failed to fini node namespaces during error handling: ");
    RCUTILS_SAFE_FWRITE_TO_STDERR(rcl_get_error_string().str);
    RCUTILS_SAFE_FWRITE_TO_STDERR("\n");
    rcl_reset_error();
  }
  fini_ret = rcutils_string_array_fini(&enclaves);
  if (RCUTILS_RET_OK != fini_ret) {
    RCUTILS_SAFE_FWRITE_TO_STDERR(
      "[rclpy|" RCUTILS_STRINGIFY(__FILE__) ":" RCUTILS_STRINGIFY(__LINE__) "]: "
      "failed to fini enclaves string array during error handling: ");
    RCUTILS_SAFE_FWRITE_TO_STDERR(rcl_get_error_string().str);
    RCUTILS_SAFE_FWRITE_TO_STDERR("\n");
    rcl_reset_error();
  }
});

py::list pynode_names_and_namespaces(node_names.size);
for (size_t idx = 0; idx < node_names.size; ++idx) {
  if (get_enclaves) {
    pynode_names_and_namespaces[idx] = py::make_tuple(
      py::str(node_names.data[idx]),
      py::str(node_namespaces.data[idx]),
      py::str(enclaves.data[idx]));
  } else {
    pynode_names_and_namespaces[idx] = py::make_tuple(
      py::str(node_names.data[idx]),
      py::str(node_namespaces.data[idx]));
  }
}

return pynode_names_and_namespaces;  // and then cleanup code is run automatically

Copy link
Copy Markdown
Contributor Author

@gbalke gbalke Mar 23, 2021

Choose a reason for hiding this comment

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

What I guess i don't understand is that the fini calls will happen before the for loop? Or is that the special magic of the SCOPE_EXIT statement? Where that will be called upon completion?

In essence, you're registering a callback with RCCPUTILS_SCOPE_EXIT.

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.

Read the comments on the blocks and now it makes sense 👍. Thanks for walking me through it haha.

Copy link
Copy Markdown
Contributor

@hidmic hidmic Mar 23, 2021

Choose a reason for hiding this comment

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

Or is that the special magic of the SCOPE_EXIT statement?

If you look under the hood, this RCPPUTILS_SCOPE_EXIT macro instantiates an object that takes your code, wraps it in a lambda function, and executes it on destruction. Using object construction/destruction to manage resource lifecycles is so common in C++ that it has a name. It's the primary idiom when it comes to exception safety.

Signed-off-by: Greg Balke <greg@openrobotics.org>
@gbalke gbalke force-pushed the more_node_accessors branch from 612b848 to 0a4fbd8 Compare March 23, 2021 20:51
@gbalke
Copy link
Copy Markdown
Contributor Author

gbalke commented Mar 23, 2021

CI up to rclpy

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

@gbalke
Copy link
Copy Markdown
Contributor Author

gbalke commented Mar 24, 2021

@sloretz lmk if you want to review this before I merge.

Signed-off-by: Greg Balke <greg@openrobotics.org>
@gbalke
Copy link
Copy Markdown
Contributor Author

gbalke commented Mar 24, 2021

@ros-pull-request-builder retest this please ❤️

@gbalke
Copy link
Copy Markdown
Contributor Author

gbalke commented Mar 24, 2021

Ci up to rclpy

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

@gbalke
Copy link
Copy Markdown
Contributor Author

gbalke commented Mar 24, 2021

@ros-pull-request-builder retest this please 💔

@gbalke gbalke merged commit eefcb1c into ros2:master Mar 24, 2021
@gbalke gbalke deleted the more_node_accessors branch March 24, 2021 20:41
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.

2 participants