Django

Code

Ticket #5183 (closed: fixed)

Opened 1 year ago

Last modified 11 months ago

Fix SortedDict deepcopy Error

Reported by: David Blewett <david@dawninglight.net> Assigned to: nobody
Milestone: Component: Core framework
Version: SVN Keywords: SortedDict deepcopy
Cc: david@dawninglight.net Triage Stage: Design decision needed
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

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

deepcopy.patch (0.5 kB) - added by David Blewett <david@dawninglight.net> on 08/16/07 09:54:57.
Patch to django/utils/datastructures.py that implements deepcopy

Change History

08/16/07 09:54:57 changed by David Blewett <david@dawninglight.net>

  • attachment deepcopy.patch added.

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

08/16/07 10:00:13 changed by David Blewett <david@dawninglight.net>

  • needs_better_patch changed.
  • needs_tests changed.
  • needs_docs changed.

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?

09/03/07 16:54:53 changed by SmileyChris

  • stage changed from Unreviewed to Design decision needed.

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

10/21/07 19:49:01 changed by mtredinnick

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.

10/21/07 19:52:54 changed by mtredinnick

  • status changed from new to closed.
  • resolution set to fixed.

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


Add/Change #5183 (Fix SortedDict deepcopy Error)




Change Properties
Action