python: Fix reference counting of ContentStat in XmlFile.
authorTomas Mlcoch <tmlcoch@redhat.com>
Thu, 13 Jun 2013 10:09:07 +0000 (12:09 +0200)
committerTomas Mlcoch <tmlcoch@redhat.com>
Thu, 13 Jun 2013 10:09:07 +0000 (12:09 +0200)
src/python/xml_file-py.c
tests/python/tests/test_contentstat.py

index db0089375f615d41813b4021693dd69df66760d7..06cecd1ce99c318c8f5af0d2c4cc97ff3a75fae1 100644 (file)
@@ -30,7 +30,7 @@
 typedef struct {
     PyObject_HEAD
     cr_XmlFile *xmlfile;
-    cr_ContentStat *stat;
+    PyObject *py_stat;
 } _XmlFileObject;
 
 static PyObject * xmlfile_close(_XmlFileObject *self, void *nothing);
@@ -58,7 +58,7 @@ xmlfile_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
     _XmlFileObject *self = (_XmlFileObject *)type->tp_alloc(type, 0);
     if (self) {
         self->xmlfile = NULL;
-        self->stat = NULL;
+        self->py_stat = NULL;
     }
     return (PyObject *)self;
 }
@@ -101,8 +101,8 @@ xmlfile_init(_XmlFileObject *self, PyObject *args, PyObject *kwds)
     /* Free all previous resources when reinitialization */
     ret = xmlfile_close(self, NULL);
     Py_XDECREF(ret);
-    Py_XDECREF(self->stat);
-    self->stat = NULL;
+    Py_XDECREF(self->py_stat);
+    self->py_stat = NULL;
     if (ret == NULL) {
         // Error encountered!
         return -1;
@@ -115,8 +115,9 @@ xmlfile_init(_XmlFileObject *self, PyObject *args, PyObject *kwds)
         g_clear_error(&err);
         return -1;
     }
-    self->stat = stat;
-    Py_XINCREF(stat);
+
+    self->py_stat = py_stat;
+    Py_XINCREF(py_stat);
 
     return 0;
 }
@@ -125,7 +126,7 @@ static void
 xmlfile_dealloc(_XmlFileObject *self)
 {
     cr_xmlfile_close(self->xmlfile, NULL);
-    Py_XDECREF(self->stat);
+    Py_XDECREF(self->py_stat);
     Py_TYPE(self)->tp_free(self);
 }
 
@@ -226,17 +227,18 @@ xmlfile_close(_XmlFileObject *self, void *nothing)
 
     CR_UNUSED(nothing);
 
-    Py_XDECREF(self->stat);
-    self->stat = NULL;
-
     if (self->xmlfile) {
         cr_xmlfile_close(self->xmlfile, &err);
         self->xmlfile = NULL;
-        if (err) {
-            PyErr_Format(CrErr_Exception, "Error while closing: %s", err->message);
-            g_clear_error(&err);
-            return NULL;
-        }
+    }
+
+    Py_XDECREF(self->py_stat);
+    self->py_stat = NULL;
+
+    if (err) {
+        PyErr_Format(CrErr_Exception, "Error while closing: %s", err->message);
+        g_clear_error(&err);
+        return NULL;
     }
 
     Py_RETURN_NONE;
index c8849f1fcd1883dbe7df41ce70a2607d34bd9c04..17be652719160c6495c7ca7febd2b02d57d896e2 100644 (file)
@@ -43,3 +43,26 @@ class TestCaseContentStat(unittest.TestCase):
         self.assertEqual(cs.checksum, "67bc6282915fad80dc11f3d7c3210977a0bde"\
                                       "05a762256d86083c2447d425776")
 
+    def test_contentstat_2(self):
+        """Test if reference is saved properly"""
+
+        pkg = cr.package_from_rpm(PKG_ARCHER_PATH)
+        self.assertTrue(pkg)
+
+        pkg.time_file = 1
+        pkg.time_build = 1
+
+        cs = cr.ContentStat(cr.SHA256)
+        self.assertEqual(cs.size, 0)
+        self.assertEqual(cs.checksum_type, cr.SHA256)
+        self.assertEqual(cs.checksum, None)
+
+        path = os.path.join(self.tmpdir, "primary.xml.gz")
+        f = cr.PrimaryXmlFile(path, cr.GZ_COMPRESSION, cs)
+        self.assertTrue(f)
+        self.assertTrue(os.path.isfile(path))
+        del cs
+        f.add_pkg(pkg)
+        f.close()
+
+        self.assertTrue(os.path.isfile(path))