Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Rewriting __getattr__

Reply
Thread Tools

Re: Rewriting __getattr__

 
 
kost BebiX
Guest
Posts: n/a
 
      01-07-2011
07.01.2011, 16:22, "Jean-Michel Pichavant" <>:
> kost BebiX wrote:
>
>> šYou're absolutely right! Now try to do except Keyerror: raise AttributeError and it will also fail. But why?
>>
>> š07.01.2011, 15:45, "Jean-Michel Pichavant" <>;:
>>> škost BebiX wrote:
>>>> ššHi everyone!
>>>> ššI just saw a bug (?) in bson.dbrefBRef.__getattr__
>>>>
>>>> ššHere's they're code:
>>>> ššššššdef __getattr__(self, key):
>>>> ššššššššššreturn self.__kwargs[key]
>>>>
>>>> ššAnd when you do copy.deepcopy on that object it will raise you KeyError. So here's a small piece of code that reproduces the problem:
>>> šfrom http://docs.python.org/reference/datamodel.html
>>>
>>> šAbout __getattr__
>>> š"This method should return the (computed) attribute value or raise an
>>> šAttributeError
>>> š<http://docs.python.org/library/exceptions.html#exceptions.AttributeError>
>>> šexception."
>>>
>>> šThe code you provided raises a KeyError thus methods such as 'getattr'
>>> šwill fail as they expect an AttributeError exception.
>>>
>>> šJM
>>>
>>> š--
>>> šhttp://mail.python.org/mailman/listinfo/python-list

>
> please don't top post
>
> It fails because you simply did not returned any value (with your class
> A example).
>
> class A(object):
> ššššdef __init__(self):
> ššššššššself.d = {}
> ššššdef __getattr__(self, key):
> šššššššštry:
> šššššššššššš*return* self.d[key]
> ššššššššexcept KeyError:
> ššššššššššššraise AttributeError
>
> works fine with deepcopy
>
> JM
>
> --
> http://mail.python.org/mailman/listinfo/python-list


Sorry for top posting, didn't know about that) I'm quote new to posting to mailing lists.

Well, actually the code you showed doesn't work)

>>> class A(object):

... def __init__(self):
... self.d = {}
... def __getattr__(self, key):
... try:
... return self.d[key]
... except KeyError:
... raise AttributeError
>>> from copy import deepcopy
>>> a = A()
>>> deepcopy(a)

Exception RuntimeError: 'maximum recursion depth exceeded while calling a Python object' in <type 'exceptions.AttributeError'> ignored
Exception RuntimeError: 'maximum recursion depth exceeded while calling a Python object' in <type 'exceptions.AttributeError'> ignored
0: <__main__.A object at 0xda0250>


--
jabber:
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      01-07-2011
On Fri, 07 Jan 2011 16:47:55 +0200, kost BebiX wrote:

> Well, actually the code you showed doesn't work)


Actually, it does. It just prints a warning message as well. Look
carefully:


>>>> class A(object):

> .. def __init__(self):
> .. self.d = {}
> .. def __getattr__(self, key):
> .. try:
> .. return self.d[key]
> .. except KeyError:
> .. raise AttributeError
>>>> from copy import deepcopy
>>>> a = A()
>>>> deepcopy(a)

> Exception RuntimeError: 'maximum recursion depth exceeded while calling
> a Python object' in <type 'exceptions.AttributeError'> ignored
> Exception RuntimeError: 'maximum recursion depth exceeded while calling
> a Python object' in <type 'exceptions.AttributeError'> ignored
> 0: <__main__.A object at 0xda0250>



The last thing printed is the deepcopied object.


I've tested the above code in Python versions 2.4 through 3.2 and the
only one that prints that message is 2.6.


--
Steven
 
Reply With Quote
 
 
 
 
kost BebiX
Guest
Posts: n/a
 
      01-07-2011
07.01.2011, 17:47, "Steven D'Aprano" <steve+>:
> On Fri, 07 Jan 2011 16:47:55 +0200, kost BebiX wrote:
>
>> šWell, actually the code you showed doesn't work)

>
> Actually, it does. It just prints a warning message as well. Look
> carefully:
>
>>>>> šclass A(object):

>> š.. ššššdef __init__(self):
>> š.. ššššššššself.d = {}
>> š.. ššššdef __getattr__(self, key):
>> š.. šššššššštry:
>> š.. ššššššššššššreturn self.d[key]
>> š.. ššššššššexcept KeyError:
>> š.. ššššššššššššraise AttributeError
>>>>> šfrom copy import deepcopy
>>>>> ša = A()
>>>>> šdeepcopy(a)

>> šException RuntimeError: 'maximum recursion depth exceeded while calling
>> ša Python object' in <type 'exceptions.AttributeError'> ignored
>> šException RuntimeError: 'maximum recursion depth exceeded while calling
>> ša Python object' in <type 'exceptions.AttributeError'> ignored
>> š0: <__main__.A object at 0xda0250>

>
> The last thing printed is the deepcopied object.
>
> I've tested the above code in Python versions 2.4 through 3.2 and the
> only one that prints that message is 2.6.
>
> --
> Steven
> --
> http://mail.python.org/mailman/listinfo/python-list


So maybe it should be fixed in 2.6? When I'll have time I'll look at copy.py in different python's. Maybe there's the answer)
Thanks anyway.
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      01-08-2011
On Fri, 07 Jan 2011 23:54:24 +0200, kost BebiX wrote:

> 07.01.2011, 17:47, "Steven D'Aprano"
> <steve+>:
>> On Fri, 07 Jan 2011 16:47:55 +0200, kost BebiX wrote:
>>
>>> Â*Well, actually the code you showed doesn't work)

>>
>> Actually, it does. It just prints a warning message as well. Look
>> carefully:

[...]
>> I've tested the above code in Python versions 2.4 through 3.2 and the
>> only one that prints that message is 2.6.


> So maybe it should be fixed in 2.6? When I'll have time I'll look at
> copy.py in different python's. Maybe there's the answer) Thanks anyway.



Before you spend too much (i.e. any) time trying to fix this, I think
that Python 2.6 is now only accepting security fixes.

http://www.python.org/download/releases/2.6.6/


--
Steven
 
Reply With Quote
 
kost BebiX
Guest
Posts: n/a
 
      01-08-2011
08.01.2011, 02:20, "Steven D'Aprano" <steve+>:
> On Fri, 07 Jan 2011 23:54:24 +0200, kost BebiX wrote:
>
>> š07.01.2011, 17:47, "Steven D'Aprano"
>> š<steve+>;:
>>> šOn Fri, 07 Jan 2011 16:47:55 +0200, kost BebiX wrote:
>>>> ššWell, actually the code you showed doesn't work)
>>> šActually, it does. It just prints a warning message as well. Look
>>> šcarefully:

>
> [...]
>
>>> šI've tested the above code in Python versions 2.4 through 3.2 and the
>>> šonly one that prints that message is 2.6.

>> šSo maybe it should be fixed in 2.6? When I'll have time I'll look at
>> šcopy.py in different python's. Maybe there's the answer) Thanks anyway.

>
> Before you spend too much (i.e. any) time trying to fix this, I think
> that Python 2.6 is now only accepting security fixes.
>
> http://www.python.org/download/releases/2.6.6/
>
> --
> Steven


Too bad (I mean, it's not too bad, because this thing can break other's 2.6 code that somehow depends on that). Now when I'll install 2.7 I'll have to remember about this bug when coding)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Rewriting __getattr__ kost BebiX Python 0 01-06-2011 10:07 PM
py2.1->py2.3.3 __getattr__ confusion Holger Joukl Python 1 07-02-2004 02:00 PM
Operator overloading and __getattr__ Samuel Kleiner Python 6 01-13-2004 06:39 AM
__setattr__ and __getattr__ with derived classes Anand Python 0 12-18-2003 09:36 PM
__getattr__ weirdness Greg Brunet Python 3 08-22-2003 08:29 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57