More memory leak fixes from Brett! It turns out PyDict_SetItemString
authorTim Potter <tpot@samba.org>
Tue, 29 Jul 2003 00:15:23 +0000 (00:15 +0000)
committerTim Potter <tpot@samba.org>
Tue, 29 Jul 2003 00:15:23 +0000 (00:15 +0000)
didn't behave exactly as I thought it did.  If you create an item using a
PyFoo_FromBar function you must decrement the reference to that object
afterwards (or use Py_BuildValue).

source/python/py_ntsec.c

index 47524d8e19048cdfa21be4d9e06a5cc7718d1f0a..3d408e0bda230cc3c7a4901be8d90433e4aee27c 100644 (file)
@@ -58,14 +58,14 @@ BOOL py_from_ACE(PyObject **dict, SEC_ACE *ace)
                return True;
        }
 
-       *dict = PyDict_New();
-
-       PyDict_SetItemString(*dict, "type", PyInt_FromLong(ace->type));
-       PyDict_SetItemString(*dict, "flags", PyInt_FromLong(ace->flags));
-       PyDict_SetItemString(*dict, "mask", PyInt_FromLong(ace->info.mask));
+       *dict = Py_BuildValue("{sisisi}", "type", ace->type,
+                               "flags", ace->flags,
+                               "mask", ace->info.mask);
 
-       if (py_from_SID(&obj, &ace->trustee))
+       if (py_from_SID(&obj, &ace->trustee)) {
                PyDict_SetItemString(*dict, "trustee", obj);
+               Py_DECREF(obj);
+       }
 
        return True;
 }
@@ -125,10 +125,6 @@ BOOL py_from_ACL(PyObject **dict, SEC_ACL *acl)
                return True;
        }
 
-       *dict = PyDict_New();
-
-       PyDict_SetItemString(*dict, "revision", PyInt_FromLong(acl->revision));
-
        ace_list = PyList_New(acl->num_aces);
 
        for (i = 0; i < acl->num_aces; i++) {
@@ -138,7 +134,8 @@ BOOL py_from_ACL(PyObject **dict, SEC_ACL *acl)
                        PyList_SetItem(ace_list, i, obj);
        }
 
-       PyDict_SetItemString(*dict, "ace_list", ace_list);
+       *dict = Py_BuildValue("{sisN}", "revision", acl->revision,
+                       "ace_list", ace_list);
 
        return True;
 }
@@ -181,19 +178,29 @@ BOOL py_from_SECDESC(PyObject **dict, SEC_DESC *sd)
 
        *dict = PyDict_New();
 
-       PyDict_SetItemString(*dict, "revision", PyInt_FromLong(sd->revision));
+       obj = PyInt_FromLong(sd->revision);
+       PyDict_SetItemString(*dict, "revision", obj);
+       Py_DECREF(obj);
 
-       if (py_from_SID(&obj, sd->owner_sid))
+       if (py_from_SID(&obj, sd->owner_sid)) {
                PyDict_SetItemString(*dict, "owner_sid", obj);
+               Py_DECREF(obj);
+       }
 
-       if (py_from_SID(&obj, sd->grp_sid))
+       if (py_from_SID(&obj, sd->grp_sid)) {
                PyDict_SetItemString(*dict, "group_sid", obj);
+               Py_DECREF(obj);
+       }
 
-       if (py_from_ACL(&obj, sd->dacl))
+       if (py_from_ACL(&obj, sd->dacl)) {
                PyDict_SetItemString(*dict, "dacl", obj);
+               Py_DECREF(obj);
+       }
 
-       if (py_from_ACL(&obj, sd->sacl))
+       if (py_from_ACL(&obj, sd->sacl)) {
                PyDict_SetItemString(*dict, "sacl", obj);
+               Py_DECREF(obj);
+       }
 
        return True;
 }