summaryrefslogtreecommitdiffstats
path: root/Modules/gcmodule.c
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2019-11-20 00:18:11 (GMT)
committerGitHub <noreply@github.com>2019-11-20 00:18:11 (GMT)
commit444b39bb64aa894d3f1831210a8ce40042a5a532 (patch)
tree994677ebd6160f8107ddd052c18813a9e61288ba /Modules/gcmodule.c
parente0cd8aa70a3ce19c3d3712568940aa0cbd9aa97b (diff)
downloadcpython-444b39bb64aa894d3f1831210a8ce40042a5a532.zip
cpython-444b39bb64aa894d3f1831210a8ce40042a5a532.tar.gz
cpython-444b39bb64aa894d3f1831210a8ce40042a5a532.tar.bz2
bpo-38631: Avoid Py_FatalError() in handle_legacy_finalizers() (GH-17266)
* Rename _PyGC_Initialize() to _PyGC_InitializeRuntime() * Add _PyGC_Init(): initialize _PyRuntime.gc.garbage list * Call _PyGC_Init() before _PyTypes_Init()
Diffstat (limited to 'Modules/gcmodule.c')
-rw-r--r--Modules/gcmodule.c24
1 files changed, 18 insertions, 6 deletions
diff --git a/Modules/gcmodule.c b/Modules/gcmodule.c
index cb7a3de..d2e3937 100644
--- a/Modules/gcmodule.c
+++ b/Modules/gcmodule.c
@@ -25,6 +25,7 @@
#include "Python.h"
#include "pycore_context.h"
+#include "pycore_initconfig.h"
#include "pycore_object.h"
#include "pycore_pymem.h"
#include "pycore_pystate.h"
@@ -129,7 +130,7 @@ static PyObject *gc_str = NULL;
#define GEN_HEAD(state, n) (&(state)->generations[n].head)
void
-_PyGC_Initialize(struct _gc_runtime_state *state)
+_PyGC_InitializeRuntime(struct _gc_runtime_state *state)
{
state->enabled = 1; /* automatic collection enabled? */
@@ -151,6 +152,21 @@ _PyGC_Initialize(struct _gc_runtime_state *state)
state->permanent_generation = permanent_generation;
}
+
+PyStatus
+_PyGC_Init(_PyRuntimeState *runtime)
+{
+ struct _gc_runtime_state *state = &runtime->gc;
+ if (state->garbage == NULL) {
+ state->garbage = PyList_New(0);
+ if (state->garbage == NULL) {
+ return _PyStatus_NO_MEMORY();
+ }
+ }
+ return _PyStatus_OK();
+}
+
+
/*
_gc_prev values
---------------
@@ -905,13 +921,9 @@ handle_legacy_finalizers(struct _gc_runtime_state *state,
PyGC_Head *finalizers, PyGC_Head *old)
{
assert(!PyErr_Occurred());
+ assert(state->garbage != NULL);
PyGC_Head *gc = GC_NEXT(finalizers);
- if (state->garbage == NULL) {
- state->garbage = PyList_New(0);
- if (state->garbage == NULL)
- Py_FatalError("gc couldn't create gc.garbage list");
- }
for (; gc != finalizers; gc = GC_NEXT(gc)) {
PyObject *op = FROM_GC(gc);