-
Notifications
You must be signed in to change notification settings - Fork 59
Eliminate array resetting loop in _msg_support.c.em #255
Copy link
Copy link
Open
Labels
enhancementNew feature or requestNew feature or request
Description
Description
As comment in the following code suggested, there exists a part of the code that uses a less ideal way to reset the array.
rosidl_python/rosidl_generator_py/resource/_msg_support.c.em
Lines 589 to 608 in 01af609
| // clear the array, poor approach to remove potential default values | |
| Py_ssize_t length = PyObject_Length(field); | |
| if (-1 == length) { | |
| Py_DECREF(field); | |
| return NULL; | |
| } | |
| if (length > 0) { | |
| PyObject * pop = PyObject_GetAttrString(field, "pop"); | |
| assert(pop != NULL); | |
| for (Py_ssize_t i = 0; i < length; ++i) { | |
| PyObject * ret = PyObject_CallFunctionObjArgs(pop, NULL); | |
| if (!ret) { | |
| Py_DECREF(pop); | |
| Py_DECREF(field); | |
| return NULL; | |
| } | |
| Py_DECREF(ret); | |
| } | |
| Py_DECREF(pop); | |
| } |
This part can be improved by replacing with either
PyObject * ret = PyObject_CallMethodNoArgs(field, "clear");but this only works with Python 3.13 or higher (reference)PySequence_DelSlice(field, 0, length)that should work in any Python version
Motivation
This is discovered and suggested in a review from PR #250
Design / Implementation Considerations
No response
Additional Information
No response
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request