[Python-checkins] CVS: python/dist/src/Objects moduleobject.c,2.30,2.31

Neil Schemenauer python-dev@python.org
Tue, 02 Jan 2001 07:58:30 -0800


Update of /cvsroot/python/python/dist/src/Objects
In directory usw-pr-cvs1:/tmp/cvs-serv10766/Objects

Modified Files:
	moduleobject.c 
Log Message:
Add garbage collection for module objects.  Closes patch #102939 and 
fixes bug #126345.


Index: moduleobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/moduleobject.c,v
retrieving revision 2.30
retrieving revision 2.31
diff -C2 -r2.30 -r2.31
*** moduleobject.c	2000/10/24 19:57:45	2.30
--- moduleobject.c	2001/01/02 15:58:27	2.31
***************
*** 19,22 ****
--- 19,23 ----
  	nameobj = PyString_FromString(name);
  	m->md_dict = PyDict_New();
+ 	PyObject_GC_Init(m);
  	if (m->md_dict == NULL || nameobj == NULL)
  		goto fail;
***************
*** 131,139 ****
  module_dealloc(PyModuleObject *m)
  {
  	if (m->md_dict != NULL) {
  		_PyModule_Clear((PyObject *)m);
  		Py_DECREF(m->md_dict);
  	}
! 	PyObject_DEL(m);
  }
  
--- 132,141 ----
  module_dealloc(PyModuleObject *m)
  {
+ 	PyObject_GC_Fini(m);
  	if (m->md_dict != NULL) {
  		_PyModule_Clear((PyObject *)m);
  		Py_DECREF(m->md_dict);
  	}
! 	PyObject_DEL(PyObject_AS_GC(m));
  }
  
***************
*** 212,220 ****
  }
  
  PyTypeObject PyModule_Type = {
  	PyObject_HEAD_INIT(&PyType_Type)
  	0,			/*ob_size*/
  	"module",		/*tp_name*/
! 	sizeof(PyModuleObject),	/*tp_size*/
  	0,			/*tp_itemsize*/
  	(destructor)module_dealloc, /*tp_dealloc*/
--- 214,233 ----
  }
  
+ /* 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) + PyGC_HEAD_SIZE,	/*tp_size*/
  	0,			/*tp_itemsize*/
  	(destructor)module_dealloc, /*tp_dealloc*/
***************
*** 224,226 ****
--- 237,251 ----
  	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 */
  };