Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #21708
| References | <5B80DD153D7D744689F57F4FB69AF474026B9264@SCACMX008.exchad.jpmchase.net> <jjte9l$4iq$1@dough.gmane.org> <5B80DD153D7D744689F57F4FB69AF474026B9625@SCACMX008.exchad.jpmchase.net> |
|---|---|
| From | Ian Kelly <ian.g.kelly@gmail.com> |
| Date | 2012-03-15 13:49 -0600 |
| Subject | Re: Context Manager getting str instead of AttributeError instance |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.698.1331840975.3037.python-list@python.org> (permalink) |
On Thu, Mar 15, 2012 at 1:10 PM, Prasad, Ramit
<ramit.prasad@jpmorgan.com> wrote:
>> Prasad, Ramit wrote:
>>
>> > So I have a context manager used to catch errors
>> >
>> > def __exit__( self, exceptionClass, exception, tracebackObject ):
>> > if isinstance( exception, self.exceptionClasses ):
>> > #do something here
>> >
>> > Normally exception would be the exception instance, but for
>> > AttributeError it seems to be a string instead.
>>
>> I don't think so:
>>
>> >>> class A(object):
>> ... def __enter__(self): return self
>> ... def __exit__(self, *args): print args
>> ...
>> >>> with A() as a:
>> ... a.x
>> ...
>> (<type 'exceptions.AttributeError'>, AttributeError("'A' object has no
>> attribute 'x'",), <traceback object at 0x7f57b70f22d8>)
>> Traceback (most recent call last):
>> File "<stdin>", line 2, in <module>
>> AttributeError: 'A' object has no attribute 'x'
>>
>> > 1) Why is AttributeError different than the other built-ins
>> > in this respect?
>> > 2) Are there other standard errors like this (I know
>> > that SystemExit is different as well)?
>> > 3) Taking into account that I want to include subclasses of
>> > classes listed in self.exceptionClasses, Is there a better check I can
>> > use?
>
> Not sure why mine behaves differently:
> Python 2.6.6 (r266:84292, Dec 17 2010, 12:36:53) [MSC v.1500 32 bit (Intel)] on win32
>>>>
>>>> class A(object):
> ... def __enter__(self): return self
> ... def __exit__(self, *args): print args
> ...
>>>> with A() as a:
> ... a.x
> ...
> (<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.
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.
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Context Manager getting str instead of AttributeError instance Ian Kelly <ian.g.kelly@gmail.com> - 2012-03-15 13:49 -0600
csiph-web