[Twisted-Python] copy.deepcopy domish.Element works in python 2.5 but not in 2.6

exarkun at twistedmatrix.com exarkun at twistedmatrix.com
Thu Dec 17 11:11:13 MST 2009


On 05:40 pm, gabriel.rossetti at arimaz.com wrote:
>Hello everyone,
>
>for some reason copy.deepcopy forks great on a domish.Element on 2.5 
>but
>not in 2.6 :
>
>Python 2.5.4 (r254:67916, Sep 20 2009, 10:05:43)
>[GCC 4.4.1] on linux2
>Type "help", "copyright", "credits" or "license" for more information.
> >>> from twisted.words.xish import domish
> >>> import copy
> >>> msg = domish.Element(('jabber:client', 'message'))
> >>> msg2 = copy.deepcopy(msg)
> >>>
>
>
>Python 2.6.4 (r264:75706, Nov  2 2009, 14:38:03)
>[GCC 4.4.1] on linux2
>Type "help", "copyright", "credits" or "license" for more information.
> >>> from twisted.words.xish import domish
> >>> import copy
> >>> msg = domish.Element(('jabber:client', 'message'))
> >>> msg2 = copy.deepcopy(msg)
>Exception RuntimeError: 'maximum recursion depth exceeded while calling
>a Python object' in <type 'exceptions.AttributeError'> ignored
> >>>
>
>does someone understand why? I'm using twisted 8.2 on ubuntu 9.10. 
>Thank
>you,
>Gabriel

Actually, it looks like it works about equally well on either version. 
While it looks like an exception was raised by deepcopy on Python 2.6, 
notice that the exception was actually *ignored*.  You get back a copy, 
and it even seems to have all the right attributes. ;)

The infinite recursion in deepcopy happens on Python 2.5 as well.  I'm 
not completely sure why it's not being reported, but it probably has to 
do with tiny differences in how deepcopy works.  hasattr is well known 
for swallowing exceptions silently.  The impressive thing is that the 
error is reported at all in Python 2.6.

A good rule of thumb is to assume that types don't support being copied 
like this unless their docs explicitly state that they do.  If you want 
this to be supported, it's probably not too hard to implement.  Feel 
free to file a ticket with a patch. :)

Jean-Paul




More information about the Twisted-Python mailing list