8000 bpo-42208: Pass tstate to _PyGC_CollectNoFail() (GH-23038) · python/cpython@8b34148 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8b34148

Browse files
authored
bpo-42208: Pass tstate to _PyGC_CollectNoFail() (GH-23038)
Move private _PyGC_CollectNoFail() to the internal C API. Remove the private _PyGC_CollectIfEnabled() which was just an alias to the public PyGC_Collect() function since Python 3.8. Rename functions: * collect() => gc_collect_main() * collect_with_callback() => gc_collect_with_callback() * collect_generations() => gc_collect_generations()
1 parent 99608c7 commit 8b34148

File tree

5 files changed

+25
-34
lines changed

5 files changed

+25
-34
lines changed

Include/cpython/objimpl.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,6 @@ PyAPI_FUNC(void) PyObject_GetArenaAllocator(PyObjectArenaAllocator *allocator);
7979
PyAPI_FUNC(void) PyObject_SetArenaAllocator(PyObjectArenaAllocator *allocator);
8080

8181

82-
PyAPI_FUNC(Py_ssize_t) _PyGC_CollectNoFail(void);
83-
PyAPI_FUNC(Py_ssize_t) _PyGC_CollectIfEnabled(void);
84-
85-
8682
/* Test if an object implements the garbage collector protocol */
8783
PyAPI_FUNC(int) PyObject_IS_GC(PyObject *obj);
8884

Include/internal/pycore_gc.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,9 @@ struct _gc_runtime_state {
161161
Py_ssize_t long_lived_pending;
162162
};
163163

164-
PyAPI_FUNC(void) _PyGC_InitState(struct _gc_runtime_state *);
164+
extern void _PyGC_InitState(struct _gc_runtime_state *);
165+
166+
extern Py_ssize_t _PyGC_CollectNoFail(PyThreadState *tstate);
165167

166168

167169
// Functions to clear types free lists

Modules/gcmodule.c

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,8 +1176,9 @@ handle_resurrected_objects(PyGC_Head *unreachable, PyGC_Head* still_unreachable,
11761176
/* This is the main function. Read this to understand how the
11771177
* collection process works. */
11781178
static Py_ssize_t
1179-
collect(PyThreadState *tstate, int generation,
1180-
Py_ssize_t *n_collected, Py_ssize_t *n_uncollectable, int nofail)
1179+
gc_collect_main(PyThreadState *tstate, int generation,
1180+
Py_ssize_t *n_collected, Py_ssize_t *n_uncollectable,
1181+
int nofail)
11811182
{
11821183
int i;
11831184
Py_ssize_t m = 0; /* # objects collected */
@@ -1395,19 +1396,19 @@ invoke_gc_callback(PyThreadState *tstate, const char *phase,
13951396
* progress callbacks.
13961397
*/
13971398
static Py_ssize_t
1398-
collect_with_callback(PyThreadState *tstate, int generation)
1399+
gc_collect_with_callback(PyThreadState *tstate, int generation)
13991400
{
14001401
assert(!_PyErr_Occurred(tstate));
14011402
Py_ssize_t result, collected, uncollectable;
14021403
invoke_gc_callback(tstate, "start", generation, 0, 0);
1403-
result = collect(tstate, generation, &collected, &uncollectable, 0);
1404+
result = gc_collect_main(tstate, generation, &collected, &uncollectable, 0);
14041405
invoke_gc_callback(tstate, "stop", generation, collected, uncollectable);
14051406
assert(!_PyErr_Occurred(tstate));
14061407
return result;
14071408
}
14081409

14091410
static Py_ssize_t
1410-
collect_generations(PyThreadState *tstate)
1411+
gc_collect_generations(PyThreadState *tstate)
14111412
{
14121413
GCState *gcstate = &tstate->interp->gc;
14131414
/* Find the oldest generation (highest numbered) where the count
@@ -1455,7 +1456,7 @@ collect_generations(PyThreadState *tstate)
14551456
if (i == NUM_GENERATIONS - 1
14561457
&& gcstate->long_lived_pending < gcstate->long_lived_total / 4)
14571458
continue;
1458-
n = collect_with_callback(tstate, i);
1459+
n = gc_collect_with_callback(tstate, i);
14591460
break;
14601461
}
14611462
}
@@ -1541,7 +1542,7 @@ gc_collect_impl(PyObject *module, int generation)
15411542
}
15421543
else {
15431544
gcstate->collecting = 1;
1544-
n = collect_with_callback(tstate, generation);
1545+
n = gc_collect_with_callback(tstate, generation);
15451546
gcstate->collecting = 0;
15461547
}
15471548
return n;
@@ -2041,7 +2042,7 @@ PyInit_gc(void)
20412042
return m;
20422043
}
20432044

2044-
/* API to invoke gc.collect() from C */
2045+
/* Public API to invoke gc.collect() from C */
20452046
Py_ssize_t
20462047
PyGC_Collect(void)
20472048
{
@@ -2061,7 +2062,7 @@ PyGC_Collect(void)
20612062
PyObject *exc, *value, *tb;
20622063
gcstate->collecting = 1;
20632064
_PyErr_Fetch(tstate, &exc, &value, &tb);
2064-
n = collect_with_callback(tstate, NUM_GENERATIONS - 1);
2065+
n = gc_collect_with_callback(tstate, NUM_GENERATIONS - 1);
20652066
_PyErr_Restore(tstate, exc, value, tb);
20662067
gcstate->collecting = 0;
20672068
}
@@ -2070,19 +2071,11 @@ PyGC_Collect(void)
20702071
}
20712072

20722073
Py_ssize_t
2073-
_PyGC_CollectIfEnabled(void)
2074+
_PyGC_CollectNoFail(PyThreadState *tstate)
20742075
{
2075-
return PyGC_Collect();
2076-
}
2077-
2078-
Py_ssize_t
2079-
_PyGC_CollectNoFail(void)
2080-
{
2081-
PyThreadState *tstate = _PyThreadState_GET();
20822076
assert(!_PyErr_Occurred(tstate));
20832077

20842078
GCState *gcstate = &tstate->interp->gc;
2085-
Py_ssize_t n;
20862079

20872080
/* Ideally, this function is only called on interpreter shutdown,
20882081
and therefore not recursively. Unfortunately, when there are daemon
@@ -2091,13 +2084,13 @@ _PyGC_CollectNoFail(void)
20912084
See http://bugs.python.org/issue8713#msg195178 for an example.
20922085
*/
20932086
if (gcstate->collecting) {
2094-
n = 0;
2095-
}
2096-
else {
2097-
gcstate->collecting = 1;
2098-
n = collect(tstate, NUM_GENERATIONS - 1, NULL, NULL, 1);
2099-
gcstate->collecting = 0;
2087+
return 0;
21002088
}
2089+
2090+
Py_ssize_t n;
2091+
gcstate->collecting = 1;
2092+
n = gc_collect_main(tstate, NUM_GENERATIONS - 1, NULL, NULL, 1);
2093+
gcstate->collecting = 0;
21012094
return n;
21022095
}
21032096

@@ -2240,7 +2233,7 @@ _PyObject_GC_Alloc(int use_calloc, size_t basicsize)
22402233
!_PyErr_Occurred(tstate))
22412234
{
22422235
gcstate->collecting = 1;
2243-
collect_generations(tstate);
2236+
gc_collect_generations(tstate);
22442237
gcstate->collecting = 0;
22452238
}
22462239
PyObject *op = FROM_GC(g);

Python/import.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ _PyImport_Cleanup(PyThreadState *tstate)
566566
}
567567
Py_XDECREF(dict);
568568
/* Collect references */
569-
_PyGC_CollectNoFail();
569+
_PyGC_CollectNoFail(tstate);
570570
/* Dump GC stats before it's too late, since it uses the warnings
571571
machinery. */
572572
_PyGC_DumpShutdownStats(tstate);
@@ -626,7 +626,7 @@ _PyImport_Cleanup(PyThreadState *tstate)
626626
Py_DECREF(modules);
627627

628628
/* Once more */
629-
_PyGC_CollectNoFail();
629+
_PyGC_CollectNoFail(tstate);
630630

631631
#undef CLEAR_MODULE
632632
#undef STORE_MODULE_WEAKREF

Python/pylifecycle.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,7 +1293,7 @@ finalize_interp_clear(PyThreadState *tstate)
12931293
PyInterpreterState_Clear(tstate->interp);
12941294

12951295
/* Last explicit GC collection */
1296-
_PyGC_CollectNoFail();
1296+
_PyGC_CollectNoFail(tstate);
12971297

12981298
/* Clear all loghooks */
12991299
/* Both _PySys_Audit function and users still need PyObject, such as tuple.
@@ -1414,7 +1414,7 @@ Py_FinalizeEx(void)
14141414
* XXX but I'm unclear on exactly how that one happens. In any case,
14151415
* XXX I haven't seen a real-life report of either of these.
14161416
*/
1417-
_PyGC_CollectIfEnabled();
1417+
PyGC_Collect();
14181418

14191419
/* Destroy all modules */
14201420
_PyImport_Cleanup(tstate);

0 commit comments

Comments
 (0)
0