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.dbref
BRef.__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: