-
Notifications
You must be signed in to change notification settings - Fork 263
rclpy_init() passes command line arguments to rcl_init() #179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -155,14 +155,58 @@ rclpy_trigger_guard_condition(PyObject * Py_UNUSED(self), PyObject * args) | |
| * Raises RuntimeError if rcl could not be initialized | ||
| */ | ||
| static PyObject * | ||
| rclpy_init(PyObject * Py_UNUSED(self), PyObject * Py_UNUSED(args)) | ||
| rclpy_init(PyObject * Py_UNUSED(self), PyObject * args) | ||
| { | ||
| // TODO(esteve): parse args | ||
| rcl_ret_t ret = rcl_init(0, NULL, rcl_get_default_allocator()); | ||
| if (ret != RCL_RET_OK) { | ||
| PyErr_Format(PyExc_RuntimeError, | ||
| "Failed to init: %s", rcl_get_error_string_safe()); | ||
| rcl_reset_error(); | ||
| // Expect one argument which is a list of strings | ||
| PyObject * pyargs; | ||
| if (!PyArg_ParseTuple(args, "O", &pyargs)) { | ||
| // Exception raised | ||
| return NULL; | ||
| } | ||
|
|
||
| pyargs = PySequence_List(pyargs); | ||
| if (NULL == pyargs) { | ||
| // Exception raised | ||
| return NULL; | ||
| } | ||
| Py_ssize_t pysize_num_args = PyList_Size(pyargs); | ||
| if (pysize_num_args > INT_MAX) { | ||
| PyErr_Format(PyExc_OverflowError, "Too many arguments"); | ||
| return NULL; | ||
| } | ||
| int num_args = (int)pysize_num_args; | ||
|
|
||
| rcl_allocator_t allocator = rcl_get_default_allocator(); | ||
| char ** arg_values = allocator.allocate(sizeof(char *) * num_args, allocator.state); | ||
| if (NULL == arg_values) { | ||
| PyErr_Format(PyExc_MemoryError, "Failed to allocate space for arguments"); | ||
| Py_DECREF(pyargs); | ||
| return NULL; | ||
| } | ||
|
|
||
| bool have_args = true; | ||
| for (int i = 0; i < num_args; ++i) { | ||
| // Returns borrowed reference, do not decref | ||
| PyObject * pyarg = PyList_GetItem(pyargs, i); | ||
| if (NULL == pyarg) { | ||
| have_args = false; | ||
| break; | ||
| } | ||
| // Borrows a pointer, do not free arg_values[i] | ||
| arg_values[i] = PyUnicode_AsUTF8(pyarg); | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we just skip this entire block (L180 to 197) when
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Block skipped when no args in 93c634b |
||
|
|
||
| if (have_args) { | ||
| rcl_ret_t ret = rcl_init(num_args, arg_values, allocator); | ||
| if (ret != RCL_RET_OK) { | ||
| PyErr_Format(PyExc_RuntimeError, "Failed to init: %s", rcl_get_error_string_safe()); | ||
| rcl_reset_error(); | ||
| } | ||
| } | ||
| allocator.deallocate(arg_values, allocator.state); | ||
| Py_DECREF(pyargs); | ||
|
|
||
| if (PyErr_Occurred()) { | ||
| return NULL; | ||
| } | ||
| Py_RETURN_NONE; | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to check, these functions setup the exceptions right? That's why only returning null here is fine?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, PyArg_ParseTuple returns false and sets an exception on failure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added comments about exceptions already being set in 023ccaf