Re-raising exceptions with modified message

samwyse samwyse at gmail.com
Thu Jul 12 07:31:25 EDT 2007


On Jul 8, 8:50 am, Christoph Zwerschke <c... at online.de> wrote:
> Did you run this?
> With Py < 2.5 I get a syntax error, and with Py 2.5 I get:
>
>      new.__class__ = old.__class__
> TypeError: __class__ must be set to a class
>
> -- Chris

Damn, I'd have sworn I ran the final copy that I posted, but
apparently I did manage to have a typo creep in as I was prettifying
the code.  You need to lose the '()' in the definition of Empty.  (I'd
orignally had it subclass Exception but discovered that it wasn't
needed.)

    class Empty: pass

I can't figure out the other complaint, though, as old.__class_ should
be a class.  I guess I need to upgrade; I am using PythonWin 2.4.3
(#69, Apr 11 2006, 15:32:42) [MSC v.1310 32 bit (Intel)] on win32.
(Of course, at work they're still stuck on 2.4.2.)  Printing
type(old.__class__) gives me <type 'classobj'>; maybe using
    setattr(new, '__class__', old.__class__)
instead of the assignment would work, or maybe it's a bug/feature
introduced in 2.5.  (Trying this code:
    class Empty(old.__class__): pass
brings us back to the "TypeError: function takes exactly 5 arguments
(0 given)" message that we're trying to avoid.)


Anyway, running the corrected version under 2.4.X gives me this:

Traceback (most recent call last):
  File "C:\Python24\Lib\site-packages\pythonwin\pywin\framework
\scriptutils.py", line 310, in RunScript
    exec codeObject in __main__.__dict__
  File "C:\Documents and Settings\dentos\Desktop\scripting
\modify_message.py", line 19, in ?
    test(lambda: unicode('\xe4'))
  File "C:\Documents and Settings\dentos\Desktop\scripting
\modify_message.py", line 16, in test
    raise modify_message(e, lambda: str(e) + ", sorry!")
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position
0: ordinal not in range(128), sorry!
>>>




More information about the Python-list mailing list