Skip to content

Commit c79f949

Browse files
author
Léo Flaventin Hauchecorne
committed
.
1 parent 8ef5f32 commit c79f949

12 files changed

+45
-13
lines changed

AUTHORS.rst

+1
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ Contributors:
2222
* "durexyl" @ GitHub
2323
* "erki1993" @ GitHub
2424
* "mentaal" @ GitHub
25+
* Léo Flaventin Hauchecorne

NEWS.rst

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
IntelHex releases
33
*****************
44

5+
2.3 (2020-09-09)
6+
------------------
7+
* API changes: ``IntelHex.__setitem__`` method: added support for slices
8+
with any iterable values. (Léo Flaventin Hauchecorne)
9+
510
2.2.1 (2018-01-30)
611
------------------
712
* Fixes for PyPI.

Readme.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ restrictions.
2424

2525
Supported Python versions
2626
-------------------------
27-
IntelHex library v.2.2 supports Python 2 (2.4-2.7) and Python 3 (3.2-3.5 or later)
27+
IntelHex library v.2.3 supports Python 2 (2.4-2.7) and Python 3 (3.2-3.5 or later)
2828
without external libraries or 2to3 tool from the same codebase.
2929

3030
Install

intelhex/__init__.py

+16-4
Original file line numberDiff line numberDiff line change
@@ -480,10 +480,22 @@ 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)
485-
if not isinstance(byte, (list, tuple)):
486-
raise ValueError('Slice operation expects a bytes or a sequence of byte values')
483+
slice_error = ValueError('Slice operation expects either a bytes, a sequence of byte values (0 <= byte <= 255), or anything convertible to bytes') # avoid duplication code
484+
if not isinstance(byte, (bytes, bytearray)):
485+
if isinstance(byte, (list, tuple)):
486+
try:
487+
check = all(0 <= x < 255 for x in byte)
488+
except TypeError as exc:
489+
raise slice_error from exc
490+
if not check :
491+
raise slice_error
492+
if not all(isinstance(x, int) for x in byte) :
493+
raise slice_error
494+
else :
495+
try:
496+
byte = bytes(byte) # check for int + 0<=x<=255 + convert to something getitem works with
497+
except TypeError as exc:
498+
raise slice_error from exc
487499
start = addr.start
488500
stop = addr.stop
489501
step = addr.step or 1

intelhex/__version__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# IntelHex library version information
2-
version_info = (2, 2, 1)
2+
version_info = (2, 3)
33
version_str = '.'.join([str(i) for i in version_info])

intelhex/test.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,8 @@ def setitem(a,b):
679679
# slice operations
680680
ih[0:4] = range_l(4)
681681
self.assertEqual({0:0, 1:1, 2:2, 3:3}, ih.todict())
682+
ih[0:4] = range_g(1,5)
683+
self.assertEqual({0:1, 1:2, 2:3, 3:4}, ih.todict())
682684
ih[0:] = range_l(5,9)
683685
self.assertEqual({0:5, 1:6, 2:7, 3:8}, ih.todict())
684686
ih[:4] = range_l(9,13)
@@ -692,8 +694,20 @@ def setitem(a,b):
692694
# errors in slice operations
693695
# ih[1:2] = 'a'
694696
self.assertRaisesMsg(ValueError,
695-
'Slice operation expects a bytes or a sequence of byte values',
697+
'Slice operation expects either a bytes, a sequence of byte values (0 <= byte <= 255), or anything convertible to bytes',
696698
setitem, slice(1,2,None), 'a')
699+
self.assertRaisesMsg(ValueError,
700+
'Slice operation expects either a bytes, a sequence of byte values (0 <= byte <= 255), or anything convertible to bytes',
701+
setitem, slice(1,4,None), [1,1.5,2])
702+
self.assertRaisesMsg(ValueError,
703+
'Slice operation expects either a bytes, a sequence of byte values (0 <= byte <= 255), or anything convertible to bytes',
704+
setitem, slice(1,4,None), [1,object(),2])
705+
self.assertRaisesMsg(ValueError,
706+
'Slice operation expects either a bytes, a sequence of byte values (0 <= byte <= 255), or anything convertible to bytes',
707+
setitem, slice(1,2,None), (i/2 for i in range(1)))
708+
self.assertRaisesMsg(ValueError,
709+
'Slice operation expects either a bytes, a sequence of byte values (0 <= byte <= 255), or anything convertible to bytes',
710+
setitem, slice(1,4,None), [1,2,256])
697711
# ih[0:1] = [1,2,3]
698712
self.assertRaisesMsg(ValueError,
699713
'Length of bytes sequence does not match address range',

scripts/bin2hex.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
'''Intel HEX file format bin2hex convertor utility.'''
3737

38-
VERSION = '2.2.1'
38+
VERSION = '2.3'
3939

4040
if __name__ == '__main__':
4141
import getopt

scripts/hex2bin.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
'''Intel HEX file format hex2bin convertor utility.'''
3737

38-
VERSION = '2.2.1'
38+
VERSION = '2.3'
3939

4040
if __name__ == '__main__':
4141
import getopt

scripts/hex2dump.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
"""Show content of hex file as hexdump."""
3737

38-
VERSION = '2.2.1'
38+
VERSION = '2.3'
3939

4040
USAGE = '''hex2dump: show content of hex file as hexdump.
4141
Usage:

scripts/hexdiff.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
of compared data.
3838
"""
3939

40-
VERSION = '2.2.1'
40+
VERSION = '2.3'
4141

4242
USAGE = '''hexdiff: diff dumps of 2 hex files.
4343
Usage:

scripts/hexinfo.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
data (if any), in YAML format.
3939
"""
4040

41-
VERSION = '2.2.1'
41+
VERSION = '2.3'
4242

4343
USAGE = '''hexinfo: summarize a hex file's contents.
4444
Usage:

scripts/hexmerge.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
"""Merge content of several hex files into one file."""
3737

38-
VERSION = '2.2.1'
38+
VERSION = '2.3'
3939

4040
USAGE = '''hexmerge: merge content of hex files.
4141
Usage:

0 commit comments

Comments
 (0)