GH-127058: Make PySequence_Tuple safer and probably faster. (#127758)

* Use a small buffer, then list when constructing a tuple from an arbitrary sequence.
This commit is contained in:
Mark Shannon 2024-12-11 14:02:59 +00:00 committed by GitHub
parent 359389ed51
commit 5a23994a3d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 88 additions and 48 deletions

View file

@ -3174,6 +3174,25 @@ PyList_AsTuple(PyObject *v)
return ret;
}
PyObject *
_PyList_AsTupleAndClear(PyListObject *self)
{
assert(self != NULL);
PyObject *ret;
if (self->ob_item == NULL) {
return PyTuple_New(0);
}
Py_BEGIN_CRITICAL_SECTION(self);
PyObject **items = self->ob_item;
Py_ssize_t size = Py_SIZE(self);
self->ob_item = NULL;
Py_SET_SIZE(self, 0);
ret = _PyTuple_FromArraySteal(items, size);
free_list_items(items, false);
Py_END_CRITICAL_SECTION();
return ret;
}
PyObject *
_PyList_FromStackRefSteal(const _PyStackRef *src, Py_ssize_t n)
{