Skip to content
Myr

Added notes on proxies

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 c0abdc4 · Browse files · 2 files+63 added−18 removed
This commit

State

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

Diff stats

2 files+63 added−18 removed

  • other:+63 added−18 removed

2 files+63 added−18 removed

modified:docs/api.rst+35 added
36 unmodified lines
37 access to it through that global object. Internally Flask makes38 sure that you always get the correct data for the active thread if you39 are in a multithreaded environment.4041 This is a proxy. See :ref:`notes-on-proxies` for more information.4243 The request object is an instance of a :class:`~werkzeug.Request`44 subclass and provides all of the attributes Werkzeug defines. This45 just shows a quick overview of the most important ones.46
118 unmodified lines
165166 The session object works pretty much like an ordinary dict, with the167 difference that it keeps track on modifications.168169 This is a proxy. See :ref:`notes-on-proxies` for more information.170171 The following attributes are interesting:172173 .. attribute:: new174
34 unmodified lines
209210 Just store on this whatever you want. For example a database211 connection or the user that is currently logged in.212213 This is a proxy. See :ref:`notes-on-proxies` for more information.214215216Useful Functions and Classes217----------------------------218219.. data:: current_app
1 unmodified line
221 Points to the application handling the request. This is useful for222 extensions that want to support multiple applications running side223 by side.224225 This is a proxy. See :ref:`notes-on-proxies` for more information.226227.. autofunction:: url_for228229.. function:: abort(code)230
165 unmodified lines
396 It is sent *before* the standard exception handling kicks in and even397 in debug mode, where no exception handling happens. The exception398 itself is passed to the subscriber as `exception`.399400.. currentmodule:: None401402.. class:: flask.signals.Namespace403404 An alias for :class:`blinker.base.Namespace` if blinker is available,405 otherwise a dummy class that creates fake signals. This class is
7 unmodified lines
413 do nothing but will fail with a :exc:`RuntimeError` for all other414 operations, including connecting.415416.. _blinker: http://pypi.python.org/pypi/blinker417418.. _notes-on-proxies:419420Notes On Proxies421----------------422423Some of the objects provided by Flask are proxies to other objects. The424reason behind this is, that these proxies are shared between threads and425they have to dispatch to the actual object bound to a thread behind the426scenes as necessary.427428Most of the time you don't have to care about that, but there are some429exceptions where it is good to know that this object is an actual proxy:430431- The proxy objects do not fake their inherited types, so if you want to432 perform actual instance checks, you have to do that on the instance433 that434- if the object reference is important (so for example for sending435 :ref:`signals`)436437If you need to get access to the underlying object that is proxied, you438can use the :meth:`~werkzeug.LocalProxy._get_current_object` method::439440 app = current_app._get_current_object()441 my_signal.send(app)
modified:docs/signals.rst+28 added-18 removed
39 unmodified lines
40the optional second argument specifies a sender. To unsubscribe from a41signal, you can use the :meth:`~blinker.base.Signal.disconnect` method.4243For all core Flask signals, the sender is the application that issued the44signal. This however might not be true for Flask extensions, so consult45the documentation when subscribing to signals.44signal. When you subscribe to a signal, be sure to also provide a sender45unless you really want to listen for signals of all applications. This is46especially true if you are developing an extension.4747Additionally there is a convenient helper method that allows you to48temporarily subscribe a function to a signal. This is especially helpful49for unittests (:meth:`~blinker.base.Signal.temporarily_connected_to`).50This has to be used in combination with the `with` statement.5148Here for example a helper context manager that can be used to figure out49in a unittest which templates were rendered and what variables were passed50to the template::5152 from flask import template_rendered53 from contextlib import contextmanager5455 @contextmanager60 def captured_templates():56 def captured_templates(app):57 recorded = []58 def record(template, context):59 recorded.append((template, context))64 template_rendered.connect(record)60 template_rendered.connect(record, app)61 try:62 yield recorded63 finally:68 template_rendered.disconnect(record)64 template_rendered.disconnect(record, app)6566This can now easily be paired with a test client::6772 with captured_templates() as templates:68 with captured_templates(app) as templates:69 rv = app.test_client().get('/')70 assert rv.status_code == 20071 assert len(templates) == 172 template, context = templates[0]73 assert template.name == 'index.html'74 assert len(context['items']) == 107580All the template rendering in the code, the `with` block wraps will now be81recorded in the `templates` variable. Whenever a template is rendered,82the template object as well as context is appended to it.76All the template rendering in the code issued by the application `app`77in the body of the `with` block will now be recorded in the `templates`78variable. Whenever a template is rendered, the template object as well as79context are appended to it.8081Additionally there is a convenient helper method82(:meth:`~blinker.base.Signal.temporarily_connected_to`). that allows you83to temporarily subscribe a function to a signal with is a context manager84on its own which simplifies the example above::8586 from flask import template_rendered8788 def captured_templates(app):89 recorded = []90 def record(template, context):91 recorded.append((template, context))92 return template_rendered.temporarily_connected_to(record, app)9394Creating Signals95----------------9697If you want to use signals in your own application, you can use the
64 unmodified lines
162 template.name or 'string template',163 context)164165 from flask import request_started156 request_started.connect(log_template_renders)166 request_started.connect(log_template_renders, app)167168.. data:: flask.request_started169 :noindex:170
7 unmodified lines
178 def log_request(sender):179 sender.logger.debug('Request context is set up')180181 from flask import request_started172 request_started.connect(log_request)182 request_started.connect(log_request, app)183184.. data:: flask.request_finished185 :noindex:186
6 unmodified lines
193 sender.logger.debug('Request context is about to close down. '194 'Response: %s', response)195196 from flask import request_finished187 request_finished.connect(log_response)197 request_finished.connect(log_response, app)198199.. data:: flask.got_request_exception200 :noindex:201
7 unmodified lines
209 def log_exception(sender, exception):210 sender.logger.debug('Got exception during processing: %s', exception)211212 from flask import got_request_exception203 got_request_exception.connect(log_exception)213 got_request_exception.connect(log_exception, app)214215.. _blinker: http://pypi.python.org/pypi/blinker