Code

Opened 7 years ago

Closed 6 years ago

#5183 closed (fixed)

Fix SortedDict deepcopy Error

Reported by: David Blewett <david@…> Owned by: nobody
Component: Core (Other) Version: master
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: UI/UX:

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@…> 7 years ago.
Patch to django/utils/datastructures.py that implements deepcopy

Download all attachments as: .zip

Change History (5)

Changed 7 years ago by David Blewett <david@…>

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

comment:1 Changed 7 years ago by David Blewett <david@…>

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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 Changed 7 years ago by SmileyChris

  • Triage Stage changed from Unreviewed to Design decision needed

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

comment:3 Changed 6 years ago 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.

comment:4 Changed 6 years ago by mtredinnick

  • Resolution set to fixed
  • Status changed from new to closed

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

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.