Skip to content
Myr

Added appcontext_pushed and appcontext_popped signals

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Armin Ronacher · committed by Armin Ronacher · parent fd99abe · Browse files · 7 files+87 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

7 files+87 added−2 removed

  • other:+87 added−2 removed

7 files+87 added−2 removed

modified:docs/api.rst+16 added-1 removed
533 unmodified lines
534535 This signal is sent when the application is tearing down the536 application context. This is always called, even if an error happened.537 An `exc` keyword argument is passed with the exception that caused the538 teardown.538 teardown. The sender is the application.539540.. data:: appcontext_pushed541542 This signal is sent when an application context is pushed. The sender543 is the application.544545 .. versionadded:: 0.10546547.. data:: appcontext_popped548549 This signal is sent when an application context is popped. The sender550 is the application. This usually falls in line with the551 :data:`appcontext_tearing_down` signal.552553 .. versionadded:: 0.10554555.. data:: message_flashed556557 This signal is sent when the application is flashing a message. The558 messages is sent as `message` keyword argument and the category as
176 unmodified lines
modified:docs/signals.rst+39 added
289 unmodified lines
290291 This will also be passed an `exc` keyword argument that has a reference292 to the exception that caused the teardown if there was one.293294.. data:: flask.appcontext_pushed295 :noindex:296297 This signal is sent when an application context is pushed. The sender298 is the application. This is usually useful for unittests in order to299 temporarily hook in information. For instance it can be used to300 set a resource early onto the `g` object.301302 Example usage::303304 from contextlib import contextmanager305 from flask import appcontext_pushed306307 @contextmanager308 def user_set(app, user):309 def handler(sender, **kwargs):310 g.user = user311 with appcontext_pushed.connected_to(handler, app):312 yield313314 And in the testcode::315316 def test_user_me(self):317 with user_set(app, 'john'):318 c = app.test_client()319 resp = c.get('/users/me')320 assert resp.data == 'username=john'321322 .. versionadded:: 0.10323324.. data:: appcontext_popped325326 This signal is sent when an application context is popped. The sender327 is the application. This usually falls in line with the328 :data:`appcontext_tearing_down` signal.329330 .. versionadded:: 0.10331332333.. data:: flask.message_flashed334 :noindex:335336 This signal is sent when the application is flashing a message. The
15 unmodified lines
modified:flask/testsuite/signals.py+24 added
94 unmodified lines
95 self.assert_true(isinstance(recorded[0], ZeroDivisionError))96 finally:97 flask.got_request_exception.disconnect(record, app)9899 def test_appcontext_signals(self):100 app = flask.Flask(__name__)101 recorded = []102 def record_push(sender, **kwargs):103 recorded.append('push')104 def record_pop(sender, **kwargs):105 recorded.append('push')106107 @app.route('/')108 def index():109 return 'Hello'110111 flask.appcontext_pushed.connect(record_push, app)112 flask.appcontext_popped.connect(record_pop, app)113 try:114 with app.test_client() as c:115 rv = c.get('/')116 self.assert_equal(rv.data, b'Hello')117 self.assert_equal(recorded, ['push'])118 self.assert_equal(recorded, ['push', 'pop'])119 finally:120 flask.appcontext_pushed.disconnect(record_push, app)121 flask.appcontext_popped.disconnect(record_pop, app)122123 def test_flash_signal(self):124 app = flask.Flask(__name__)125 app.config['SECRET_KEY'] = 'secret'126
27 unmodified lines
modified:flask/__init__.py+2 added-1 removed
32 unmodified lines
3334# the signals35from .signals import signals_available, template_rendered, request_started, \36 request_finished, got_request_exception, request_tearing_down, \37 appcontext_tearing_down, message_flashed37 appcontext_tearing_down, appcontext_pushed, \38 appcontext_popped, message_flashed3940# We're not exposing the actual json module but a convenient wrapper around41# it.42from . import json
8 unmodified lines
modified:flask/ctx.py+3 added
16 unmodified lines
17from werkzeug.exceptions import HTTPException1819from .globals import _request_ctx_stack, _app_ctx_stack20from .module import blueprint_is_module21from .signals import appcontext_pushed, appcontext_popped222324class _AppCtxGlobals(object):25 """A plain object."""
140 unmodified lines
166 def push(self):167 """Binds the app context to the current context."""168 self._refcnt += 1169 _app_ctx_stack.push(self)170 appcontext_pushed.send(self.app)171172 def pop(self, exc=None):173 """Pops the app context."""174 self._refcnt -= 1
3 unmodified lines
178 self.app.do_teardown_appcontext(exc)179 rv = _app_ctx_stack.pop()180 assert rv is self, 'Popped wrong app context. (%r instead of %r)' \181 % (rv, self)182 appcontext_popped.send(self.app)183184 def __enter__(self):185 self.push()186 return self
211 unmodified lines
modified:flask/signals.py+2 added
48 unmodified lines
49request_finished = _signals.signal('request-finished')50request_tearing_down = _signals.signal('request-tearing-down')51got_request_exception = _signals.signal('got-request-exception')52appcontext_tearing_down = _signals.signal('appcontext-tearing-down')53appcontext_pushed = _signals.signal('appcontext-pushed')54appcontext_popped = _signals.signal('appcontext-popped')55message_flashed = _signals.signal('message-flashed')
modified:CHANGES+1 added
68 unmodified lines
69 distinguish error from success cases.70- Added the ``JSONIFY_PRETTYPRINT_REGULAR`` configuration variable.71- Flask now orders JSON keys by default to not trash HTTP caches due to72 different hash seeds between different workers.73- Added `appcontext_pushed` and `appcontext_popped` signals.7475Version 0.976-----------77
365 unmodified lines