Move allocations out of rpmds object init method
authorPanu Matilainen <pmatilai@redhat.com>
Tue, 22 Sep 2009 15:53:15 +0000 (18:53 +0300)
committerPanu Matilainen <pmatilai@redhat.com>
Tue, 22 Sep 2009 15:53:15 +0000 (18:53 +0300)
- tp_init can be called several times, allocating from there leaks memory
- tp_init gets called automatically on object creation, dont call manually

python/rpmds-py.c

index 63f52e9..223c27d 100644 (file)
@@ -475,29 +475,7 @@ static PyMappingMethods rpmds_as_mapping = {
  */
 static int rpmds_init(rpmdsObject * s, PyObject *args, PyObject *kwds)
 {
-    hdrObject * ho = NULL;
-    PyObject * to = NULL;
-    rpmTag tagN = RPMTAG_REQUIRENAME;
-    rpmsenseFlags flags = 0;
-    char * kwlist[] = {"header", "tag", "flags", NULL};
-
-if (_rpmds_debug < 0)
-fprintf(stderr, "*** rpmds_init(%p,%p,%p)\n", s, args, kwds);
-
-    if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!|Oi:rpmds_init", kwlist, 
-           &hdr_Type, &ho, &to, &flags))
-       return -1;
-
-    if (to != NULL) {
-       tagN = tagNumFromPyObject(to);
-       if (tagN == -1) {
-           PyErr_SetString(PyExc_KeyError, "unknown header tag");
-           return -1;
-       }
-    }
-    s->ds = rpmdsNew(hdrGetHeader(ho), tagN, 0);
     s->active = 0;
-
     return 0;
 }
 
@@ -517,15 +495,24 @@ fprintf(stderr, "%p -- ds %p\n", s, s->ds);
 static PyObject * rpmds_new(PyTypeObject * subtype, PyObject *args, PyObject *kwds)
 {
     rpmdsObject * s = (void *) PyObject_New(rpmdsObject, subtype);
+    hdrObject * ho = NULL;
+    PyObject * to = NULL;
+    rpmTag tagN = RPMTAG_REQUIRENAME;
+    rpmsenseFlags flags = 0;
+    char * kwlist[] = {"header", "tag", "flags", NULL};
 
-    /* Perform additional initialization. */
-    if (rpmds_init(s, args, kwds) < 0) {
-       rpmds_free(s);
+    if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!|Oi:rpmds_new", kwlist, 
+           &hdr_Type, &ho, &to, &flags))
        return NULL;
-    }
 
-if (_rpmds_debug)
-fprintf(stderr, "%p ++ ds %p\n", s, s->ds);
+    if (to != NULL) {
+       tagN = tagNumFromPyObject(to);
+       if (tagN == -1) {
+           PyErr_SetString(PyExc_KeyError, "unknown header tag");
+           return NULL;
+       }
+    }
+    s->ds = rpmdsNew(hdrGetHeader(ho), tagN, 0);
 
     return (PyObject *)s;
 }