Skip to content

Commit 0a06553

Browse files
committed
Don't leak references
1 parent 40a49d6 commit 0a06553

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

Include/internal/pycore_list.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ typedef struct {
5555
} _PyListIterObject;
5656

5757
PyAPI_FUNC(PyObject *)_PyList_FromArraySteal(PyObject *const *src, Py_ssize_t n);
58+
PyObject *_PyList_AsTupleStealItems(PyObject *);
5859

5960
#ifdef __cplusplus
6061
}

Objects/abstract.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2069,7 +2069,7 @@ PySequence_Tuple(PyObject *v)
20692069
if (temp == NULL)
20702070
goto Fail;
20712071

2072-
/* Fill the tuple. */
2072+
/* Fill the temporary list. */
20732073
for (;;) {
20742074
PyObject *item = PyIter_Next(it);
20752075
if (item == NULL) {
@@ -2085,7 +2085,7 @@ PySequence_Tuple(PyObject *v)
20852085
}
20862086
}
20872087
Py_DECREF(it);
2088-
PyObject *result = PyList_AsTuple(temp);
2088+
PyObject *result = _PyList_AsTupleStealItems(temp);
20892089
Py_DECREF(temp);
20902090
return result;
20912091

Objects/listobject.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3174,6 +3174,25 @@ PyList_AsTuple(PyObject *v)
31743174
return ret;
31753175
}
31763176

3177+
PyObject *
3178+
_PyList_AsTupleStealItems(PyObject *v)
3179+
{
3180+
if (v == NULL || !PyList_Check(v)) {
3181+
PyErr_BadInternalCall();
3182+
return NULL;
3183+
}
3184+
PyObject *ret;
3185+
PyListObject *self = (PyListObject *)v;
3186+
if (Py_SIZE(v) == 0) {
3187+
return PyTuple_New(0);
3188+
}
3189+
Py_BEGIN_CRITICAL_SECTION(self);
3190+
ret = _PyTuple_FromNonEmptyArraySteal(self->ob_item, Py_SIZE(v));
3191+
Py_SET_SIZE(v, 0);
3192+
Py_END_CRITICAL_SECTION();
3193+
return ret;
3194+
}
3195+
31773196
PyObject *
31783197
_PyList_FromArraySteal(PyObject *const *src, Py_ssize_t n)
31793198
{

0 commit comments

Comments
 (0)