diff --git a/intelhex/__init__.py b/intelhex/__init__.py index 155f17f..0c15c7d 100644 --- a/intelhex/__init__.py +++ b/intelhex/__init__.py @@ -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: @@ -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: @@ -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 @@ -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() @@ -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.""" diff --git a/intelhex/test.py b/intelhex/test.py index 2aff575..f12934c 100755 --- a/intelhex/test.py +++ b/intelhex/test.py @@ -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 @@ -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