[Python-Dev] Exception masking/chaining

Guido van Rossum guido@python.org
Thu, 12 Jun 2003 14:22:21 -0400


> > > >The big problem is that it would require a major rewrite of the sys
> > > >module, wouldn't it?
> > >
> > > Under 2.2 this would be easy because you could just do 'sys.__class__ =
> > > MyNewSysClass'.  Can you still do that in 2.3 as long as 
> > 'MyNewSysClass' is
> > > a non-heap type with a compatible layout?
> >
> >I very much doubt that this worked in any version of Python 2.2 or
> >later.
> 
> Python 2.2.2 (#37, Oct 14 2002, 17:02:34) [MSC 32 bit (Intel)] on win32
> Type "copyright", "credits" or "license" for more information.
> IDLE 0.8 -- press F1 for help
>  >>> from types import ModuleType
>  >>> import sys
>  >>> class NewSys(ModuleType):
>          __slots__ = ()
>          def exc_type(self):
>                  return self.__dict__['exc_type']
>          exc_type = property(exc_type)
> 
>  >>> sys.__class__ = NewSys
>  >>> try:
>          raise TypeError
> except:
>          print sys.exc_type
> 
> 
> exceptions.TypeError
>  >>>
> 
>  >>> sys.exc_type = 1
> Traceback (most recent call last):
>    File "<pyshell#33>", line 1, in ?
>      sys.exc_type = 1
> AttributeError: can't set attribute
>  >>> sys.__class__ = ModuleType
>  >>> sys.exc_type = 1
>  >>>

OK, so it worked.  It doesn't work in 2.3.  But something else might
work.

--Guido van Rossum (home page: http://www.python.org/~guido/)