Fix python ts.addErase() not raising exception on not-found packages

The code would only raise an exception if TransactionSetCore.addErase()
returned an error, but the catch is that with many kinds of argument
types we'd silently skip the whole addition because no headers were found.
This looks to be a regression introduced some eleven years ago in
commit 9b20c706a4.

As a special case, a match iterator argument will not raise an exception
if it doesn't actually match anything.

Fixes: #1214
This commit is contained in:
Panu Matilainen 2020-06-04 11:18:01 +03:00 committed by Florian Festi
parent 25e856a6d4
commit 09a3d5c23b
2 changed files with 37 additions and 11 deletions

View File

@ -85,14 +85,22 @@ class TransactionSet(TransactionSetCore):
def addErase(self, item): def addErase(self, item):
hdrs = [] hdrs = []
if isinstance(item, rpm.hdr): # match iterators are passed on as-is
hdrs = [item] if isinstance(item, rpm.mi):
elif isinstance(item, rpm.mi):
hdrs = item hdrs = item
elif isinstance(item, int): elif isinstance(item, rpm.hdr):
hdrs = self.dbMatch(rpm.RPMDBI_PACKAGES, item) hdrs.append(item)
elif isinstance(item, str): elif isinstance(item, (int, str)):
hdrs = self.dbMatch(rpm.RPMDBI_LABEL, item) if isinstance(item, int):
dbi = rpm.RPMDBI_PACKAGES
else:
dbi = rpm.RPMDBI_LABEL
for h in self.dbMatch(dbi, item):
hdrs.append(h)
if not hdrs:
raise rpm.error("package not installed")
else: else:
raise TypeError("invalid type %s" % type(item)) raise TypeError("invalid type %s" % type(item))
@ -100,10 +108,6 @@ class TransactionSet(TransactionSetCore):
if not TransactionSetCore.addErase(self, h): if not TransactionSetCore.addErase(self, h):
raise rpm.error("adding erasure to transaction failed") raise rpm.error("adding erasure to transaction failed")
# garbage collection should take care but just in case...
if isinstance(hdrs, rpm.mi):
del hdrs
def run(self, callback, data): def run(self, callback, data):
rc = TransactionSetCore.run(self, callback, data, self._probFilter) rc = TransactionSetCore.run(self, callback, data, self._probFilter)

View File

@ -259,6 +259,28 @@ for e in ts:
[foo-1.0-1.noarch] [foo-1.0-1.noarch]
) )
RPMPY_TEST([add erasure to transaction],[
ts = rpm.ts()
for i in ['foo', 1234]:
myprint('addErase %s' % i)
try:
ts.addErase(i)
except rpm.error as err:
myprint(err)
myprint('addErase mi')
mi = ts.dbMatch('name', 'foo')
try:
ts.addErase(mi)
except rpm.error as err:
myprint(err)
],
[addErase foo
package not installed
addErase 1234
package not installed
addErase mi]
)
RPMPY_TEST([add bogus package to transaction 1],[ RPMPY_TEST([add bogus package to transaction 1],[
ts = rpm.ts() ts = rpm.ts()
h = rpm.hdr() h = rpm.hdr()