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