teknoraver / rpms / rpm

Forked from rpms/rpm 4 months ago
Clone

Blame SOURCES/0001-Fix-python-ts.addErase-not-raising-exception-on-not-.patch

b445f2
From 60066aba510b3ff4a7db092021aae71948e3f8be Mon Sep 17 00:00:00 2001
b445f2
From: Panu Matilainen <pmatilai@redhat.com>
b445f2
Date: Thu, 4 Jun 2020 11:18:01 +0300
b445f2
Subject: [PATCH] Fix python ts.addErase() not raising exception on not-found
b445f2
 packages
b445f2
b445f2
The code would only raise an exception if TransactionSetCore.addErase()
b445f2
returned an error, but the catch is that with many kinds of argument
b445f2
types we'd silently skip the whole addition because no headers were found.
b445f2
This looks to be a regression introduced some eleven years ago in
b445f2
commit 9b20c706a4f93266450fae2f94007343b2e8fd9e.
b445f2
b445f2
As a special case, a match iterator argument will not raise an exception
b445f2
if it doesn't actually match anything.
b445f2
b445f2
Fixes: #1214
b445f2
---
b445f2
 python/rpm/transaction.py | 26 +++++++++++++++-----------
b445f2
 tests/rpmpython.at        | 22 ++++++++++++++++++++++
b445f2
 2 files changed, 37 insertions(+), 11 deletions(-)
b445f2
b445f2
diff --git a/python/rpm/transaction.py b/python/rpm/transaction.py
b445f2
index 7c4a551d3..3c9ddb207 100644
b445f2
--- a/python/rpm/transaction.py
b445f2
+++ b/python/rpm/transaction.py
b445f2
@@ -91,14 +91,22 @@ class TransactionSet(TransactionSetCore):
b445f2
 
b445f2
     def addErase(self, item):
b445f2
         hdrs = []
b445f2
-        if isinstance(item, rpm.hdr):
b445f2
-            hdrs = [item]
b445f2
-        elif isinstance(item, rpm.mi):
b445f2
+        # match iterators are passed on as-is
b445f2
+        if isinstance(item, rpm.mi):
b445f2
             hdrs = item
b445f2
-        elif isinstance(item, int):
b445f2
-            hdrs = self.dbMatch(rpm.RPMDBI_PACKAGES, item)
b445f2
-        elif isinstance(item, _string_types):
b445f2
-            hdrs = self.dbMatch(rpm.RPMDBI_LABEL, item)
b445f2
+        elif isinstance(item, rpm.hdr):
b445f2
+            hdrs.append(item)
b445f2
+        elif isinstance(item, (int, _string_types)):
b445f2
+            if isinstance(item, int):
b445f2
+                dbi = rpm.RPMDBI_PACKAGES
b445f2
+            else:
b445f2
+                dbi = rpm.RPMDBI_LABEL
b445f2
+
b445f2
+            for h in self.dbMatch(dbi, item):
b445f2
+                hdrs.append(h)
b445f2
+
b445f2
+            if not hdrs:
b445f2
+                raise rpm.error("package not installed")
b445f2
         else:
b445f2
             raise TypeError("invalid type %s" % type(item))
b445f2
 
b445f2
@@ -106,10 +114,6 @@ class TransactionSet(TransactionSetCore):
b445f2
             if not TransactionSetCore.addErase(self, h):
b445f2
                 raise rpm.error("package not installed")
b445f2
 
b445f2
-        # garbage collection should take care but just in case...
b445f2
-        if isinstance(hdrs, rpm.mi):
b445f2
-            del hdrs
b445f2
-
b445f2
     def run(self, callback, data):
b445f2
         rc = TransactionSetCore.run(self, callback, data, self._probFilter)
b445f2
 
b445f2
diff --git a/tests/rpmpython.at b/tests/rpmpython.at
b445f2
index 3a7c251f1..de39c8417 100644
b445f2
--- a/tests/rpmpython.at
b445f2
+++ b/tests/rpmpython.at
b445f2
@@ -201,6 +201,28 @@ for e in ts:
b445f2
 [foo-1.0-1.noarch]
b445f2
 )
b445f2
 
b445f2
+RPMPY_TEST([add erasure to transaction],[
b445f2
+ts = rpm.ts()
b445f2
+for i in ['foo', 1234]:
b445f2
+    myprint('addErase %s' % i)
b445f2
+    try:
b445f2
+        ts.addErase(i)
b445f2
+    except rpm.error as err:
b445f2
+        myprint(err)
b445f2
+myprint('addErase mi')
b445f2
+mi = ts.dbMatch('name', 'foo')
b445f2
+try:
b445f2
+    ts.addErase(mi)
b445f2
+except rpm.error as err:
b445f2
+    myprint(err)
b445f2
+],
b445f2
+[addErase foo
b445f2
+package not installed
b445f2
+addErase 1234
b445f2
+package not installed
b445f2
+addErase mi]
b445f2
+)
b445f2
+
b445f2
 RPMPY_TEST([add bogus package to transaction 1],[
b445f2
 ts = rpm.ts()
b445f2
 h = rpm.hdr()
b445f2
-- 
b445f2
2.26.2
b445f2