releasetools: Fix a bug in blockimgdiff.HeapItem.

HeapItem defines __bool__(), which contains a logical error that should
return the opposite value.

Note that the bug only manifests while using Python 3, which calls
__bool__(). With Python 2, `if x:` or bool(x) actually calls
x.__nonzero__() or x.__len__(). If a class defines neither __len__() nor
__nonzero__(), as the case in HeapItem, it always returns True.

Test: python -m unittest test_blockimgdiff
Test: python3 -m unittest test_blockimgdiff
Test: Generate an incremental non-A/B OTA package successfully.
Change-Id: Ibe8430e0b495a7d2f430cfffb716d2536ffb53d2
This commit is contained in:
Tao Bao
2017-12-23 11:50:52 -08:00
parent a52691b12a
commit 186ec99eb9
2 changed files with 44 additions and 5 deletions

View File

@@ -237,15 +237,23 @@ class Transfer(object):
class HeapItem(object):
def __init__(self, item):
self.item = item
# Negate the score since python's heap is a min-heap and we want
# the maximum score.
# Negate the score since python's heap is a min-heap and we want the
# maximum score.
self.score = -item.score
def clear(self):
self.item = None
def __bool__(self):
return self.item is None
return self.item is not None
# Python 2 uses __nonzero__, while Python 3 uses __bool__.
__nonzero__ = __bool__
# The rest operations are generated by functools.total_ordering decorator.
def __eq__(self, other):
return self.score == other.score
def __le__(self, other):
return self.score <= other.score