python-intelhex / intelhex

Python IntelHex library
BSD 3-Clause "New" or "Revised" License
201 stars 107 forks source link

A [0:0] slice will return the entire contents, when it should return a empty object #52

Closed mr-eg9 closed 3 years ago

mr-eg9 commented 3 years ago

Consider the following.

ih = IntelHex({0:0, 1:1, 2:2})
# The bug
print(ih[0:0].tobinarray()) # array('B', [0, 1, 2])
# Correct for other indices
print(ih[1:1].tobinarray()) # array('B', [])

# stdlib __getitem__ for reference
li = [1, 2, 3]
print(li[0:0]) # []

Looking at the source for _getitem_() this behavior happens because the endaddr 0 evaluates the same as None.

The fix should simply be:

# Current
start = addr.start or addresses[0]
stop = addr.stop or (addresses[-1]+1)
# Fixed
start = addr.start if addr.start is not None else addresses[0]
stop = addr.stop if addr.stop is not None else (addresses[-1]+1)
The-42 commented 3 years ago

Your fix looks good to me, I'll take it :)