Kaydet (Commit) b59f963a authored tarafından Alexey Kotlyarov's avatar Alexey Kotlyarov Kaydeden (comit) Tim Graham

Fixed #26212 -- Made forms.FileField and translation.lazy_number() picklable.

üst 1ac7fdcd
...@@ -106,6 +106,8 @@ def lazy_number(func, resultclass, number=None, **kwargs): ...@@ -106,6 +106,8 @@ def lazy_number(func, resultclass, number=None, **kwargs):
kwargs['number'] = number kwargs['number'] = number
proxy = lazy(func, resultclass)(**kwargs) proxy = lazy(func, resultclass)(**kwargs)
else: else:
original_kwargs = kwargs.copy()
class NumberAwareString(resultclass): class NumberAwareString(resultclass):
def __bool__(self): def __bool__(self):
return bool(kwargs['singular']) return bool(kwargs['singular'])
...@@ -134,9 +136,14 @@ def lazy_number(func, resultclass, number=None, **kwargs): ...@@ -134,9 +136,14 @@ def lazy_number(func, resultclass, number=None, **kwargs):
return translated return translated
proxy = lazy(lambda **kwargs: NumberAwareString(), NumberAwareString)(**kwargs) proxy = lazy(lambda **kwargs: NumberAwareString(), NumberAwareString)(**kwargs)
proxy.__reduce__ = lambda: (_lazy_number_unpickle, (func, resultclass, number, original_kwargs))
return proxy return proxy
def _lazy_number_unpickle(func, resultclass, number, kwargs):
return lazy_number(func, resultclass, number=number, **kwargs)
def ngettext_lazy(singular, plural, number=None): def ngettext_lazy(singular, plural, number=None):
return lazy_number(ngettext, str, singular=singular, plural=plural, number=number) return lazy_number(ngettext, str, singular=singular, plural=plural, number=number)
......
...@@ -14,3 +14,6 @@ Bugfixes ...@@ -14,3 +14,6 @@ Bugfixes
* Added system checks for query name clashes of hidden relationships * Added system checks for query name clashes of hidden relationships
(:ticket:`26162`). (:ticket:`26162`).
* Made ``forms.FileField`` and ``utils.translation.lazy_number()`` picklable
(:ticket:`26212`).
...@@ -24,3 +24,6 @@ Bugfixes ...@@ -24,3 +24,6 @@ Bugfixes
* Fixed regression with an ``__in=qs`` lookup for a ``ForeignKey`` with * Fixed regression with an ``__in=qs`` lookup for a ``ForeignKey`` with
``to_field`` set (:ticket:`26196`). ``to_field`` set (:ticket:`26196`).
* Made ``forms.FileField`` and ``utils.translation.lazy_number()`` picklable
(:ticket:`26212`).
...@@ -949,6 +949,9 @@ class FieldsTests(SimpleTestCase): ...@@ -949,6 +949,9 @@ class FieldsTests(SimpleTestCase):
# with here) # with here)
self.assertTrue(f.has_changed('resume.txt', {'filename': 'resume.txt', 'content': 'My resume'})) self.assertTrue(f.has_changed('resume.txt', {'filename': 'resume.txt', 'content': 'My resume'}))
def test_file_picklable(self):
self.assertIsInstance(pickle.loads(pickle.dumps(FileField())), FileField)
# ImageField ################################################################## # ImageField ##################################################################
@skipIf(Image is None, "Pillow is required to test ImageField") @skipIf(Image is None, "Pillow is required to test ImageField")
......
...@@ -243,6 +243,14 @@ class TranslationTests(SimpleTestCase): ...@@ -243,6 +243,14 @@ class TranslationTests(SimpleTestCase):
self.assertTrue(ungettext_lazy('%d good result', '%d good results')) self.assertTrue(ungettext_lazy('%d good result', '%d good results'))
self.assertFalse(ungettext_lazy('', '')) self.assertFalse(ungettext_lazy('', ''))
def test_ungettext_lazy_pickle(self):
s1 = ungettext_lazy('%d good result', '%d good results')
self.assertEqual(s1 % 1, '1 good result')
self.assertEqual(s1 % 8, '8 good results')
s2 = pickle.loads(pickle.dumps(s1))
self.assertEqual(s2 % 1, '1 good result')
self.assertEqual(s2 % 8, '8 good results')
@override_settings(LOCALE_PATHS=extended_locale_paths) @override_settings(LOCALE_PATHS=extended_locale_paths)
def test_pgettext(self): def test_pgettext(self):
trans_real._active = local() trans_real._active = local()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment