mirror of
https://github.com/python/cpython.git
synced 2025-08-04 17:08:35 +00:00
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:
parent
359389ed51
commit
5a23994a3d
5 changed files with 88 additions and 48 deletions
|
@ -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)
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue