Index: Include/dictobject.h =================================================================== --- Include/dictobject.h (revision 63892) +++ Include/dictobject.h (working copy) @@ -137,6 +137,8 @@ PyAPI_FUNC(int) PyDict_SetItemString(PyObject *dp, const char *key, PyObject *item); PyAPI_FUNC(int) PyDict_DelItemString(PyObject *dp, const char *key); +PyAPI_FUNC(int) PyDict_ClearFreeList(void); + #ifdef __cplusplus } #endif Index: Include/listobject.h =================================================================== --- Include/listobject.h (revision 63892) +++ Include/listobject.h (working copy) @@ -62,6 +62,8 @@ #define PyList_SET_ITEM(op, i, v) (((PyListObject *)(op))->ob_item[i] = (v)) #define PyList_GET_SIZE(op) Py_SIZE(op) +PyAPI_FUNC(int) PyList_ClearFreeList(void); + #ifdef __cplusplus } #endif Index: Include/setobject.h =================================================================== --- Include/setobject.h (revision 63892) +++ Include/setobject.h (working copy) @@ -93,6 +93,8 @@ PyAPI_FUNC(PyObject *) PySet_Pop(PyObject *set); PyAPI_FUNC(int) _PySet_Update(PyObject *set, PyObject *iterable); +PyAPI_FUNC(int) PySet_ClearFreeList(void); + #ifdef __cplusplus } #endif Index: Objects/dictobject.c =================================================================== --- Objects/dictobject.c (revision 63892) +++ Objects/dictobject.c (working copy) @@ -207,18 +207,27 @@ static PyDictObject *free_list[PyDict_MAXFREELIST]; static int numfree = 0; -void -PyDict_Fini(void) +int +PyDict_ClearFreeList(void) { + int freelist_size = 0; PyDictObject *op; while (numfree) { op = free_list[--numfree]; assert(PyDict_CheckExact(op)); PyObject_GC_Del(op); + freelist_size += 1; } + return freelist_size; } +void +PyDict_Fini(void) +{ + (void)PyDict_ClearFreeList(); +} + PyObject * PyDict_New(void) { Index: Objects/listobject.c =================================================================== --- Objects/listobject.c (revision 63892) +++ Objects/listobject.c (working copy) @@ -88,18 +88,27 @@ static PyListObject *free_list[PyList_MAXFREELIST]; static int numfree = 0; -void -PyList_Fini(void) +int +PyList_ClearFreeList(void) { + int freelist_size = 0; PyListObject *op; while (numfree) { op = free_list[--numfree]; assert(PyList_CheckExact(op)); PyObject_GC_Del(op); + freelist_size += 1; } + return freelist_size; } +void +PyList_Fini(void) +{ + (void)PyList_ClearFreeList(); +} + PyObject * PyList_New(Py_ssize_t size) { Index: Objects/setobject.c =================================================================== --- Objects/setobject.c (revision 63892) +++ Objects/setobject.c (working copy) @@ -1054,16 +1054,25 @@ return emptyfrozenset; } -void -PySet_Fini(void) +int +PySet_ClearFreeList(void) { - PySetObject *so; + int freelist_size = 0; + PySetObject *op; while (numfree) { - numfree--; - so = free_list[numfree]; - PyObject_GC_Del(so); + op = free_list[--numfree]; + assert(PySet_CheckExact(op)); + PyObject_GC_Del(op); + freelist_size += 1; } + return freelist_size; +} + +void +PySet_Fini(void) +{ + (void)PySet_ClearFreeList(); Py_CLEAR(dummy); Py_CLEAR(emptyfrozenset); } Index: Doc/c-api/list.rst =================================================================== --- Doc/c-api/list.rst (revision 63892) +++ Doc/c-api/list.rst (working copy) @@ -145,3 +145,10 @@ Return a new tuple object containing the contents of *list*; equivalent to ``tuple(list)``. + + +.. cfunction:: int PyList_ClearFreeList(void) + + Clear the free list. Return the total number of freed items. + + .. versionadded:: 2.6 Index: Doc/c-api/set.rst =================================================================== --- Doc/c-api/set.rst (revision 63892) +++ Doc/c-api/set.rst (working copy) @@ -169,3 +169,10 @@ .. cfunction:: int PySet_Clear(PyObject *set) Empty an existing set of all elements. + + +.. cfunction:: int PySet_ClearFreeList(void) + + Clear the free list. Return the total number of freed items. + + .. versionadded:: 2.6 Index: Doc/c-api/dict.rst =================================================================== --- Doc/c-api/dict.rst (revision 63892) +++ Doc/c-api/dict.rst (working copy) @@ -218,3 +218,10 @@ a[key] = value .. versionadded:: 2.2 + + +.. cfunction:: int PyDict_ClearFreeList(void) + + Clear the free list. Return the total number of freed items. + + .. versionadded:: 2.6 Index: Modules/gcmodule.c =================================================================== --- Modules/gcmodule.c (revision 63892) +++ Modules/gcmodule.c (working copy) @@ -735,6 +735,9 @@ (void)PyFrame_ClearFreeList(); (void)PyCFunction_ClearFreeList(); (void)PyTuple_ClearFreeList(); + (void)PyList_ClearFreeList(); + (void)PySet_ClearFreeList(); + (void)PyDict_ClearFreeList(); (void)PyUnicode_ClearFreeList(); }