Pluggable IntelHexError(Exception) message
Bug #113140 reported by
Enoch
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
IntelHex |
Fix Released
|
Wishlist
|
Alexander Belchenko |
Bug Description
Hello Alex,
May I suggest the following small change:
- _fmt = 'IntelHex base error' #: format string
+ _fmt = '%(message)s' # user pluggable message
I use it to report hex file constraint error as an ordinary IntelHexError exception case. For example,
ih = IntelHex(filepath)
bot = ih.minaddr()
top = ih.maxaddr() + 1
if (bot & 1) or (top & 1) or (top > APP_TOP) or top < 1024:
raise IntelHexError(
Thanks, Enoch.
P/S My suggestion to capture IOError too and report it as IntelhexError still stands :-)
Related branches
Changed in intelhex: | |
status: | Unconfirmed → Confirmed |
Changed in intelhex: | |
assignee: | nobody → bialix |
status: | Confirmed → Fix Committed |
Changed in intelhex: | |
status: | Fix Committed → Fix Released |
To post a comment you must log in.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Enoch пишет: message= 'FOREIGN HEX FILE!')
> Public bug reported:
>
> May I suggest the following small change:
>
> - _fmt = 'IntelHex base error' #: format string
> + _fmt = '%(message)s' # user pluggable message
>
> I use it to report hex file constraint error as an ordinary
> IntelHexError exception case. For example,
>
> ih = IntelHex(filepath)
> bot = ih.minaddr()
> top = ih.maxaddr() + 1
> if (bot & 1) or (top & 1) or (top > APP_TOP) or top < 1024:
> raise IntelHexError(
>
Actually your proposal is slightly incorrect. Ideally you should
subclass base exception class and provide your own exception, e.g.
class MyError( IntelHexError) :
_fmt = 'FOREIGN HEX FILE!'
And then use this exception in your code:
if (bot & 1) or (top & 1) or (top > APP_TOP) or top < 1024:
raise MyError
As workaround you right now can use base IntelHexError as you want
but to customize error message you need to pass new '_fmt' argument:
In [2]: raise intelhex. IntelHexError( _fmt='Custom error') ------- ------- ------- ------- ------- ------- ------- ------- ------- ----- IntelHexError' > Traceback (most recent call last)
- -------
<class 'intelhex.
C:\work\ MyCode\ intelhex\ trunk\< ipython console> in <module>()
<class 'intelhex. IntelHexError' >: Custom error
Of course it possible to handle 'message' argument specially.
Actually it should be quite easy to implement, but I have more
important bugs to fix now.
>
> P/S My suggestion to capture IOError too and report it as IntelhexError
> still stands :-)
It's incorrect. I think transforming IOError to custom IntelHexError is
bad style. User is able to catch IOError himself. Per example:
try:
ih = IntelHex('foo.hex')
except (IOError, IntelHexError):
print 'Unable to load hex file'
[µ]
affects /products/intelhex
importance wishlist
-----BEGIN PGP SIGNATURE----- enigmail. mozdev. org
338mxwCURArmxAJ 49T4cOeltETgzqo HTJ7y2Te7bo6QCd HTY0 lljsBhsg=
Version: GnuPG v1.4.6 (MingW32)
Comment: Using GnuPG with Mozilla - http://
iD8DBQFGP3uPzYr
urwSJzkc0HDc2u9
=Eacl
-----END PGP SIGNATURE-----