[Python-bugs-list] [ python-Bugs-476616 ] UserDict.copy() error

noreply@sourceforge.net noreply@sourceforge.net
Tue, 30 Oct 2001 19:32:54 -0800


Bugs item #476616, was opened at 2001-10-30 18:57
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=476616&group_id=5470

Category: Python Library
Group: Python 2.2
Status: Open
Resolution: None
Priority: 5
Submitted By: Nobody/Anonymous (nobody)
Assigned to: Nobody/Anonymous (nobody)
Summary: UserDict.copy() error

Initial Comment:
The copy() method applied to subclasses of UserDict
copies the object, but does not copy the contained
dictionary. The dictionary is shared between the
original and the copy.

The copy method applied to UserDict (not a subclass)
works okay.

This error goes back to at least 2.0, and still exists
in 2.2b1.

This little test program demonstrates the error:

----------------------------------------------
from UserDict import UserDict

class MyDict (UserDict):
	pass

d = MyDict()
d[1] = 11
c = d.copy()
c[2] = 22	# this should affect c only, but also affects
d
print "c:", c
print "d:", d
---------------------------------------

BTW, subclassing 'dictionary' works fine in 2.2 (of
course). For running under 2.0, I worked around this by
using copy.deepcopy().

I'm in no hurry for a fix for this.

Bob Alexander
balexander@rsv.ricoh.com

----------------------------------------------------------------------

>Comment By: Guido van Rossum (gvanrossum)
Date: 2001-10-30 19:32

Message:
Logged In: YES 
user_id=6380

Hm, you're right. Sigh. This has been there since 1.5.2. I'm
not sure how to fix this -- using deepcopy() is too much
(and not what happens if self.__class__ is UserDict either).
Maybe this:
    c = copy.copy(self)
    c.data = self.data.copy()
    return c


----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=476616&group_id=5470