[Python-Dev] Exception masking/chaining

Phillip J. Eby pje@telecommunity.com
Thu, 12 Jun 2003 14:10:01 -0400


At 02:04 PM 6/12/03 -0400, Guido van Rossum wrote:
> > >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
 >>>