Opened 17 years ago

Closed 17 years ago

#5183 closed (fixed)

Fix SortedDict deepcopy Error

Reported by: David Blewett <david@…> Owned by: nobody
Component: Core (Other) Version: dev
Severity: Keywords: SortedDict deepcopy
Cc: david@… Triage Stage: Design decision needed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I was using the SortedDict implementation from django/utils/datastructures.py for my own use in a project, and came across a problem when I need to do a deepcopy of a SortedDict object. I consistently got the following error:

Python 2.4:

Python 2.4.4 (#1, Aug 11 2007, 00:45:55)
[GCC 3.4.4 (Gentoo 3.4.4-r1, ssp-3.4.4-1.0, pie-8.7.8)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from django.utils.datastructures import SortedDict
>>> from copy import deepcopy
>>> sd = SortedDict({'test': 1})
>>> sd_c = deepcopy(sd)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/lib/python2.4/copy.py", line 204, in deepcopy
    y = _reconstruct(x, rv, 1, memo)
  File "/usr/lib/python2.4/copy.py", line 348, in _reconstruct
    y[key] = value
  File "django/utils/datastructures.py", line 62, in __setitem__
    if key not in self.keyOrder:
AttributeError: 'SortedDict' object has no attribute 'keyOrder'

and 2.5:

Python 2.5.1 (r251:54863, Aug 13 2007, 20:15:26)
[GCC 3.4.4 (Gentoo 3.4.4-r1, ssp-3.4.4-1.0, pie-8.7.8)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from django.utils.datastructures import SortedDict
>>> from copy import deepcopy
>>> sd = SortedDict({'test': 1})
>>> sd_c = deepcopy(sd)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.5/copy.py", line 189, in deepcopy
    y = _reconstruct(x, rv, 1, memo)
  File "/usr/lib/python2.5/copy.py", line 334, in _reconstruct
    y[key] = value
  File "django/utils/datastructures.py", line 62, in __setitem__
    if key not in self.keyOrder:
AttributeError: 'SortedDict' object has no attribute 'keyOrder'

The attached patch implements __deepcopy__ in a way that avoids these errors.

Attachments (1)

deepcopy.patch (554 bytes ) - added by David Blewett <david@…> 17 years ago.
Patch to django/utils/datastructures.py that implements deepcopy

Download all attachments as: .zip

Change History (5)

by David Blewett <david@…>, 17 years ago

Attachment: deepcopy.patch added

Patch to django/utils/datastructures.py that implements deepcopy

comment:1 by David Blewett <david@…>, 17 years ago

On a related note, SortedDict doesn't handle .pop(k[, x]) or .popitem(). Perhaps the Ordered Dictionary from http://www.voidspace.org.uk/python/odict.html could be of assistance there?

comment:2 by Chris Beaven, 17 years ago

Triage Stage: UnreviewedDesign decision needed

Design decision on whether to implement this patch or go with just a better class altogether.

comment:3 by Malcolm Tredinnick, 17 years ago

With regards to adding all these new methods to SortedDict, it's not really that important. SortedDict is for Django's internal use and it works well for that case. I'll add pop() and popitem(), but let's try to avoid needing to add every method under the sun. Django doesn't need them.

comment:4 by Malcolm Tredinnick, 17 years ago

Resolution: fixed
Status: newclosed

(In [6593]) Fixed #5183 -- Added deepcopy, pop() and popitem() to SortedDict. Based on
a patch from David Blewett.

Note: See TracTickets for help on using tickets.
Back to Top