Skip to content

Commit

Permalink
Add garbage collection for module objects. Closes patch #102939 and
Browse files Browse the repository at this point in the history
fixes bug #126345.
  • Loading branch information
nascheme committed Jan 2, 2001
1 parent 5a95e42 commit 10e31cf
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions Objects/moduleobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ PyModule_New(char *name)
return NULL;
nameobj = PyString_FromString(name);
m->md_dict = PyDict_New();
PyObject_GC_Init(m);
if (m->md_dict == NULL || nameobj == NULL)
goto fail;
if (PyDict_SetItemString(m->md_dict, "__name__", nameobj) != 0)
Expand Down Expand Up @@ -130,11 +131,12 @@ _PyModule_Clear(PyObject *m)
static void
module_dealloc(PyModuleObject *m)
{
PyObject_GC_Fini(m);
if (m->md_dict != NULL) {
_PyModule_Clear((PyObject *)m);
Py_DECREF(m->md_dict);
}
PyObject_DEL(m);
PyObject_DEL(PyObject_AS_GC(m));
}

static PyObject *
Expand Down Expand Up @@ -211,16 +213,39 @@ module_setattr(PyModuleObject *m, char *name, PyObject *v)
return PyDict_SetItemString(m->md_dict, name, v);
}

/* We only need a traverse function, no clear function: If the module
is in a cycle, md_dict will be cleared as well, which will break
the cycle. */
static int
module_traverse(PyModuleObject *m, visitproc visit, void *arg)
{
if (m->md_dict != NULL)
return visit(m->md_dict, arg);
return 0;
}

PyTypeObject PyModule_Type = {
PyObject_HEAD_INIT(&PyType_Type)
0, /*ob_size*/
"module", /*tp_name*/
sizeof(PyModuleObject), /*tp_size*/
sizeof(PyModuleObject) + PyGC_HEAD_SIZE, /*tp_size*/
0, /*tp_itemsize*/
(destructor)module_dealloc, /*tp_dealloc*/
0, /*tp_print*/
(getattrfunc)module_getattr, /*tp_getattr*/
(setattrfunc)module_setattr, /*tp_setattr*/
0, /*tp_compare*/
(reprfunc)module_repr, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/
0, /* tp_doc */
(traverseproc)module_traverse, /* tp_traverse */
};

0 comments on commit 10e31cf

Please sign in to comment.