Skip to content

Commit 8ef5f32

Browse files
author
Léo Flaventin Hauchecorne
committed
Added support for bytes in sliced __setitem__
1 parent 5c15003 commit 8ef5f32

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

intelhex/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,8 +480,10 @@ def __setitem__(self, addr, byte):
480480
raise TypeError('Address should be >= 0.')
481481
self._buf[addr] = byte
482482
elif t == slice:
483+
if isinstance(byte, bytes) :
484+
byte = list(byte)
483485
if not isinstance(byte, (list, tuple)):
484-
raise ValueError('Slice operation expects sequence of bytes')
486+
raise ValueError('Slice operation expects a bytes or a sequence of byte values')
485487
start = addr.start
486488
stop = addr.stop
487489
step = addr.step or 1

intelhex/test.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -687,10 +687,12 @@ def setitem(a,b):
687687
ih = IntelHex()
688688
ih[0:8:2] = range_l(4)
689689
self.assertEqual({0:0, 2:1, 4:2, 6:3}, ih.todict())
690+
ih[0:8:2] = b'\xDE\xAD\xBE\xEF'
691+
self.assertEqual({0:0xDE, 2:0xAD, 4:0xBE, 6:0xEF}, ih.todict())
690692
# errors in slice operations
691693
# ih[1:2] = 'a'
692694
self.assertRaisesMsg(ValueError,
693-
'Slice operation expects sequence of bytes',
695+
'Slice operation expects a bytes or a sequence of byte values',
694696
setitem, slice(1,2,None), 'a')
695697
# ih[0:1] = [1,2,3]
696698
self.assertRaisesMsg(ValueError,

0 commit comments

Comments
 (0)