Fixed #27513 -- Made Signal.send()/send_robust() a tiny bit faster.

This commit is contained in:
Adam Chainz 2016-11-22 14:30:14 +00:00 committed by Tim Graham
parent c70ffeeb8b
commit 22a60f8d0b
2 changed files with 10 additions and 11 deletions

View File

@ -185,14 +185,13 @@ class Signal(object):
Returns a list of tuple pairs [(receiver, response), ... ]. Returns a list of tuple pairs [(receiver, response), ... ].
""" """
responses = []
if not self.receivers or self.sender_receivers_cache.get(sender) is NO_RECEIVERS: if not self.receivers or self.sender_receivers_cache.get(sender) is NO_RECEIVERS:
return responses return []
for receiver in self._live_receivers(sender): return [
response = receiver(signal=self, sender=sender, **named) (receiver, receiver(signal=self, sender=sender, **named))
responses.append((receiver, response)) for receiver in self._live_receivers(sender)
return responses ]
def send_robust(self, sender, **named): def send_robust(self, sender, **named):
""" """
@ -218,12 +217,12 @@ class Signal(object):
receiver. The traceback is always attached to the error at receiver. The traceback is always attached to the error at
``__traceback__``. ``__traceback__``.
""" """
responses = []
if not self.receivers or self.sender_receivers_cache.get(sender) is NO_RECEIVERS: if not self.receivers or self.sender_receivers_cache.get(sender) is NO_RECEIVERS:
return responses return []
# Call each receiver with whatever arguments it can accept. # Call each receiver with whatever arguments it can accept.
# Return a list of tuple pairs [(receiver, response), ... ]. # Return a list of tuple pairs [(receiver, response), ... ].
responses = []
for receiver in self._live_receivers(sender): for receiver in self._live_receivers(sender):
try: try:
response = receiver(signal=self, sender=sender, **named) response = receiver(signal=self, sender=sender, **named)

View File

@ -8,7 +8,7 @@ from django.core.signals import setting_changed
from django.db import connections, router from django.db import connections, router
from django.db.utils import ConnectionRouter from django.db.utils import ConnectionRouter
from django.dispatch import Signal, receiver from django.dispatch import Signal, receiver
from django.utils import timezone from django.utils import six, timezone
from django.utils.functional import empty from django.utils.functional import empty
template_rendered = Signal(providing_args=["template", "context"]) template_rendered = Signal(providing_args=["template", "context"])
@ -127,9 +127,9 @@ def file_storage_changed(**kwargs):
def complex_setting_changed(**kwargs): def complex_setting_changed(**kwargs):
if kwargs['enter'] and kwargs['setting'] in COMPLEX_OVERRIDE_SETTINGS: if kwargs['enter'] and kwargs['setting'] in COMPLEX_OVERRIDE_SETTINGS:
# Considering the current implementation of the signals framework, # Considering the current implementation of the signals framework,
# stacklevel=5 shows the line containing the override_settings call. # this stacklevel shows the line containing the override_settings call.
warnings.warn("Overriding setting %s can lead to unexpected behavior." warnings.warn("Overriding setting %s can lead to unexpected behavior."
% kwargs['setting'], stacklevel=5) % kwargs['setting'], stacklevel=5 if six.PY2 else 6)
@receiver(setting_changed) @receiver(setting_changed)