From 9bf8d50a678463a5a3cd52f10c830fe0ca65d2c6 Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Sat, 18 Jun 2016 20:45:38 -0400 Subject: [PATCH] Fixed #26778 -- Fixed ModelSignal.connect() weak argument. --- django/db/models/signals.py | 5 ++++- tests/signals/tests.py | 15 ++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/django/db/models/signals.py b/django/db/models/signals.py index 51c3ff0554..5047f11743 100644 --- a/django/db/models/signals.py +++ b/django/db/models/signals.py @@ -27,7 +27,10 @@ class ModelSignal(Signal): return partial_method(sender) def connect(self, receiver, sender=None, weak=True, dispatch_uid=None, apps=None): - self._lazy_method(super(ModelSignal, self).connect, apps, receiver, sender, dispatch_uid=dispatch_uid) + self._lazy_method( + super(ModelSignal, self).connect, apps, receiver, sender, + weak=weak, dispatch_uid=dispatch_uid, + ) def disconnect(self, receiver=None, sender=None, weak=None, dispatch_uid=None, apps=None): if weak is not None: diff --git a/tests/signals/tests.py b/tests/signals/tests.py index 0ed0f85f6b..b52cf1d11c 100644 --- a/tests/signals/tests.py +++ b/tests/signals/tests.py @@ -4,7 +4,7 @@ from django.apps.registry import Apps from django.db import models from django.db.models import signals from django.dispatch import receiver -from django.test import TestCase +from django.test import TestCase, mock from django.test.utils import isolate_apps from django.utils import six @@ -258,6 +258,19 @@ class SignalTests(BaseSignalTest): self.assertTrue(b._run) self.assertEqual(signals.post_save.receivers, []) + @mock.patch('weakref.ref') + def test_lazy_model_signal(self, ref): + def callback(sender, args, **kwargs): + pass + signals.pre_init.connect(callback) + signals.pre_init.disconnect(callback) + self.assertTrue(ref.called) + ref.reset_mock() + + signals.pre_init.connect(callback, weak=False) + signals.pre_init.disconnect(callback) + ref.assert_not_called() + class LazyModelRefTest(BaseSignalTest): def setUp(self):