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
Provenance
- Commit
0676bb8- Tree
3014a3d- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
fd99abe
7 files changed+87 added−2 removed
modified:docs/api.rst+16 added-1 removed
533 unmodified lines534 535 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.539 540+.. data:: appcontext_pushed541+542+ This signal is sent when an application context is pushed. The sender543+ is the application.544+545+ .. versionadded:: 0.10546+547+.. data:: appcontext_popped548+549+ 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.552+553+ .. versionadded:: 0.10554+555 .. data:: message_flashed556 557 This signal is sent when the application is flashing a message. The558 messages is sent as `message` keyword argument and the category as176 unmodified linesmodified:docs/signals.rst+39 added
289 unmodified lines290 291 This will also be passed an `exc` keyword argument that has a reference292 to the exception that caused the teardown if there was one.293 294+.. data:: flask.appcontext_pushed295+ :noindex:296+297+ 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.301+302+ Example usage::303+304+ from contextlib import contextmanager305+ from flask import appcontext_pushed306+307+ @contextmanager308+ def user_set(app, user):309+ def handler(sender, **kwargs):310+ g.user = user311+ with appcontext_pushed.connected_to(handler, app):312+ yield313+314+ And in the testcode::315+316+ 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'321+322+ .. versionadded:: 0.10323+324+.. data:: appcontext_popped325+326+ 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.329+330+ .. versionadded:: 0.10331+332+333 .. data:: flask.message_flashed334 :noindex:335 336 This signal is sent when the application is flashing a message. The15 unmodified linesmodified:flask/testsuite/signals.py+24 added
94 unmodified lines95 self.assert_true(isinstance(recorded[0], ZeroDivisionError))96 finally:97 flask.got_request_exception.disconnect(record, app)98 99+ 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')106+107+ @app.route('/')108+ def index():109+ return 'Hello'110+111+ 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)122+123 def test_flash_signal(self):124 app = flask.Flask(__name__)125 app.config['SECRET_KEY'] = 'secret'126 27 unmodified linesmodified:flask/__init__.py+2 added-1 removed
32 unmodified lines33 34 # the signals35 from .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_flashed39 40 # We're not exposing the actual json module but a convenient wrapper around41 # it.42 from . import json8 unmodified linesmodified:flask/ctx.py+3 added
16 unmodified lines17 from werkzeug.exceptions import HTTPException18 19 from .globals import _request_ctx_stack, _app_ctx_stack20 from .module import blueprint_is_module21+from .signals import appcontext_pushed, appcontext_popped22 23 24 class _AppCtxGlobals(object):25 """A plain object."""140 unmodified lines166 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)171 172 def pop(self, exc=None):173 """Pops the app context."""174 self._refcnt -= 13 unmodified lines178 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)183 184 def __enter__(self):185 self.push()186 return self211 unmodified linesmodified:flask/signals.py+2 added
48 unmodified lines49 request_finished = _signals.signal('request-finished')50 request_tearing_down = _signals.signal('request-tearing-down')51 got_request_exception = _signals.signal('got-request-exception')52 appcontext_tearing_down = _signals.signal('appcontext-tearing-down')53+appcontext_pushed = _signals.signal('appcontext-pushed')54+appcontext_popped = _signals.signal('appcontext-popped')55 message_flashed = _signals.signal('message-flashed')modified:CHANGES+1 added
68 unmodified lines69 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.74 75 Version 0.976 -----------77 365 unmodified lines