]> granicus.if.org Git - python/commitdiff
List objects obtained through as_List(resource) are not auto-disposed upon
authorJack Jansen <jack.jansen@cwi.nl>
Thu, 13 Jan 2000 16:23:39 +0000 (16:23 +0000)
committerJack Jansen <jack.jansen@cwi.nl>
Thu, 13 Jan 2000 16:23:39 +0000 (16:23 +0000)
Python object freeing.

Mac/Modules/list/Listmodule.c
Mac/Modules/list/listsupport.py

index c9091e6b35504ff35110c6f78e8a01c70ae77973..0993ad9c66c04d4532fbe804e596a4e257d4967e 100644 (file)
@@ -58,6 +58,7 @@ PyTypeObject List_Type;
 typedef struct ListObject {
        PyObject_HEAD
        ListHandle ob_itself;
+       int ob_must_be_disposed;
 } ListObject;
 
 PyObject *ListObj_New(itself)
@@ -71,6 +72,7 @@ PyObject *ListObj_New(itself)
        it = PyObject_NEW(ListObject, &List_Type);
        if (it == NULL) return NULL;
        it->ob_itself = itself;
+       it->ob_must_be_disposed = 1;
        return (PyObject *)it;
 }
 ListObj_Convert(v, p_itself)
@@ -89,7 +91,7 @@ ListObj_Convert(v, p_itself)
 static void ListObj_dealloc(self)
        ListObject *self;
 {
-       LDispose(self->ob_itself);
+       if (self->ob_must_be_disposed && self->ob_itself) LDispose(self->ob_itself);
        PyMem_DEL(self);
 }
 
@@ -686,22 +688,22 @@ static PyObject *List_as_List(_self, _args)
        PyObject *_args;
 {
        PyObject *_res = NULL;
-       ListHandle _rv;
+
        Handle h;
-       if (!PyArg_ParseTuple(_args, "O&",
-                             ResObj_Convert, &h))
+       ListObject *l;
+       if (!PyArg_ParseTuple(_args, "O&", ResObj_Convert, &h))
                return NULL;
-       _rv = as_List(h);
-       _res = Py_BuildValue("O&",
-                            ListObj_New, _rv);
-       return _res;
+       l = (ListObject *)ListObj_New(as_List(h));
+       l->ob_must_be_disposed = 0;
+       return Py_BuildValue("O", l);
+
 }
 
 static PyMethodDef List_methods[] = {
        {"LNew", (PyCFunction)List_LNew, 1,
         "(Rect rView, Rect dataBounds, Point cSize, short theProc, WindowPtr theWindow, Boolean drawIt, Boolean hasGrow, Boolean scrollHoriz, Boolean scrollVert) -> (ListHandle _rv)"},
        {"as_List", (PyCFunction)List_as_List, 1,
-        "(Handle h) -> (ListHandle _rv)"},
+        "(Resource)->List.\nReturns List object (which is not auto-freed!)"},
        {NULL, NULL, 0}
 };
 
index 4fd7d7e8c83f4a7b9b233058906678607072bafe..eca08cbeb0062b60da976ecd5c3b4a8749021d00 100644 (file)
@@ -83,13 +83,23 @@ ListObj_setattr(self, name, value)
 
 
 class MyObjectDefinition(GlobalObjectDefinition):
+
+       def outputStructMembers(self):
+               ObjectDefinition.outputStructMembers(self)
+               Output("int ob_must_be_disposed;")
+
        def outputCheckNewArg(self):
                Output("""if (itself == NULL) {
                                        PyErr_SetString(List_Error,"Cannot create null List");
                                        return NULL;
                                }""")
+                               
+       def outputInitStructMembers(self):
+               ObjectDefinition.outputInitStructMembers(self)
+               Output("it->ob_must_be_disposed = 1;")
+
        def outputFreeIt(self, itselfname):
-               Output("LDispose(%s);", itselfname)
+               Output("if (self->ob_must_be_disposed && %s) LDispose(%s);", itselfname, itselfname)
                
        def outputGetattrHook(self):
                Output(getattrHookCode)
@@ -114,7 +124,18 @@ methods = []
 execfile(INPUTFILE)
 
 # Function to convert any handle to a list and vv.
-f = Function(ListHandle, 'as_List', (Handle, 'h', InMode))
+##f = Function(ListHandle, 'as_List', (Handle, 'h', InMode))
+as_List_body = """
+Handle h;
+ListObject *l;
+if (!PyArg_ParseTuple(_args, "O&", ResObj_Convert, &h))
+       return NULL;
+l = (ListObject *)ListObj_New(as_List(h));
+l->ob_must_be_disposed = 0;
+return Py_BuildValue("O", l);
+"""
+f = ManualGenerator("as_List", as_List_body)
+f.docstring = lambda: "(Resource)->List.\nReturns List object (which is not auto-freed!)"
 functions.append(f)
 
 f = Method(Handle, 'as_Resource', (ListHandle, 'lh', InMode))