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


Groups > comp.lang.python > #21708

Re: Context Manager getting str instead of AttributeError instance

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!tudelft.nl!txtfeed1.tudelft.nl!multikabel.net!newsfeed20.multikabel.net!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
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; '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; 'handy': 0.16; 'self,': 0.16; 'subject:instance': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.18; '>>>': 0.18; 'instance': 0.18; 'seems': 0.20; '(most': 0.21; 'to:2**1': 0.21; 'header:In-Reply-To:1': 0.22; 'dec': 0.22; 'string': 0.24; 'traceback': 0.24; 'cc:2**0': 0.26; 'classes': 0.26; 'looks': 0.27; 'not.': 0.28; 'mine': 0.28; 'bit': 0.28; 'message-id:@mail.gmail.com': 0.29; 'see,': 0.29; 'class': 0.29; 'print': 0.29; 'cc:addr:python.org': 0.29; 'pm,': 0.29; 'behaves': 0.30; 'str': 0.30; 'use?': 0.30; 'on,': 0.31; 'version': 0.32; 'instead.': 0.32; 'thu,': 0.32; 'there': 0.33; 'object': 0.33; 'test': 0.34; 'file': 0.34; 'normally': 0.34; 'last):': 0.34; 'subject:getting': 0.34; '...': 0.35; 'something': 0.35; 'but': 0.37; 'received:google.com': 0.37; 'skip:_ 10': 0.38; 'received:209.85': 0.38; 'think': 0.38; 'listed': 0.38; 'getting': 0.38; 'received:209': 0.39; 'here': 0.64; 'account': 0.66; 'taking': 0.66; '2.7.': 0.84; 'difference.': 0.84; 'so:': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; bh=ysgjXxJFVYg0IoRLqxwTOvtH8N+6k9uO0f8HWSKkcvY=; b=ClVUaSa8YOHmJ/O7LNUx2jc+yzE/VBMphVXF8IF22k4S+FQUVF9aX4lT3U2tZcwnU+ kBYyAyA01UAGjjS8Iy3/AfwUf6jM2aZQXGDeLayqx1f0s66mlStSgMtnBflM2TkeyVIZ JQQhZb0MHQpOshhh4Jx60OpwQ11Z6yGwHhkUqulRetSFTeVo6cCY3CVjXg0TMqDnMpYe Tybyrf7+sTjWmUuKCqVjoqS3htaEF4+6VJplbWDcP6vZn6bvYf7mVCNenMhsoA0abCwe 4CEKqbtHG5em0cjdPvCc6l1nHOo5/5+iWS5uvN6IGcu8f79qR64WbrX7U78A18BPm3G7 7ZRQ==
MIME-Version 1.0
In-Reply-To <5B80DD153D7D744689F57F4FB69AF474026B9625@SCACMX008.exchad.jpmchase.net>
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 Thu, 15 Mar 2012 13:49:02 -0600
Subject Re: Context Manager getting str instead of AttributeError instance
To "Prasad, Ramit" <ramit.prasad@jpmorgan.com>
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
Cc "python-list@python.org" <python-list@python.org>
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.698.1331840975.3037.python-list@python.org> (permalink)
Lines 56
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1331840975 news.xs4all.nl 6962 [2001:888:2000:d::a6]:36292
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:21708

Show key headers only | View raw


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


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