Skip to content

Added support for bytes in sliced __setitem__ #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
Binary file added .coverage
Binary file not shown.
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ Contributors:
* "durexyl" @ GitHub
* "erki1993" @ GitHub
* "mentaal" @ GitHub
* Léo Flaventin Hauchecorne
5 changes: 5 additions & 0 deletions NEWS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
IntelHex releases
*****************

2.3 (2020-09-09)
------------------
* API changes: ``IntelHex.__setitem__`` method: added support for slices
with any iterable values. (Léo Flaventin Hauchecorne)

2.2.1 (2018-01-30)
------------------
* Fixes for PyPI.
Expand Down
2 changes: 1 addition & 1 deletion Readme.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ restrictions.

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

Install
Expand Down
75 changes: 75 additions & 0 deletions intelhex.egg-info/PKG-INFO
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
Metadata-Version: 1.2
Name: intelhex
Version: 2.3
Summary: Python library for Intel HEX files manipulations
Home-page: https://github.com/python-intelhex/intelhex
Author: Alexander Belchenko
Author-email: [email protected]
Maintainer: Bert van Hall
Maintainer-email: [email protected]
License: BSD
Description: Python IntelHex library
***********************

Introduction
------------
The Intel HEX file format is widely used in microprocessors and microcontrollers
area (embedded systems etc) as the de facto standard
for representation of code to be programmed into microelectronic devices.

This work implements an ``intelhex`` Python library to read, write,
create from scratch and manipulate data from Intel HEX file format.

The distribution package also includes several convenience Python scripts,
including "classic" ``hex2bin`` and ``bin2hex`` converters and more,
those based on the library itself. Check the docs to know more.

License
-------
The code is distributed under BSD license,
see `LICENSE.txt <https://github.com/python-intelhex/intelhex/blob/master/LICENSE.txt>`_.

In short: you can use IntelHex library in your project without *any*
restrictions.

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

Install
-------
Install using ``pip`` (recommended, no separate download required)::

pip install intelhex

Download
--------
* https://pypi.org/project/IntelHex/
* https://github.com/python-intelhex/intelhex/releases

Source code, bug reports, patches
---------------------------------
IntelHex on GitHub:

https://github.com/python-intelhex/intelhex

User manual
-----------
User manual for IntelHex is available in the sources ``docs/manual/`` directory.
You can browse User Manual online:

https://readthedocs.org/projects/python-intelhex/

Changelog
---------
See `NEWS.rst <https://github.com/python-intelhex/intelhex/blob/master/NEWS.rst>`_

Keywords: Intel HEX hex2bin HEX8
Platform: UNKNOWN
Classifier: Development Status :: 5 - Production/Stable
Classifier: Environment :: Console
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: BSD License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python
24 changes: 24 additions & 0 deletions intelhex.egg-info/SOURCES.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
AUTHORS.rst
LICENSE.txt
MANIFEST.in
NEWS.rst
Readme.rst
setup.cfg
setup.py
intelhex/__init__.py
intelhex/__main__.py
intelhex/__version__.py
intelhex/bench.py
intelhex/compat.py
intelhex/getsizeof.py
intelhex/test.py
intelhex.egg-info/PKG-INFO
intelhex.egg-info/SOURCES.txt
intelhex.egg-info/dependency_links.txt
intelhex.egg-info/top_level.txt
scripts/bin2hex.py
scripts/hex2bin.py
scripts/hex2dump.py
scripts/hexdiff.py
scripts/hexinfo.py
scripts/hexmerge.py
1 change: 1 addition & 0 deletions intelhex.egg-info/dependency_links.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions intelhex.egg-info/top_level.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
intelhex
18 changes: 16 additions & 2 deletions intelhex/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,8 +480,22 @@ def __setitem__(self, addr, byte):
raise TypeError('Address should be >= 0.')
self._buf[addr] = byte
elif t == slice:
if not isinstance(byte, (list, tuple)):
raise ValueError('Slice operation expects sequence of bytes')
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
if not isinstance(byte, (bytes, bytearray)):
if isinstance(byte, (list, tuple)):
try:
check = all(0 <= x < 255 for x in byte)
except TypeError as exc:
raise slice_error from exc
if not check :
raise slice_error
if not all(isinstance(x, int) for x in byte) :
raise slice_error
else :
try:
byte = bytes(byte) # check for int + 0<=x<=255 + convert to something getitem works with
except TypeError as exc:
raise slice_error from exc
start = addr.start
stop = addr.stop
step = addr.step or 1
Expand Down
2 changes: 1 addition & 1 deletion intelhex/__version__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# IntelHex library version information
version_info = (2, 2, 1)
version_info = (2, 3)
version_str = '.'.join([str(i) for i in version_info])
2 changes: 1 addition & 1 deletion intelhex/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def asstr(s):
return s
return s.decode('latin1')

array_tobytes = getattr(array.array, "tobytes", array.array.tostring)
array_tobytes = getattr(array.array, "tobytes", array.array.tobytes)

IntTypes = (int,)
StrType = str
Expand Down
18 changes: 17 additions & 1 deletion intelhex/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,8 @@ def setitem(a,b):
# slice operations
ih[0:4] = range_l(4)
self.assertEqual({0:0, 1:1, 2:2, 3:3}, ih.todict())
ih[0:4] = range_g(1,5)
self.assertEqual({0:1, 1:2, 2:3, 3:4}, ih.todict())
ih[0:] = range_l(5,9)
self.assertEqual({0:5, 1:6, 2:7, 3:8}, ih.todict())
ih[:4] = range_l(9,13)
Expand All @@ -687,11 +689,25 @@ def setitem(a,b):
ih = IntelHex()
ih[0:8:2] = range_l(4)
self.assertEqual({0:0, 2:1, 4:2, 6:3}, ih.todict())
ih[0:8:2] = b'\xDE\xAD\xBE\xEF'
self.assertEqual({0:0xDE, 2:0xAD, 4:0xBE, 6:0xEF}, ih.todict())
# errors in slice operations
# ih[1:2] = 'a'
self.assertRaisesMsg(ValueError,
'Slice operation expects sequence of bytes',
'Slice operation expects either a bytes, a sequence of byte values (0 <= byte <= 255), or anything convertible to bytes',
setitem, slice(1,2,None), 'a')
self.assertRaisesMsg(ValueError,
'Slice operation expects either a bytes, a sequence of byte values (0 <= byte <= 255), or anything convertible to bytes',
setitem, slice(1,4,None), [1,1.5,2])
self.assertRaisesMsg(ValueError,
'Slice operation expects either a bytes, a sequence of byte values (0 <= byte <= 255), or anything convertible to bytes',
setitem, slice(1,4,None), [1,object(),2])
self.assertRaisesMsg(ValueError,
'Slice operation expects either a bytes, a sequence of byte values (0 <= byte <= 255), or anything convertible to bytes',
setitem, slice(1,2,None), (i/2 for i in range(1)))
self.assertRaisesMsg(ValueError,
'Slice operation expects either a bytes, a sequence of byte values (0 <= byte <= 255), or anything convertible to bytes',
setitem, slice(1,4,None), [1,2,256])
# ih[0:1] = [1,2,3]
self.assertRaisesMsg(ValueError,
'Length of bytes sequence does not match address range',
Expand Down
2 changes: 1 addition & 1 deletion scripts/bin2hex.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

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

VERSION = '2.2.1'
VERSION = '2.3'

if __name__ == '__main__':
import getopt
Expand Down
2 changes: 1 addition & 1 deletion scripts/hex2bin.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

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

VERSION = '2.2.1'
VERSION = '2.3'

if __name__ == '__main__':
import getopt
Expand Down
2 changes: 1 addition & 1 deletion scripts/hex2dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

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

VERSION = '2.2.1'
VERSION = '2.3'

USAGE = '''hex2dump: show content of hex file as hexdump.
Usage:
Expand Down
2 changes: 1 addition & 1 deletion scripts/hexdiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
of compared data.
"""

VERSION = '2.2.1'
VERSION = '2.3'

USAGE = '''hexdiff: diff dumps of 2 hex files.
Usage:
Expand Down
2 changes: 1 addition & 1 deletion scripts/hexinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
data (if any), in YAML format.
"""

VERSION = '2.2.1'
VERSION = '2.3'

USAGE = '''hexinfo: summarize a hex file's contents.
Usage:
Expand Down
2 changes: 1 addition & 1 deletion scripts/hexmerge.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

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

VERSION = '2.2.1'
VERSION = '2.3'

USAGE = '''hexmerge: merge content of hex files.
Usage:
Expand Down