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


Groups > comp.lang.python > #21704 > unrolled thread

Re: Context Manager getting str instead of AttributeError instance

Started byPeter Otten <__peter__@web.de>
First post2012-03-15 20:01 +0100
Last post2012-03-15 20:01 +0100
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Context Manager getting str instead of AttributeError instance Peter Otten <__peter__@web.de> - 2012-03-15 20:01 +0100

#21704 — Re: Context Manager getting str instead of AttributeError instance

FromPeter Otten <__peter__@web.de>
Date2012-03-15 20:01 +0100
SubjectRe: Context Manager getting str instead of AttributeError instance
Message-ID<mailman.695.1331838082.3037.python-list@python.org>
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?

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web