Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.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.015 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; '"""': 0.07; 'attribute': 0.07; "'a'": 0.09; 'comments?': 0.09; 'def': 0.12; '"b":': 0.16; '@property': 0.16; 'alphabet': 0.16; 'name):': 0.16; 'subject: \n ': 0.16; 'unhelpful': 0.16; 'subject:python': 0.16; 'demonstrate': 0.16; 'exception': 0.16; 'import': 0.22; 'this:': 0.26; 'pass': 0.26; 'gets': 0.27; 'raise': 0.29; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; "skip:' 10": 0.31; 'produces': 0.31; 'file': 0.32; 'class': 0.32; 'running': 0.33; '(most': 0.33; 'skip:_ 10': 0.34; 'problem': 0.35; 'subject:with': 0.35; 'received:209.85': 0.35; 'received:209.85.220': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'received:209': 0.37; 'skip:& 10': 0.38; 'to:addr :python-list': 0.38; 'rather': 0.38; 'recent': 0.39; 'skip:& 20': 0.39; 'to:addr:python.org': 0.39; 'enough': 0.39; 'easy': 0.60; 'identify': 0.61; 'name': 0.63; 'due': 0.66; '20,': 0.68; '26,': 0.68; 'subject:skip:A 10': 0.78 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:date:message-id:subject:from:to :content-type; bh=fHZODsOo313n9UWo7bwW4vEM1nNLRPzTOa8X2mlc358=; b=kYp5e0IOy2zVb+cGZL6S38lOHHOop0TEIqtCTPNvJS8+22KoUm43t5HbP1OM47sh9V 3XM2VLwZj+yIPJbHE9iW+PBKP7z7N24t0UBc3ws+pD9Bw6DFuHSNwhe/Th++JGEeoMz4 3MZ7XI88WqrIdxeTYnXTxXgDHFLmhV4VqL0V2wmSDhGu7ODIle9D7OAhRiTOTxQlsLVe tIYR7avwjW2RzrCypyZpst46Tio/wBu/D3B1GXGRWHZAvupubEq7Pu7LX7CUA/WPsrDM TgDsLLsdfd+pAzKCURrfgBx6dkec9EqMKWcu+9quJc/KCx/5EQ/Aw2WEwdrZBKLIqdvt sztQ== MIME-Version: 1.0 X-Received: by 10.66.144.136 with SMTP id sm8mr11969872pab.115.1367537680961; Thu, 02 May 2013 16:34:40 -0700 (PDT) Date: Fri, 3 May 2013 05:34:40 +0600 Subject: Debugging difficulty in python with __getattr__, decorated properties and AttributeError. From: "Mr. Joe" To: python-list@python.org Content-Type: multipart/alternative; boundary=047d7b6d826eb0d6dc04dbc4ad99 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 91 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1367537690 news.xs4all.nl 15916 [2001:888:2000:d::a6]:44965 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:44648 --047d7b6d826eb0d6dc04dbc4ad99 Content-Type: text/plain; charset=UTF-8 Is there any way to raise the original exception that made the call to __getattr__? I seem to stumble upon a problem where multi-layered attribute failure gets obscured due to use of __getattr__. Here's a dummy code to demonstrate my problems: """ import traceback class BackupAlphabet(object): pass class Alphabet(object): @property def a(self): return backupalphabet.a def __getattr__(self, name): if name == "b": return "banana" raise AttributeError( "'{0} object has no attribute '{1}'" .format(self.__class__.__name__, name)) alphabet = Alphabet() backupalphabet = BackupAlphabet() print(alphabet.a) print(alphabet.b) """ Running the above code produces this: """ Traceback (most recent call last): File "example.py", line 26, in print(alphabet.a) File "example.py", line 20, in __getattr__ .format(self.__class__.__name__, name)) AttributeError: 'Alphabet object has no attribute 'a' """ While it's easy enough to identify the problem here, the traceback is rather unhelpful in complex situations. Any comments? Regards, TB --047d7b6d826eb0d6dc04dbc4ad99 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Is there any way to raise the original exception that made= the call to __getattr__? I seem to stumble upon a problem where multi-laye= red attribute failure gets obscured due to use of __getattr__. Here's a= dummy code to demonstrate my problems:
"""
import traceback

<= br>
class BackupAlphabet(object):
=C2=A0 =C2=A0 pass


class Alphabet(object):
=C2= =A0 =C2=A0 @property
=C2=A0 =C2=A0 def a(self):
=C2=A0 =C2=A0 =C2=A0 =C2=A0 retur= n backupalphabet.a


=C2=A0 =C2=A0 de= f __getattr__(self, name):
=C2=A0 =C2=A0 =C2=A0 =C2=A0 if name = =3D=3D "b":
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 r= eturn "banana"

=C2=A0 =C2=A0 =C2=A0 =C2=A0 raise AttributeError(
=
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 "'{0} object has no= attribute '{1}'"
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 .format(self.__class__.__name__, name))

alphabet =3D Alphabet()
backupalphabet =3D BackupAlpha= bet()

print(alphabet.a)
print(alphabet.b= )
"""

R= unning the above code produces this:
"""
Traceback (most recent call last= ):
=C2=A0 File "example.py", line 26, in <module>=
=C2=A0 =C2=A0 print(alphabet.a)
=C2=A0 File "exam= ple.py", line 20, in __getattr__
=C2=A0 =C2=A0 .format(self.__class__.__name__, name))
Attrib= uteError: 'Alphabet object has no attribute 'a'
"""

While it's easy enou= gh to identify the problem here, the traceback is rather unhelpful in compl= ex situations. Any comments?

Regards,
TB
--047d7b6d826eb0d6dc04dbc4ad99--