diff options
author | Neil Schemenauer <nascheme@enme.ucalgary.ca> | 2001-01-02 15:58:27 (GMT) |
---|---|---|
committer | Neil Schemenauer <nascheme@enme.ucalgary.ca> | 2001-01-02 15:58:27 (GMT) |
commit | 10e31cf82e5fa325e779013619d9e11bf8b56c28 (patch) | |
tree | 61143a048035bbc9536d302892680ec39c4b424e /Objects/moduleobject.c | |
parent | 5a95e42841c88072a6f3123a8fb33b22f9d2de97 (diff) | |
download | cpython-10e31cf82e5fa325e779013619d9e11bf8b56c28.zip cpython-10e31cf82e5fa325e779013619d9e11bf8b56c28.tar.gz cpython-10e31cf82e5fa325e779013619d9e11bf8b56c28.tar.bz2 |
Add garbage collection for module objects. Closes patch #102939 and
fixes bug #126345.
Diffstat (limited to 'Objects/moduleobject.c')
-rw-r--r-- | Objects/moduleobject.c | 29 |
1 files changed, 27 insertions, 2 deletions
diff --git a/Objects/moduleobject.c b/Objects/moduleobject.c index 4267896..f9c9228 100644 --- a/Objects/moduleobject.c +++ b/Objects/moduleobject.c @@ -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) @@ -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 * @@ -211,11 +213,22 @@ 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*/ @@ -223,4 +236,16 @@ PyTypeObject PyModule_Type = { (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 */ }; |