From 81d5a0d4cdb6a2a7ffb093ddc7a67da8f0400655 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Flaventin=20Hauchecorne?= Date: Tue, 7 Jul 2020 03:33:25 +0200 Subject: [PATCH 1/2] Added support for bytes in sliced __setitem__ --- AUTHORS.rst | 1 + NEWS.rst | 5 +++++ Readme.rst | 2 +- intelhex/__init__.py | 18 ++++++++++++++++-- intelhex/__version__.py | 2 +- intelhex/test.py | 18 +++++++++++++++++- scripts/bin2hex.py | 2 +- scripts/hex2bin.py | 2 +- scripts/hex2dump.py | 2 +- scripts/hexdiff.py | 2 +- scripts/hexinfo.py | 2 +- scripts/hexmerge.py | 2 +- 12 files changed, 47 insertions(+), 11 deletions(-) diff --git a/AUTHORS.rst b/AUTHORS.rst index 297e869..bdafa87 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -22,3 +22,4 @@ Contributors: * "durexyl" @ GitHub * "erki1993" @ GitHub * "mentaal" @ GitHub +* Léo Flaventin Hauchecorne diff --git a/NEWS.rst b/NEWS.rst index 80734ed..e2c4b9b 100644 --- a/NEWS.rst +++ b/NEWS.rst @@ -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. diff --git a/Readme.rst b/Readme.rst index e3808a2..f9619e1 100644 --- a/Readme.rst +++ b/Readme.rst @@ -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 diff --git a/intelhex/__init__.py b/intelhex/__init__.py index 45188b0..be6646f 100644 --- a/intelhex/__init__.py +++ b/intelhex/__init__.py @@ -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 diff --git a/intelhex/__version__.py b/intelhex/__version__.py index 5ce4f0b..d826775 100644 --- a/intelhex/__version__.py +++ b/intelhex/__version__.py @@ -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]) diff --git a/intelhex/test.py b/intelhex/test.py index 84e771b..bde7b76 100755 --- a/intelhex/test.py +++ b/intelhex/test.py @@ -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) @@ -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', diff --git a/scripts/bin2hex.py b/scripts/bin2hex.py index 8a726f9..afc6ee4 100755 --- a/scripts/bin2hex.py +++ b/scripts/bin2hex.py @@ -35,7 +35,7 @@ '''Intel HEX file format bin2hex convertor utility.''' -VERSION = '2.2.1' +VERSION = '2.3' if __name__ == '__main__': import getopt diff --git a/scripts/hex2bin.py b/scripts/hex2bin.py index 3f56ec1..d534719 100755 --- a/scripts/hex2bin.py +++ b/scripts/hex2bin.py @@ -35,7 +35,7 @@ '''Intel HEX file format hex2bin convertor utility.''' -VERSION = '2.2.1' +VERSION = '2.3' if __name__ == '__main__': import getopt diff --git a/scripts/hex2dump.py b/scripts/hex2dump.py index 129b10c..0e83cc3 100755 --- a/scripts/hex2dump.py +++ b/scripts/hex2dump.py @@ -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: diff --git a/scripts/hexdiff.py b/scripts/hexdiff.py index f9bb9db..52656ad 100755 --- a/scripts/hexdiff.py +++ b/scripts/hexdiff.py @@ -37,7 +37,7 @@ of compared data. """ -VERSION = '2.2.1' +VERSION = '2.3' USAGE = '''hexdiff: diff dumps of 2 hex files. Usage: diff --git a/scripts/hexinfo.py b/scripts/hexinfo.py index 03d4420..024c5bf 100755 --- a/scripts/hexinfo.py +++ b/scripts/hexinfo.py @@ -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: diff --git a/scripts/hexmerge.py b/scripts/hexmerge.py index 9359662..4ce8d84 100755 --- a/scripts/hexmerge.py +++ b/scripts/hexmerge.py @@ -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: From 94068de06d1d2060d3b7506232510eb4fa5a1fb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Flaventin=20Hauchecorne?= Date: Thu, 11 Mar 2021 11:15:02 +0100 Subject: [PATCH 2/2] Fixed array conversion --- .coverage | Bin 0 -> 53248 bytes intelhex.egg-info/PKG-INFO | 75 +++++++++++++++++++++++++ intelhex.egg-info/SOURCES.txt | 24 ++++++++ intelhex.egg-info/dependency_links.txt | 1 + intelhex.egg-info/top_level.txt | 1 + intelhex/compat.py | 2 +- 6 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 .coverage create mode 100644 intelhex.egg-info/PKG-INFO create mode 100644 intelhex.egg-info/SOURCES.txt create mode 100644 intelhex.egg-info/dependency_links.txt create mode 100644 intelhex.egg-info/top_level.txt diff --git a/.coverage b/.coverage new file mode 100644 index 0000000000000000000000000000000000000000..1ccf5a3587812e399ae7d62bcf3cfc21bb6a85be GIT binary patch literal 53248 zcmeI4UvLx09mnr4;Cd1^Y-rhVlpOv-wCloWId8NxGEzM>;%A+}t6&$wfvsgk;Q!P`QAQ6S4o>Pm{ z$`7^aA-jNkHB&oiIvFHH(>fN^>6RjWef-Os zZIfC|(W|G;8p>#yvVEKcrHo>lX`d{r`BX+L4Jqn^g(-vdJZL)B7L8ooG?c8Kx6R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8a*-Zs6ZbZhTM>5? zdRu^m^mQsEk;tifK9Qi^?5%zh6zWt^lAgSiAt_IaBxPl%QW6PwwQOtjgg~<;qwv6rnOJRwV-wN*xUkLwFJ|IUa zi2wp100JNY0w4eaAOHd&00JOze-hX#R(tqPSEcUyUwT5U_H`^|(fU6yCRRfo3-Z?g z{vBdI5ZRRe%fBf2;7i&i@&AoHd zny*)^vkhymV$I*0wSL6ilCCyypKI)`Z>}9Wf9+DE*}Qh*!{hU>y75X%~M7*{Kij1 z0-5|J;qrsFef$a^B_HsyMAy%6ox6BUtgh$%!Qf1WW&I_>rN8^q@8%!8eChqS=YDqd z>aD9sPXD3O^M&vEx95+R^`13rHLY{j?0a** z)AGzr-Ktw_XLDBN)zvp@N18jYfByEBWF>Yv&J9=}RjfHb_tC#=jh|Q%uIHu6-*d*= z_)fKZ*O?ivx9ha{0O=uKw#NB)xKqy;s*yd{_guFc)0F${QR#$w{LdRNY>#j%j~&?a|NjdhSlpig literal 0 HcmV?d00001 diff --git a/intelhex.egg-info/PKG-INFO b/intelhex.egg-info/PKG-INFO new file mode 100644 index 0000000..22ec024 --- /dev/null +++ b/intelhex.egg-info/PKG-INFO @@ -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: alexander.belchenko@gmail.com +Maintainer: Bert van Hall +Maintainer-email: bert.vanhall@gmx.de +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 `_. + + 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 `_ + +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 diff --git a/intelhex.egg-info/SOURCES.txt b/intelhex.egg-info/SOURCES.txt new file mode 100644 index 0000000..97d80f5 --- /dev/null +++ b/intelhex.egg-info/SOURCES.txt @@ -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 \ No newline at end of file diff --git a/intelhex.egg-info/dependency_links.txt b/intelhex.egg-info/dependency_links.txt new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/intelhex.egg-info/dependency_links.txt @@ -0,0 +1 @@ + diff --git a/intelhex.egg-info/top_level.txt b/intelhex.egg-info/top_level.txt new file mode 100644 index 0000000..a7dde12 --- /dev/null +++ b/intelhex.egg-info/top_level.txt @@ -0,0 +1 @@ +intelhex diff --git a/intelhex/compat.py b/intelhex/compat.py index 194cd5d..6f95245 100644 --- a/intelhex/compat.py +++ b/intelhex/compat.py @@ -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