Mercurial > cpython-lru_cache-weakref
diff Objects/moduleobject.c @ 15686:986d1c3ccdde legacy-trunk
Add garbage collection for module objects. Closes patch #102939 and
fixes bug #126345.
| author | Neil Schemenauer <nascheme@enme.ucalgary.ca> |
|---|---|
| date | Tue, 02 Jan 2001 15:58:27 +0000 |
| parents | 8aa947e1eee8 |
| children | 1579d309676e |
line wrap: on
line diff
--- a/Objects/moduleobject.c Mon Jan 01 22:58:20 2001 +0000 +++ b/Objects/moduleobject.c Tue Jan 02 15:58:27 2001 +0000 @@ -18,6 +18,7 @@ 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) @@ -130,11 +131,12 @@ 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 * @@ -211,11 +213,22 @@ 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*/ @@ -223,4 +236,16 @@ (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 */ };
