Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 10 additions & 13 deletions intelhex/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ def fromdict(self, dikt):
if start_addr is not None:
del s['start_addr']
for k in dict_keys_g(s):
if type(k) not in IntTypes or k < 0:
if not isinstance(k, IntTypes) or k < 0:
raise ValueError('Source dictionary should have only int keys')
self._buf.update(s)
if start_addr is not None:
Expand Down Expand Up @@ -460,15 +460,14 @@ def __getitem__(self, addr):
@return byte if address exists in HEX file, or self.padding
if no data found.
'''
t = type(addr)
if t in IntTypes:
if isinstance(addr, IntTypes):
if addr < 0:
raise TypeError('Address should be >= 0.')
addresses = dict_keys(self._buf)
if not addresses or addr > max(addresses):
raise IndexError
return self._buf.get(addr, self.padding)
elif t == slice:
elif isinstance(addr, slice):
addresses = dict_keys(self._buf)
ih = IntelHex()
if addresses:
Expand All @@ -482,16 +481,15 @@ def __getitem__(self, addr):
ih[i] = x
return ih
else:
raise TypeError('Address has unsupported type: %s' % t)
raise TypeError('Address has unsupported type: %s' % type(addr))

def __setitem__(self, addr, byte):
"""Set byte at address."""
t = type(addr)
if t in IntTypes:
if isinstance(addr, IntTypes):
if addr < 0:
raise TypeError('Address should be >= 0.')
self._buf[addr] = byte
elif t == slice:
elif isinstance(addr, slice):
if not isinstance(byte, (list, tuple)):
raise ValueError('Slice operation expects sequence of bytes')
start = addr.start
Expand All @@ -517,16 +515,15 @@ def __setitem__(self, addr, byte):
self._buf[i] = byte[j]
j += 1
else:
raise TypeError('Address has unsupported type: %s' % t)
raise TypeError('Address has unsupported type: %s' % type(addr))

def __delitem__(self, addr):
"""Delete byte at address."""
t = type(addr)
if t in IntTypes:
if isinstance(addr, IntTypes):
if addr < 0:
raise TypeError('Address should be >= 0.')
del self._buf[addr]
elif t == slice:
elif isinstance(addr, slice):
addresses = dict_keys(self._buf)
if addresses:
addresses.sort()
Expand All @@ -538,7 +535,7 @@ def __delitem__(self, addr):
if x is not None:
del self._buf[i]
else:
raise TypeError('Address has unsupported type: %s' % t)
raise TypeError('Address has unsupported type: %s' % type(addr))

def __len__(self):
"""Return count of bytes with real values."""
Expand Down
20 changes: 20 additions & 0 deletions intelhex/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,16 @@ def getitem(index):
self.assertEqual({}, ih[0:0].todict())
self.assertEqual({}, ih[1:1].todict())

def test__getitem__with_subclass_of_int(self):

class SubclassOfInt(int):
pass

ih = IntelHex()
ih[0] = 0xFF
scoi : SubclassOfInt = SubclassOfInt(0)
self.assertEqual(0xFF, ih[scoi])

def test__setitem__(self):
ih = IntelHex()
# simple indexing operation
Expand Down Expand Up @@ -747,6 +757,16 @@ def setitem(a,b):
'stop address cannot be negative',
setitem, slice(0,-3,-1), [1,2,3])

def test__setitem__with_subclass_of_int(self):

class SubclassOfInt(int):
pass

ih = IntelHex()
scoi : SubclassOfInt = SubclassOfInt(0)
# simple indexing operation
ih[scoi] = 1

def test__delitem__(self):
ih = IntelHex()
ih[0] = 1
Expand Down