Pluggable IntelHexError(Exception) message

Bug #113140 reported by Enoch
2
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(message='FOREIGN HEX FILE!')

Thanks, Enoch.

P/S My suggestion to capture IOError too and report it as IntelhexError still stands :-)

Related branches

Revision history for this message
Alexander Belchenko (bialix) wrote : Re: [Bug 113140] Pluggable IntelHexError(Exception) message

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Enoch пишет:
> 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(message='FOREIGN HEX FILE!')
>

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')
- ---------------------------------------------------------------------------
<class 'intelhex.IntelHexError'> Traceback (most recent call last)

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-----
Version: GnuPG v1.4.6 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGP3uPzYr338mxwCURArmxAJ49T4cOeltETgzqoHTJ7y2Te7bo6QCdHTY0
urwSJzkc0HDc2u9lljsBhsg=
=Eacl
-----END PGP SIGNATURE-----

Changed in intelhex:
importance: Undecided → Wishlist
Changed in intelhex:
status: Unconfirmed → Confirmed
Revision history for this message
Enoch (hwexler) wrote :

Alex,

Regarding the representation of general file errors through IntelHexError, I think that is a better "pythonic" style since we lower the complexity of library use and doc.
For example, otherwise the docs should explicitly warn the programmer to catch IOError, OSError, etc.

Thanks, Enoch.

Revision history for this message
Alexander Belchenko (bialix) wrote : Re: [Bug 113140] Re: Pluggable IntelHexError(Exception) message

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Enoch пишет:
> Regarding the representation of general file errors through IntelHexError, I think that is a better "pythonic" style since we lower the complexity of library use and doc.

I'm no sure that leaving IOError as is will increase complexity.
My idea that intelhex should raise errors related to its own
internals, leaving other errors for python interpreter itself.

When intelhex library will be used into another bigger application,
and probably with some another libraries that operate with files on disk,
I think at higher level it's better to catch IOError,
because it will be universal among several libraries,
instead of writing special exception handling code for every library.

> For example, otherwise the docs should explicitly warn the programmer to catch IOError, OSError, etc.

I'm OK to write detailed doc. Actually it's in my TODO list.

[µ]
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGQBJyzYr338mxwCURAsdQAJ9AQt4IasV5wCuacBBLzvAfTF45jACfdCYe
K3kYuLZSriGdRiQXUDvMVDU=
=GPYa
-----END PGP SIGNATURE-----

Revision history for this message
Alexander Belchenko (bialix) wrote :

Actually your request is very easy to implement.
This patch solve your request, but it lacks unit test.
Will be added to trunk with corresponding unit test.

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.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.