Fixed #26778 -- Fixed ModelSignal.connect() weak argument.

This commit is contained in:
Tim Graham 2016-06-18 20:45:38 -04:00 committed by GitHub
parent 8ba44ecda0
commit 9bf8d50a67
2 changed files with 18 additions and 2 deletions

View File

@ -27,7 +27,10 @@ class ModelSignal(Signal):
return partial_method(sender) return partial_method(sender)
def connect(self, receiver, sender=None, weak=True, dispatch_uid=None, apps=None): 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): def disconnect(self, receiver=None, sender=None, weak=None, dispatch_uid=None, apps=None):
if weak is not 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 import models
from django.db.models import signals from django.db.models import signals
from django.dispatch import receiver 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.test.utils import isolate_apps
from django.utils import six from django.utils import six
@ -258,6 +258,19 @@ class SignalTests(BaseSignalTest):
self.assertTrue(b._run) self.assertTrue(b._run)
self.assertEqual(signals.post_save.receivers, []) 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): class LazyModelRefTest(BaseSignalTest):
def setUp(self): def setUp(self):