summaryrefslogtreecommitdiff
path: root/Modules/gcmodule.c
diff options
context:
space:
mode:
authorJason R. Coombs <jaraco@jaraco.com>2020-12-27 12:46:59 -0500
committerJason R. Coombs <jaraco@jaraco.com>2020-12-27 12:46:59 -0500
commita78f0158a28734f965218b834ea8c0b166b7353f (patch)
treedca70268e2a41d49658e7eed783c6fc243d119cd /Modules/gcmodule.c
parentec8e6895a3ce9cd69b6ceb75a15fcc74d4a522dc (diff)
parentbf64d9064ab641b1ef9a0c4bda097ebf1204faf4 (diff)
downloadcpython-git-revert-23107-revert-13893-fix-issue-37193.tar.gz
Merge branch 'master' into revert-23107-revert-13893-fix-issue-37193revert-23107-revert-13893-fix-issue-37193
Diffstat (limited to 'Modules/gcmodule.c')
-rw-r--r--Modules/gcmodule.c90
1 files changed, 43 insertions, 47 deletions
diff --git a/Modules/gcmodule.c b/Modules/gcmodule.c
index e6ad0f2dd4..fdbba6a7af 100644
--- a/Modules/gcmodule.c
+++ b/Modules/gcmodule.c
@@ -165,12 +165,17 @@ PyStatus
_PyGC_Init(PyThreadState *tstate)
{
GCState *gcstate = &tstate->interp->gc;
+
+ gcstate->garbage = PyList_New(0);
if (gcstate->garbage == NULL) {
- gcstate->garbage = PyList_New(0);
- if (gcstate->garbage == NULL) {
- return _PyStatus_NO_MEMORY();
- }
+ return _PyStatus_NO_MEMORY();
+ }
+
+ gcstate->callbacks = PyList_New(0);
+ if (gcstate->callbacks == NULL) {
+ return _PyStatus_NO_MEMORY();
}
+
return _PyStatus_OK();
}
@@ -1992,59 +1997,50 @@ static PyMethodDef GcMethods[] = {
{NULL, NULL} /* Sentinel */
};
-static struct PyModuleDef gcmodule = {
- PyModuleDef_HEAD_INIT,
- "gc", /* m_name */
- gc__doc__, /* m_doc */
- -1, /* m_size */
- GcMethods, /* m_methods */
- NULL, /* m_reload */
- NULL, /* m_traverse */
- NULL, /* m_clear */
- NULL /* m_free */
-};
-
-PyMODINIT_FUNC
-PyInit_gc(void)
+static int
+gcmodule_exec(PyObject *module)
{
GCState *gcstate = get_gc_state();
- PyObject *m = PyModule_Create(&gcmodule);
-
- if (m == NULL) {
- return NULL;
- }
-
- if (gcstate->garbage == NULL) {
- gcstate->garbage = PyList_New(0);
- if (gcstate->garbage == NULL) {
- return NULL;
- }
- }
- Py_INCREF(gcstate->garbage);
- if (PyModule_AddObject(m, "garbage", gcstate->garbage) < 0) {
- return NULL;
- }
-
- if (gcstate->callbacks == NULL) {
- gcstate->callbacks = PyList_New(0);
- if (gcstate->callbacks == NULL) {
- return NULL;
- }
+ /* garbage and callbacks are initialized by _PyGC_Init() early in
+ * interpreter lifecycle. */
+ assert(gcstate->garbage != NULL);
+ if (PyModule_AddObjectRef(module, "garbage", gcstate->garbage) < 0) {
+ return -1;
}
- Py_INCREF(gcstate->callbacks);
- if (PyModule_AddObject(m, "callbacks", gcstate->callbacks) < 0) {
- return NULL;
+ assert(gcstate->callbacks != NULL);
+ if (PyModule_AddObjectRef(module, "callbacks", gcstate->callbacks) < 0) {
+ return -1;
}
-#define ADD_INT(NAME) if (PyModule_AddIntConstant(m, #NAME, NAME) < 0) { return NULL; }
+#define ADD_INT(NAME) if (PyModule_AddIntConstant(module, #NAME, NAME) < 0) { return -1; }
ADD_INT(DEBUG_STATS);
ADD_INT(DEBUG_COLLECTABLE);
ADD_INT(DEBUG_UNCOLLECTABLE);
ADD_INT(DEBUG_SAVEALL);
ADD_INT(DEBUG_LEAK);
#undef ADD_INT
- return m;
+ return 0;
+}
+
+static PyModuleDef_Slot gcmodule_slots[] = {
+ {Py_mod_exec, gcmodule_exec},
+ {0, NULL}
+};
+
+static struct PyModuleDef gcmodule = {
+ PyModuleDef_HEAD_INIT,
+ .m_name = "gc",
+ .m_doc = gc__doc__,
+ .m_size = 0, // per interpreter state, see: get_gc_state()
+ .m_methods = GcMethods,
+ .m_slots = gcmodule_slots
+};
+
+PyMODINIT_FUNC
+PyInit_gc(void)
+{
+ return PyModuleDef_Init(&gcmodule);
}
/* Public API to invoke gc.collect() from C */
@@ -2294,7 +2290,7 @@ _PyObject_GC_Resize(PyVarObject *op, Py_ssize_t nitems)
}
PyGC_Head *g = AS_GC(op);
- g = (PyGC_Head *)PyObject_REALLOC(g, sizeof(PyGC_Head) + basicsize);
+ g = (PyGC_Head *)PyObject_Realloc(g, sizeof(PyGC_Head) + basicsize);
if (g == NULL)
return (PyVarObject *)PyErr_NoMemory();
op = (PyVarObject *) FROM_GC(g);
@@ -2313,7 +2309,7 @@ PyObject_GC_Del(void *op)
if (gcstate->generations[0].count > 0) {
gcstate->generations[0].count--;
}
- PyObject_FREE(g);
+ PyObject_Free(g);
}
int