Code

Ticket #17029: pickled_signal.diff

File pickled_signal.diff, 1.8 KB (added by d0ugal, 3 years ago)
Line 
1diff --git a/AUTHORS b/AUTHORS
2index b64a627..689b2d1 100644
3--- a/AUTHORS
4+++ b/AUTHORS
5@@ -550,6 +550,7 @@ answer newbie questions, and generally made Django that much better:
6     Gasper Zejn <zejn@kiberpipa.org>
7     Jarek Zgoda <jarek.zgoda@gmail.com>
8     Cheng Zhang
9+    Dougal Matthews <dougal@dougalmatthews.com>
10 
11 A big THANK YOU goes to:
12 
13diff --git a/django/dispatch/dispatcher.py b/django/dispatch/dispatcher.py
14index ed9da57..857b06f 100644
15--- a/django/dispatch/dispatcher.py
16+++ b/django/dispatch/dispatcher.py
17@@ -253,6 +253,14 @@ class Signal(object):
18         finally:
19             self.lock.release()
20 
21+    def __reduce__(self):
22+        """
23+        Return a 2-tuple containing a callable and the arguements for the
24+        callable which can be used by pickle to pickle and unpickle Signal
25+        instances.
26+        """
27+        return (Signal, (self.providing_args,))
28+
29 
30 def receiver(signal, **kwargs):
31     """
32diff --git a/tests/regressiontests/signals_regress/tests.py b/tests/regressiontests/signals_regress/tests.py
33index 332f5d9..90917d7 100644
34--- a/tests/regressiontests/signals_regress/tests.py
35+++ b/tests/regressiontests/signals_regress/tests.py
36@@ -92,3 +92,13 @@ class SignalsRegressTests(TestCase):
37         self.get_signal_output(a1.save)
38         self.assertEqual(self.get_signal_output(setattr, b1, 'authors', [a1]), [])
39         self.assertEqual(self.get_signal_output(setattr, b1, 'authors', []), [])
40+
41+    def test_pickle_signal(self):
42+        """ Test that a Signal instance can be pickled."""
43+
44+        import pickle
45+        pickled_string = """cdjango.dispatch.dispatcher\nSignal\np0\n(c__builtin__
46+set\np1\n((lp2\nS'instance'\np3\naS'using'\np4\naS'raw'\np5\natp6\nRp7\ntp8\nRp9\n."""
47+
48+        result = pickle.dumps(models.signals.pre_save)
49+        self.assertEqual(pickled_string, result)