Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: 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; 'python': 0.08; '>>>>': 0.09; 'ah,': 0.09; 'bug:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'release.': 0.09; 'subclasses': 0.09; 'exception': 0.12; 'win32': 0.12; 'def': 0.13; "'a'": 0.16; '*args):': 0.16; 'a()': 0.16; 'a.x': 0.16; 'bugfix': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'self,': 0.16; 'subject:instance': 0.16; 'url:bugs': 0.17; 'wrote:': 0.18; '>>>': 0.18; 'seems': 0.20; '(most': 0.21; 'dec': 0.22; 'from:addr:web.de': 0.23; 'string': 0.24; 'traceback': 0.24; 'fix': 0.25; 'classes': 0.26; 'not.': 0.28; 'mine': 0.28; 'bit': 0.28; 'see,': 0.29; 'class': 0.29; 'print': 0.29; 'behaves': 0.30; 'use?': 0.30; 'instead.': 0.32; 'there': 0.33; 'header:User-Agent:1': 0.33; 'object': 0.33; 'test': 0.34; '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; '...': 0.35; 'something': 0.35; 'url:python': 0.35; 'received:org': 0.36; 'but': 0.37; 'skip:_ 10': 0.38; 'could': 0.38; 'think': 0.38; 'listed': 0.38; 'getting': 0.38; 'url:org': 0.39; '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 21:38:22 +0100 Organization: None References: <5B80DD153D7D744689F57F4FB69AF474026B9264@SCACMX008.exchad.jpmchase.net> <5B80DD153D7D744689F57F4FB69AF474026B9625@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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 61 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1331843886 news.xs4all.nl 6984 [2001:888:2000:d::a6]:58267 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:21712 Prasad, Ramit 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 >> ... >> (, AttributeError("'A' object has no >> attribute 'x'",), ) >> Traceback (most recent call last): >> File "", line 2, in >> 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 > ... > (, "'A' object has no attribute 'x'", > ) AttributeError: 'A' object has no > attribute 'x' > > As you can see, I am getting a string while you are not. Ah, it's a bug: http://bugs.python.org/issue7853 Unfortunately it is to severe to fix in a bugfix release. You could work around it with an issubclass() test on the first argument.