Ticket #13370: django-i18n-pickling.diff

File django-i18n-pickling.diff, 2.9 KB (added by Alex Gaynor, 14 years ago)
  • django/utils/functional.py

    diff --git a/django/utils/functional.py b/django/utils/functional.py
    index b66fdd3..fecf9f0 100644
    a b def lazy(func, *resultclasses):  
    147147    the lazy evaluation code is triggered. Results are not memoized; the
    148148    function is evaluated on every access.
    149149    """
    150     # When lazy() is called by the __reduce_ex__ machinery to reconstitute the
    151     # __proxy__ class it can't call with *args, so the first item will just be
    152     # a tuple.
    153     if len(resultclasses) == 1 and isinstance(resultclasses[0], tuple):
    154         resultclasses = resultclasses[0]
    155150
    156151    class __proxy__(Promise):
    157152        """
    def lazy(func, *resultclasses):  
    168163            if self.__dispatch is None:
    169164                self.__prepare_class__()
    170165
    171         def __reduce_ex__(self, protocol):
    172             return (lazy, (self.__func, resultclasses), self.__dict__)
     166        def __reduce__(self):
     167            return (
     168                lazy_proxy_unpickle,
     169                (self.__func, self.__args, self.__kw) + resultclasses
     170            )
    173171
    174172        def __prepare_class__(cls):
    175173            cls.__dispatch = {}
    def lazy(func, *resultclasses):  
    249247
    250248    return wraps(func)(__wrapper__)
    251249
     250def lazy_proxy_unpickle(func, args, kwargs, *resultclasses):
     251    return lazy(func, *resultclasses)(*args, **kwargs)
     252
    252253def allow_lazy(func, *resultclasses):
    253254    """
    254255    A decorator that allows a function to be called with one or more lazy
  • tests/regressiontests/i18n/tests.py

    diff --git a/tests/regressiontests/i18n/tests.py b/tests/regressiontests/i18n/tests.py
    index 17e53df..bb6d0a7 100644
    a b  
    11# -*- encoding: utf-8 -*-
     2import datetime
     3import decimal
    24import os
    35import sys
    4 import decimal
    5 import datetime
     6import pickle
    67
    78from django.template import Template, Context
    89from django.conf import settings
    9 from django.utils.formats import get_format, date_format, time_format, localize, localize_input
     10from django.utils.formats import (get_format, date_format, time_format, localize,
     11    localize_input)
    1012from django.utils.numberformat import format as nformat
    1113from django.test import TestCase
    12 from django.utils.translation import ugettext, ugettext_lazy, activate, deactivate, gettext_lazy, to_locale
     14from django.utils.translation import (ugettext, ugettext_lazy, activate,
     15    deactivate, gettext_lazy, to_locale)
    1316
    1417from forms import I18nForm, SelectDateForm, SelectDateWidget, CompanyForm
    1518
    class TranslationTests(TestCase):  
    4043        self.assertEqual(True, s == s2)
    4144        s4 = ugettext_lazy('Some other string')
    4245        self.assertEqual(False, s == s4)
     46   
     47    def test_lazy_pickle(self):
     48        s1 = ugettext_lazy("test")
     49        self.assertEqual(unicode(s1), "test")
     50        s2 = pickle.loads(pickle.dumps(s1))
     51        self.assertEqual(unicode(s2), "test")
    4352
    4453    def test_string_concat(self):
    4554        """
Back to Top