Skip to content

Commit e13ddc9

Browse files
committed
- New C API PyGC_Collect(), same as calling gc.collect().
- Call this in Py_Finalize(). - Expand the Misc/NEWS text on PY_LONG_LONG.
1 parent cf8d285 commit e13ddc9

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

Include/objimpl.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,9 @@ PyAPI_FUNC(PyVarObject *) _PyObject_NewVar(PyTypeObject *, int);
228228
* ==========================
229229
*/
230230

231+
/* C equivalent of gc.collect(). */
232+
long PyGC_Collect(void);
233+
231234
/* Test if a type has a GC head */
232235
#define PyType_IS_GC(t) PyType_HasFeature((t), Py_TPFLAGS_HAVE_GC)
233236

Misc/NEWS

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,17 @@ Build
169169
C API
170170
-----
171171

172+
- Added PyGC_Collect(), equivalent to calling gc.collect().
173+
172174
- PyThreadState_GetDict() was changed not to raise an exception or
173175
issue a fatal error when no current thread state is available. This
174176
makes it possible to print dictionaries when no thread is active.
175177

176-
- LONG_LONG was renamed to PY_LONG_LONG.
178+
- LONG_LONG was renamed to PY_LONG_LONG. Extensions that use this and
179+
need compatibility with previous versions can use this:
180+
#ifndef PY_LONG_LONG
181+
#define PY_LONG_LONG LONG_LONG
182+
#endif
177183

178184
- Added PyObject_SelfIter() to fill the tp_iter slot for the
179185
typical case where the method returns its self argument.

Modules/gcmodule.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -980,8 +980,26 @@ initgc(void)
980980
#undef ADD_INT
981981
}
982982

983+
/* API to invoke gc.collect() from C */
984+
long
985+
PyGC_Collect(void)
986+
{
987+
long n;
988+
989+
if (collecting)
990+
n = 0; /* already collecting, don't do anything */
991+
else {
992+
collecting = 1;
993+
n = collect(NUM_GENERATIONS - 1);
994+
collecting = 0;
995+
}
996+
997+
return n;
998+
}
999+
9831000
/* for debugging */
984-
void _PyGC_Dump(PyGC_Head *g)
1001+
void
1002+
_PyGC_Dump(PyGC_Head *g)
9851003
{
9861004
_PyObject_Dump(FROM_GC(g));
9871005
}

Python/pythonrun.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,17 @@ Py_Finalize(void)
255255
Py_XDECREF(PyModule_WarningsModule);
256256
PyModule_WarningsModule = NULL;
257257

258+
/* Collect garbage. This may call finalizers; it's nice to call these
259+
before all modules are destroyed. */
260+
PyGC_Collect();
261+
258262
/* Destroy all modules */
259263
PyImport_Cleanup();
260264

265+
/* Collect final garbage. This disposes of cycles created by
266+
new-style class definitions, for example. */
267+
PyGC_Collect();
268+
261269
/* Destroy the database used by _PyImport_{Fixup,Find}Extension */
262270
_PyImport_Fini();
263271

0 commit comments

Comments
 (0)