commit 62bdcc0a32d07a7423876b3ae17900da04eb8a97 Author: Jan Silhan Date: Fri Oct 18 18:30:52 2013 +0200 Python 3 compatibility fixes - Use open() instead of calling file constructor - Borrow python-six trick of dealing with difference in string types Signed-off-by: Panu Matilainen diff --git a/python/rpm/transaction.py b/python/rpm/transaction.py index 756e893..df72ce9 100644 --- a/python/rpm/transaction.py +++ b/python/rpm/transaction.py @@ -1,8 +1,14 @@ #!/usr/bin/python +import sys import rpm from rpm._rpm import ts as TransactionSetCore +if sys.version_info[0] == 3: + _string_types = str, +else: + _string_types = basestring, + # TODO: migrate relevant documentation from C-side class TransactionSet(TransactionSetCore): _probFilter = 0 @@ -45,14 +51,14 @@ class TransactionSet(TransactionSetCore): return tuple(keys) def addInstall(self, item, key, how="u"): - if isinstance(item, basestring): - f = file(item) + if isinstance(item, _string_types): + f = open(item) header = self.hdrFromFdno(f) f.close() - elif isinstance(item, file): - header = self.hdrFromFdno(item) - else: + elif isinstance(item, rpm.hdr): header = item + else: + header = self.hdrFromFdno(item) if not how in ['u', 'i']: raise ValueError('how argument must be "u" or "i"') @@ -69,7 +75,7 @@ class TransactionSet(TransactionSetCore): hdrs = item elif isinstance(item, int): hdrs = self.dbMatch(rpm.RPMDBI_PACKAGES, item) - elif isinstance(item, basestring): + elif isinstance(item, _string_types): hdrs = self.dbMatch(rpm.RPMDBI_LABEL, item) else: raise TypeError("invalid type %s" % type(item))