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


Groups > comp.lang.python > #21704

Re: Context Manager getting str instead of AttributeError instance

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'context': 0.04; 'args': 0.05; 'instance,': 0.05; 'attribute': 0.07; 'exception,': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subclasses': 0.09; 'exception': 0.12; 'def': 0.13; "'a'": 0.16; '*args):': 0.16; 'a()': 0.16; 'a.x': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'self,': 0.16; 'subject:instance': 0.16; 'wrote:': 0.18; '>>>': 0.18; 'seems': 0.20; '(most': 0.21; 'from:addr:web.de': 0.23; 'string': 0.24; 'traceback': 0.24; 'classes': 0.26; 'class': 0.29; 'print': 0.29; 'use?': 0.30; 'instead.': 0.32; 'there': 0.33; 'header:User-Agent:1': 0.33; 'object': 0.33; 'file': 0.34; 'header:X-Complaints-To:1': 0.34; 'normally': 0.34; 'last):': 0.34; 'subject:getting': 0.34; 'to:addr:python-list': 0.35; 'something': 0.35; 'received:org': 0.36; 'but': 0.37; 'skip:_ 10': 0.38; 'think': 0.38; 'listed': 0.38; 'to:addr:python.org': 0.40; 'here': 0.64; 'account': 0.66; 'taking': 0.66; 'so:': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Context Manager getting str instead of AttributeError instance
Date Thu, 15 Mar 2012 20:01:38 +0100
Organization None
References <5B80DD153D7D744689F57F4FB69AF474026B9264@SCACMX008.exchad.jpmchase.net>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p5084947c.dip.t-dialin.net
User-Agent KNode/4.7.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.695.1331838082.3037.python-list@python.org> (permalink)
Lines 35
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1331838082 news.xs4all.nl 6893 [2001:888:2000:d::a6]:48810
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:21704

Show key headers only | View raw


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?

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


Thread

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

csiph-web