Skip to content

Eliminate array resetting loop in _msg_support.c.em #255

@nvcyc

Description

@nvcyc

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.

// 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions