Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #21711

Re: Context Manager getting str instead of AttributeError instance

References <5B80DD153D7D744689F57F4FB69AF474026B9264@SCACMX008.exchad.jpmchase.net> <jjte9l$4iq$1@dough.gmane.org> <5B80DD153D7D744689F57F4FB69AF474026B9625@SCACMX008.exchad.jpmchase.net> <CALwzid=O78BW2yqh6_4pSAVVXuSxqM5GVzXdhyPnLY7KUWvbaQ@mail.gmail.com> <5B80DD153D7D744689F57F4FB69AF474026B9706@SCACMX008.exchad.jpmchase.net>
From Ian Kelly <ian.g.kelly@gmail.com>
Date 2012-03-15 14:33 -0600
Subject Re: Context Manager getting str instead of AttributeError instance
Newsgroups comp.lang.python
Message-ID <mailman.701.1331843658.3037.python-list@python.org> (permalink)

Show all headers | View raw


On Thu, Mar 15, 2012 at 2:25 PM, Prasad, Ramit
<ramit.prasad@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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Context Manager getting str instead of AttributeError instance Ian Kelly <ian.g.kelly@gmail.com> - 2012-03-15 14:33 -0600

csiph-web