Skip to content

Commit fef7e94

Browse files
committed
Don't run garbage collection on interpreter exit if it was explicitly disabled
by the user.
1 parent 9e3ef52 commit fef7e94

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

Include/objimpl.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,11 +224,12 @@ PyAPI_FUNC(void) PyObject_SetArenaAllocator(PyObjectArenaAllocator *allocator);
224224
* ==========================
225225
*/
226226

227-
/* C equivalent of gc.collect(). */
227+
/* C equivalent of gc.collect() which ignores the state of gc.enabled. */
228228
PyAPI_FUNC(Py_ssize_t) PyGC_Collect(void);
229229

230230
#ifndef Py_LIMITED_API
231231
PyAPI_FUNC(Py_ssize_t) _PyGC_CollectNoFail(void);
232+
PyAPI_FUNC(Py_ssize_t) _PyGC_CollectIfEnabled(void);
232233
#endif
233234

234235
/* Test if a type has a GC head */

Modules/gcmodule.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1596,6 +1596,15 @@ PyGC_Collect(void)
15961596
return n;
15971597
}
15981598

1599+
Py_ssize_t
1600+
_PyGC_CollectIfEnabled(void)
1601+
{
1602+
if (!enabled)
1603+
return 0;
1604+
1605+
return PyGC_Collect();
1606+
}
1607+
15991608
Py_ssize_t
16001609
_PyGC_CollectNoFail(void)
16011610
{

Python/pylifecycle.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -600,12 +600,12 @@ Py_FinalizeEx(void)
600600
* XXX but I'm unclear on exactly how that one happens. In any case,
601601
* XXX I haven't seen a real-life report of either of these.
602602
*/
603-
PyGC_Collect();
603+
_PyGC_CollectIfEnabled();
604604
#ifdef COUNT_ALLOCS
605605
/* With COUNT_ALLOCS, it helps to run GC multiple times:
606606
each collection might release some types from the type
607607
list, so they become garbage. */
608-
while (PyGC_Collect() > 0)
608+
while (_PyGC_CollectIfEnabled() > 0)
609609
/* nothing */;
610610
#endif
611611
/* Destroy all modules */
@@ -632,7 +632,7 @@ Py_FinalizeEx(void)
632632
* XXX Python code getting called.
633633
*/
634634
#if 0
635-
PyGC_Collect();
635+
_PyGC_CollectIfEnabled();
636636
#endif
637637

638638
/* Disable tracemalloc after all Python objects have been destroyed,

0 commit comments

Comments
 (0)