Opened 8 years ago

Closed 7 years ago

#5808 closed (duplicate)

Cannot pickle the default filters

Reported by: simon@… Owned by: nobody
Component: Core (Other) Version: master
Severity: Keywords: template pickle filter
Cc: Triage Stage: Design decision needed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

Reproduction code:

from django import template
t = template.Template("{{ firstname|capfirst }}")
c = template.Context("firstname": "simon")
p = pickle.dumps(t)
print pickle.loads(p).render(c)

Expected result:

Simon

Actual result:

<class 'pickle.PicklingError'>: Can't pickle <function _dec at 0x83c010c>: it's not found as django.template.defaultfilters._dec

Attachments (1)

django-pickle-filters.patch (469 bytes) - added by simon@… 8 years ago.
Patch

Download all attachments as: .zip

Change History (5)

Changed 8 years ago by simon@…

Patch

comment:1 Changed 8 years ago by mtredinnick

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

The __name__ attribute is write-only in Python 2.3.

This might be fixed by Jeremy Dunck's recent decorator work (which isn't going in just yet because we need to get some license clearance).

We're probably unlikely to jump through a lot of hoops to make pickling work, though, if more than this is required.

comment:2 Changed 8 years ago by mtredinnick

  • Triage Stage changed from Unreviewed to Design decision needed

comment:3 Changed 8 years ago by Simon Law <simon@…>

I see. This is related to #3558.

Since name is read-only in Python 2.3, it still possible for this to work by wrapping the patch in a try statement.

try:
    _dec.__name__ = func.__name__ 
except TypeError:
    pass

This breaks pickling in Python 2.3, but permits it elsewhere.

Pickling is useful so that we can cache templates in memcached. If template parsing is non-trivial, you really want to cache these objects.

comment:4 Changed 7 years ago by jacob

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

As Malcolm said, this is a duplicate of #5701.

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