"""A test for AttributeError subclasses raised within __getattr__ This script raises a subclass of AttributeError in a __getattr__ method and checks the exception caught by the caller. """ import sys class DerivedAttributeError(AttributeError): pass raisedException = None class C: def __getattr__(self, name): global raisedException raisedException = DerivedAttributeError(name) raise DerivedAttributeError, raisedException obj = C() try: obj.xyz except: eType,eValue = sys.exc_info()[:2] if eValue == raisedException: print "Caught exception is raised exception" else: print "Caught exception is different from raised exception" print " Caught: class %s '%s'" % (eValue.__class__, eValue) print " Raised: class %s '%s'" % (raisedException.__class__, raisedException) else: print "OOps, no exception"