Re: Rewriting __getattr__

Discussion in 'Python' started by kost BebiX, Jan 7, 2011.

  1. kost BebiX

    kost BebiX Guest

    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:DBRef.__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:
    kost BebiX, Jan 7, 2011
    #1
    1. Advertising

  2. 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
    Steven D'Aprano, Jan 7, 2011
    #2
    1. Advertising

  3. kost BebiX

    kost BebiX Guest

    07.01.2011, 17:47, "Steven D'Aprano" <>:
    > 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.
    kost BebiX, Jan 7, 2011
    #3
  4. On Fri, 07 Jan 2011 23:54:24 +0200, kost BebiX wrote:

    > 07.01.2011, 17:47, "Steven D'Aprano"
    > <>:
    >> 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
    Steven D'Aprano, Jan 8, 2011
    #4
  5. kost BebiX

    kost BebiX Guest

    08.01.2011, 02:20, "Steven D'Aprano" <>:
    > On Fri, 07 Jan 2011 23:54:24 +0200, kost BebiX wrote:
    >
    >> š07.01.2011, 17:47, "Steven D'Aprano"
    >> š<>;:
    >>> š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)
    kost BebiX, Jan 8, 2011
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Greg Brunet

    __getattr__ weirdness

    Greg Brunet, Aug 22, 2003, in forum: Python
    Replies:
    3
    Views:
    329
    Greg Brunet
    Aug 22, 2003
  2. Anand
    Replies:
    0
    Views:
    396
    Anand
    Dec 18, 2003
  3. Samuel Kleiner

    Operator overloading and __getattr__

    Samuel Kleiner, Dec 22, 2003, in forum: Python
    Replies:
    6
    Views:
    1,895
    Shalabh Chaturvedi
    Jan 13, 2004
  4. Holger Joukl

    py2.1->py2.3.3 __getattr__ confusion

    Holger Joukl, Jul 2, 2004, in forum: Python
    Replies:
    1
    Views:
    299
    Peter Otten
    Jul 2, 2004
  5. kost BebiX

    Rewriting __getattr__

    kost BebiX, Jan 6, 2011, in forum: Python
    Replies:
    0
    Views:
    187
    kost BebiX
    Jan 6, 2011
Loading...

Share This Page