Skip to content

Commit

Permalink
Fixed Application.on_loop_available signal #1739
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 committed Mar 21, 2017
1 parent a05542b commit 20ef6f5
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 12 deletions.
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ Changes
=======


2.0.2 (2017-03-21)
------------------

- Fixed Application.on_loop_available signal #1739

- Remove debug code


Expand Down
2 changes: 1 addition & 1 deletion aiohttp/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '2.0.1'
__version__ = '2.0.2'

# This relies on each of the submodules having an __all__ variable.

Expand Down
38 changes: 30 additions & 8 deletions aiohttp/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,36 @@ def send(self, *args, **kwargs):
"""
Sends data to all registered receivers.
"""
ordinal = None
debug = self._app._debug
if debug:
ordinal = self._pre.ordinal()
yield from self._pre.send(ordinal, self._name, *args, **kwargs)
yield from self._send(*args, **kwargs)
if debug:
yield from self._post.send(ordinal, self._name, *args, **kwargs)
if self._items:
ordinal = None
debug = self._app._debug
if debug:
ordinal = self._pre.ordinal()
yield from self._pre.send(
ordinal, self._name, *args, **kwargs)
yield from self._send(*args, **kwargs)
if debug:
yield from self._post.send(
ordinal, self._name, *args, **kwargs)


class FuncSignal(BaseSignal):
"""Callback-based signal implementation.
To connect a callback to a signal, use any list method.
Signals are fired using the :meth:`send` method, which takes named
arguments.
"""

__slots__ = ()

def send(self, *args, **kwargs):
"""
Sends data to all registered receivers.
"""
for receiver in self._items:
receiver(*args, **kwargs)


class DebugSignal(BaseSignal):
Expand Down
6 changes: 3 additions & 3 deletions aiohttp/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from .helpers import FrozenList
from .http import HttpVersion # noqa
from .log import access_logger, web_logger
from .signals import PostSignal, PreSignal, Signal
from .signals import FuncSignal, PostSignal, PreSignal, Signal
from .web_exceptions import * # noqa
from .web_fileresponse import * # noqa
from .web_middlewares import * # noqa
Expand Down Expand Up @@ -67,7 +67,7 @@ def __init__(self, *, logger=web_logger, router=None, middlewares=(),

self._on_pre_signal = PreSignal()
self._on_post_signal = PostSignal()
self._on_loop_available = Signal(self)
self._on_loop_available = FuncSignal(self)
self._on_response_prepare = Signal(self)
self._on_startup = Signal(self)
self._on_shutdown = Signal(self)
Expand Down Expand Up @@ -113,7 +113,7 @@ def _set_loop(self, loop):
"web.Application instance initialized with different loop")

self._loop = loop
self._on_loop_available.send()
self._on_loop_available.send(self)

# set loop debug
if self._debug is ...:
Expand Down
10 changes: 10 additions & 0 deletions tests/test_web_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ def test_set_loop_with_different_loops(loop):
app._set_loop(loop=object())


def test_on_loop_available(loop):
app = web.Application()

cb = mock.Mock()
app.on_loop_available.append(cb)

app._set_loop(loop)
cb.assert_called_with(app)


@pytest.mark.parametrize('debug', [True, False])
def test_app_make_handler_debug_exc(loop, mocker, debug):
app = web.Application(debug=debug)
Expand Down

0 comments on commit 20ef6f5

Please sign in to comment.