Context Manager getting str instead of AttributeError instance

Ian Kelly ian.g.kelly at gmail.com
Thu Mar 15 21:33:46 CET 2012


On Thu, Mar 15, 2012 at 2:25 PM, Prasad, Ramit
<ramit.prasad at jpmorgan.com> wrote:
>> > ...
>> > (<type 'exceptions.AttributeError'>, "'A' object has no attribute 'x'",
>> <traceback object at 0x1817F648>)
>> > AttributeError: 'A' object has no attribute 'x'
>> >
>> > As you can see, I am getting a string while you are not.
>>
>>Ian Kelly said:
>> Looks like a version difference.  I don't have Python 2.6 handy to
>> test on, but I get a str in Python 2.5 and an AttributeError instance
>> in Python 2.7.
>
> Thanks Ian, that was the key! I guess I will just have to work around it.
> Any suggestions? I am thinking about just creating a blank instance of
> the error class since that the class gets passed successfully.

Well, for what you want to do, I don't think you need an instance at
all -- just use issubclass instead of isinstance:

def __exit__( self, exceptionClass, exception, tracebackObject ):
   if issubclass( exceptionClass, self.exceptionClasses ):
        #do something here

Cheers,
Ian



More information about the Python-list mailing list