-
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -155,14 +155,51 @@ 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)) { | ||
| return NULL; | ||
| } | ||
|
|
||
| pyargs = PySequence_List(pyargs); | ||
| if (NULL == pyargs) { | ||
| return NULL; | ||
| } | ||
| Py_ssize_t num_args = PyList_Size(pyargs); | ||
|
||
|
|
||
| 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; | ||
|
|
||
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