[1.10.x] Fixed #26778 -- Fixed ModelSignal.connect() weak argument.

Backport of 9bf8d50a67 from master
This commit is contained in:
Tim Graham 2016-06-18 20:45:38 -04:00
parent 1b6de8fd23
commit 91d754eb2a
2 changed files with 18 additions and 2 deletions

View File

@ -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:

View File

@ -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):