richardphibel / rpms / rpm

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