Re-raising exceptions with modified message

samwyse samwyse at gmail.com
Thu Jul 12 12:48:45 EDT 2007


On Jul 12, 6:31 am, samwyse <samw... at gmail.com> wrote:
> On Jul 8, 8:50 am, Christoph Zwerschke <c... at online.de> wrote:
>
> > With Py 2.5 I get:
>
> >      new.__class__ = old.__class__
> > TypeError: __class__ must be set to a class


Hmmm, under Python 2.4.X, printing repr(old.__class__) gives me this:
  <class exceptions.UnicodeDecodeError at 0x00A24F00>
while under 2.5.X, I get this:
  <type 'exceptions.UnicodeDecodeError'>


So, let's try sub-classing the type:

def modify_message(old, f):
    class Empty: pass
    new = Empty()
    print "old.__class__ =", repr(old.__class__)
    print "Empty =", repr(Empty)
    new.__class__ = Empty

    class Excpt(old.__class__): pass
    print "Excpt =", repr(Excpt)
    print "Excpt.__class__ =", repr(Excpt.__class__)
    new.__class__ = Excpt

    new.__dict__ = old.__dict__.copy()
    new.__str__ = f
    return new

Nope, that gives us the same message:

old.__class__ = <type 'exceptions.UnicodeDecodeError'>
Empty = <class __main__.Empty at 0x00AB0AB0>
Excpt = <class '__main__.Excpt'>
Excpt.__class__ = <type 'type'>
Traceback (most recent call last):
[...]
TypeError: __class__ must be set to a class

Excpt ceratinly appears to be a class.  Does anyone smarter than me
know what's going on here?




More information about the Python-list mailing list