Opened 16 years ago

Closed 16 years ago

Last modified 12 years ago

#7496 closed (fixed)

Getting cached instance of SortedDict using db cache backend throws AttributeError

Reported by: John Huddleston <huddlej@…> Owned by: John Huddleston
Component: Core (Cache system) Version: dev
Severity: Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Using database caching, I set a non-empty SortedDict instance in the cache. When I try to retrieve it, I get an AttributeError with the message "'SortedDict' object has no attribute 'keyOrder'". Here is the code to recreate the problem:

# Using database cache backend
from django.core.cache import cache
from django.utils.datastructures import SortedDict
value = SortedDict()
value['1'] = 1
cache.set('my_dict', value)
cache.get('my_dict')

I looked into the database cache backend and traced the problem to the pickling of the SortedDict instance which can be recreated like this:

import base64
import pickle
from django.utils.datastructures import SortedDict
value = SortedDict()
value['1'] = 1
encoded = base64.encodestring(pickle.dumps(value, 2)).strip()
decoded = pickle.loads(base64.decodestring(encoded))

The call to pickle.loads results in the AttributeError. If the pickling protocol is changed in dumps to 0, the call to loads works. The problem appears to be that protocol 2 calls SortedDict.__new__ instead of creating an empty SortedDict. When __new__ is called, __init__ is not being called so keyOrder is not being initialized.

Possible Solutions:

If this is really unexpected behavior, the problem could be solved by changing the pickle protocol in the database caching backend from 2 to 0. It looks like the database cache backend is the only backend using pickle protocol 2 while the local memory backend is using protocol 0.

Another solution would be to subclass SortedDict.__new__ so keyOrder is always set:

def __new__(cls, *args, **kwargs):
    instance = super(SortedDict, cls).__new__(cls, *args, **kwargs)
    instance.keyOrder = []
    return instance 

I have attached patches reflecting both of these solutions (db.py.diff and datastructures.py.diff, respectively).

Attachments (2)

db.py.diff (690 bytes ) - added by John Huddleston <huddlej@…> 16 years ago.
db cache backend patch
datastructures.py.diff (580 bytes ) - added by John Huddleston <huddlej@…> 16 years ago.
SortedDict patch

Download all attachments as: .zip

Change History (6)

by John Huddleston <huddlej@…>, 16 years ago

Attachment: db.py.diff added

db cache backend patch

by John Huddleston <huddlej@…>, 16 years ago

Attachment: datastructures.py.diff added

SortedDict patch

comment:1 by John Huddleston, 16 years ago

Owner: changed from nobody to John Huddleston
Status: newassigned

comment:2 by Eric Holscher, 16 years ago

milestone: 1.0
Triage Stage: UnreviewedAccepted

comment:3 by Malcolm Tredinnick, 16 years ago

Resolution: fixed
Status: assignedclosed

(In [8531]) Fixed #7496 -- It's now possible to pickle SortedDicts with pickle protocol 2
(used in caching). Thanks, John Huddleston.

comment:4 by Jacob, 12 years ago

milestone: 1.0

Milestone 1.0 deleted

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