This file is indexed.

/usr/share/doc/python-werkzeug-doc/html/local.html is in python-werkzeug-doc 0.10.4+dfsg1-1ubuntu1.1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Context Locals &mdash; Werkzeug 0.10.4 documentation</title>
    
    <link rel="stylesheet" href="_static/werkzeug.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    './',
        VERSION:     '0.10.4',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <link rel="top" title="Werkzeug 0.10.4 documentation" href="index.html" />
    <link rel="next" title="Middlewares" href="middlewares.html" />
    <link rel="prev" title="URL Helpers" href="urls.html" /> 
  </head>
  <body role="document">
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="middlewares.html" title="Middlewares"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="urls.html" title="URL Helpers"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="index.html">Werkzeug 0.10.4 documentation</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="module-werkzeug.local">
<span id="context-locals"></span><h1>Context Locals<a class="headerlink" href="#module-werkzeug.local" title="Permalink to this headline"></a></h1>
<p>Sooner or later you have some things you want to have in every single view
or helper function or whatever.  In PHP the way to go are global
variables.  However, that isn&#8217;t possible in WSGI applications without a
major drawback:  As soon as you operate on the global namespace your
application isn&#8217;t thread-safe any longer.</p>
<p>The Python standard library comes with a utility called &#8220;thread locals&#8221;.
A thread local is a global object in which you can put stuff in and get back
later in a thread-safe way.  That means whenever you set or get an object
on a thread local object, the thread local object checks in which
thread you are and retrieves the correct value.</p>
<p>This, however, has a few disadvantages.  For example, besides threads there
are other ways to handle concurrency in Python.  A very popular approach
is greenlets.  Also, whether every request gets its own thread is not
guaranteed in WSGI.  It could be that a request is reusing a thread from
before, and hence data is left in the thread local object.</p>
<p>Here&#8217;s a simple example of how one could use werkzeug.local:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">werkzeug.local</span> <span class="kn">import</span> <span class="n">Local</span><span class="p">,</span> <span class="n">LocalManager</span>

<span class="n">local</span> <span class="o">=</span> <span class="n">Local</span><span class="p">()</span>
<span class="n">local_manager</span> <span class="o">=</span> <span class="n">LocalManager</span><span class="p">([</span><span class="n">local</span><span class="p">])</span>

<span class="k">def</span> <span class="nf">application</span><span class="p">(</span><span class="n">environ</span><span class="p">,</span> <span class="n">start_response</span><span class="p">):</span>
    <span class="n">local</span><span class="o">.</span><span class="n">request</span> <span class="o">=</span> <span class="n">request</span> <span class="o">=</span> <span class="n">Request</span><span class="p">(</span><span class="n">environ</span><span class="p">)</span>
    <span class="o">...</span>

<span class="n">application</span> <span class="o">=</span> <span class="n">local_manager</span><span class="o">.</span><span class="n">make_middleware</span><span class="p">(</span><span class="n">application</span><span class="p">)</span>
</pre></div>
</div>
<p>This binds the request to <cite>local.request</cite>.  Every other piece of code executed
after this assignment in the same context can safely access local.request and
will get the same request object.  The <cite>make_middleware</cite> method on the local
manager ensures that all references to the local objects are cleared up after
the request.</p>
<p>The same context means the same greenlet (if you&#8217;re using greenlets) in
the same thread and same process.</p>
<p>If a request object is not yet set on the local object and you try to
access it, you will get an <cite>AttributeError</cite>.  You can use <cite>getattr</cite> to avoid
that:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">get_request</span><span class="p">():</span>
    <span class="k">return</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">local</span><span class="p">,</span> <span class="s1">&#39;request&#39;</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span>
</pre></div>
</div>
<p>This will try to get the request or return <cite>None</cite> if the request is not
(yet?) available.</p>
<p>Note that local objects cannot manage themselves, for that you need a local
manager.  You can pass a local manager multiple locals or add additionals
later by appending them to <cite>manager.locals</cite> and everytime the manager
cleans up it will clean up all the data left in the locals for this
context.</p>
<dl class="function">
<dt id="werkzeug.local.release_local">
<code class="descclassname">werkzeug.local.</code><code class="descname">release_local</code><span class="sig-paren">(</span><em>local</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.local.release_local" title="Permalink to this definition"></a></dt>
<dd><p>Releases the contents of the local for the current context.
This makes it possible to use locals without a manager.</p>
<p>Example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">loc</span> <span class="o">=</span> <span class="n">Local</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">loc</span><span class="o">.</span><span class="n">foo</span> <span class="o">=</span> <span class="mi">42</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">release_local</span><span class="p">(</span><span class="n">loc</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">hasattr</span><span class="p">(</span><span class="n">loc</span><span class="p">,</span> <span class="s1">&#39;foo&#39;</span><span class="p">)</span>
<span class="go">False</span>
</pre></div>
</div>
<p>With this function one can release <code class="xref py py-class docutils literal"><span class="pre">Local</span></code> objects as well
as <a class="reference internal" href="#werkzeug.local.LocalStack" title="werkzeug.local.LocalStack"><code class="xref py py-class docutils literal"><span class="pre">LocalStack</span></code></a> objects.  However it is not possible to
release data held by proxies that way, one always has to retain
a reference to the underlying local object in order to be able
to release it.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.6.1.</span></p>
</div>
</dd></dl>

<dl class="class">
<dt id="werkzeug.local.LocalManager">
<em class="property">class </em><code class="descclassname">werkzeug.local.</code><code class="descname">LocalManager</code><span class="sig-paren">(</span><em>locals=None</em>, <em>ident_func=None</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.local.LocalManager" title="Permalink to this definition"></a></dt>
<dd><p>Local objects cannot manage themselves. For that you need a local
manager.  You can pass a local manager multiple locals or add them later
by appending them to <cite>manager.locals</cite>.  Everytime the manager cleans up
it, will clean up all the data left in the locals for this context.</p>
<p>The <cite>ident_func</cite> parameter can be added to override the default ident
function for the wrapped locals.</p>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 0.6.1: </span>Instead of a manager the <a class="reference internal" href="#werkzeug.local.release_local" title="werkzeug.local.release_local"><code class="xref py py-func docutils literal"><span class="pre">release_local()</span></code></a> function can be used
as well.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 0.7: </span><cite>ident_func</cite> was added.</p>
</div>
<dl class="method">
<dt id="werkzeug.local.LocalManager.cleanup">
<code class="descname">cleanup</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.local.LocalManager.cleanup" title="Permalink to this definition"></a></dt>
<dd><p>Manually clean up the data in the locals for this context.  Call
this at the end of the request or use <cite>make_middleware()</cite>.</p>
</dd></dl>

<dl class="method">
<dt id="werkzeug.local.LocalManager.get_ident">
<code class="descname">get_ident</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.local.LocalManager.get_ident" title="Permalink to this definition"></a></dt>
<dd><p>Return the context identifier the local objects use internally for
this context.  You cannot override this method to change the behavior
but use it to link other context local objects (such as SQLAlchemy&#8217;s
scoped sessions) to the Werkzeug locals.</p>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 0.7: </span>You can pass a different ident function to the local manager that
will then be propagated to all the locals passed to the
constructor.</p>
</div>
</dd></dl>

<dl class="method">
<dt id="werkzeug.local.LocalManager.make_middleware">
<code class="descname">make_middleware</code><span class="sig-paren">(</span><em>app</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.local.LocalManager.make_middleware" title="Permalink to this definition"></a></dt>
<dd><p>Wrap a WSGI application so that cleaning up happens after
request end.</p>
</dd></dl>

<dl class="method">
<dt id="werkzeug.local.LocalManager.middleware">
<code class="descname">middleware</code><span class="sig-paren">(</span><em>func</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.local.LocalManager.middleware" title="Permalink to this definition"></a></dt>
<dd><p>Like <cite>make_middleware</cite> but for decorating functions.</p>
<p>Example usage:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="nd">@manager.middleware</span>
<span class="k">def</span> <span class="nf">application</span><span class="p">(</span><span class="n">environ</span><span class="p">,</span> <span class="n">start_response</span><span class="p">):</span>
    <span class="o">...</span>
</pre></div>
</div>
<p>The difference to <cite>make_middleware</cite> is that the function passed
will have all the arguments copied from the inner application
(name, docstring, module).</p>
</dd></dl>

</dd></dl>

<dl class="class">
<dt id="werkzeug.local.LocalStack">
<em class="property">class </em><code class="descclassname">werkzeug.local.</code><code class="descname">LocalStack</code><a class="headerlink" href="#werkzeug.local.LocalStack" title="Permalink to this definition"></a></dt>
<dd><p>This class works similar to a <code class="xref py py-class docutils literal"><span class="pre">Local</span></code> but keeps a stack
of objects instead.  This is best explained with an example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">ls</span> <span class="o">=</span> <span class="n">LocalStack</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ls</span><span class="o">.</span><span class="n">push</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ls</span><span class="o">.</span><span class="n">top</span>
<span class="go">42</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ls</span><span class="o">.</span><span class="n">push</span><span class="p">(</span><span class="mi">23</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ls</span><span class="o">.</span><span class="n">top</span>
<span class="go">23</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ls</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>
<span class="go">23</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ls</span><span class="o">.</span><span class="n">top</span>
<span class="go">42</span>
</pre></div>
</div>
<p>They can be force released by using a <a class="reference internal" href="#werkzeug.local.LocalManager" title="werkzeug.local.LocalManager"><code class="xref py py-class docutils literal"><span class="pre">LocalManager</span></code></a> or with
the <a class="reference internal" href="#werkzeug.local.release_local" title="werkzeug.local.release_local"><code class="xref py py-func docutils literal"><span class="pre">release_local()</span></code></a> function but the correct way is to pop the
item from the stack after using.  When the stack is empty it will
no longer be bound to the current context (and as such released).</p>
<p>By calling the stack without arguments it returns a proxy that resolves to
the topmost item on the stack.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.6.1.</span></p>
</div>
<dl class="method">
<dt id="werkzeug.local.LocalStack.pop">
<code class="descname">pop</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.local.LocalStack.pop" title="Permalink to this definition"></a></dt>
<dd><p>Removes the topmost item from the stack, will return the
old value or <cite>None</cite> if the stack was already empty.</p>
</dd></dl>

<dl class="method">
<dt id="werkzeug.local.LocalStack.push">
<code class="descname">push</code><span class="sig-paren">(</span><em>obj</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.local.LocalStack.push" title="Permalink to this definition"></a></dt>
<dd><p>Pushes a new item to the stack</p>
</dd></dl>

<dl class="attribute">
<dt id="werkzeug.local.LocalStack.top">
<code class="descname">top</code><a class="headerlink" href="#werkzeug.local.LocalStack.top" title="Permalink to this definition"></a></dt>
<dd><p>The topmost item on the stack.  If the stack is empty,
<cite>None</cite> is returned.</p>
</dd></dl>

</dd></dl>

<dl class="class">
<dt id="werkzeug.local.LocalProxy">
<em class="property">class </em><code class="descclassname">werkzeug.local.</code><code class="descname">LocalProxy</code><span class="sig-paren">(</span><em>local</em>, <em>name=None</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.local.LocalProxy" title="Permalink to this definition"></a></dt>
<dd><p>Acts as a proxy for a werkzeug local.  Forwards all operations to
a proxied object.  The only operations not supported for forwarding
are right handed operands and any kind of assignment.</p>
<p>Example usage:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">werkzeug.local</span> <span class="kn">import</span> <span class="n">Local</span>
<span class="n">l</span> <span class="o">=</span> <span class="n">Local</span><span class="p">()</span>

<span class="c1"># these are proxies</span>
<span class="n">request</span> <span class="o">=</span> <span class="n">l</span><span class="p">(</span><span class="s1">&#39;request&#39;</span><span class="p">)</span>
<span class="n">user</span> <span class="o">=</span> <span class="n">l</span><span class="p">(</span><span class="s1">&#39;user&#39;</span><span class="p">)</span>


<span class="kn">from</span> <span class="nn">werkzeug.local</span> <span class="kn">import</span> <span class="n">LocalStack</span>
<span class="n">_response_local</span> <span class="o">=</span> <span class="n">LocalStack</span><span class="p">()</span>

<span class="c1"># this is a proxy</span>
<span class="n">response</span> <span class="o">=</span> <span class="n">_response_local</span><span class="p">()</span>
</pre></div>
</div>
<p>Whenever something is bound to l.user / l.request the proxy objects
will forward all operations.  If no object is bound a <code class="xref py py-exc docutils literal"><span class="pre">RuntimeError</span></code>
will be raised.</p>
<p>To create proxies to <code class="xref py py-class docutils literal"><span class="pre">Local</span></code> or <a class="reference internal" href="#werkzeug.local.LocalStack" title="werkzeug.local.LocalStack"><code class="xref py py-class docutils literal"><span class="pre">LocalStack</span></code></a> objects,
call the object as shown above.  If you want to have a proxy to an
object looked up by a function, you can (as of Werkzeug 0.6.1) pass
a function to the <a class="reference internal" href="#werkzeug.local.LocalProxy" title="werkzeug.local.LocalProxy"><code class="xref py py-class docutils literal"><span class="pre">LocalProxy</span></code></a> constructor:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">session</span> <span class="o">=</span> <span class="n">LocalProxy</span><span class="p">(</span><span class="k">lambda</span><span class="p">:</span> <span class="n">get_current_request</span><span class="p">()</span><span class="o">.</span><span class="n">session</span><span class="p">)</span>
</pre></div>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 0.6.1: </span>The class can be instanciated with a callable as well now.</p>
</div>
<p>Keep in mind that <code class="docutils literal"><span class="pre">repr()</span></code> is also forwarded, so if you want to find
out if you are dealing with a proxy you can do an <code class="docutils literal"><span class="pre">isinstance()</span></code> check:</p>
<div class="highlight-pycon"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">werkzeug.local</span> <span class="kn">import</span> <span class="n">LocalProxy</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">isinstance</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">LocalProxy</span><span class="p">)</span>
<span class="go">True</span>
</pre></div>
</div>
<p>You can also create proxy objects by hand:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">werkzeug.local</span> <span class="kn">import</span> <span class="n">Local</span><span class="p">,</span> <span class="n">LocalProxy</span>
<span class="n">local</span> <span class="o">=</span> <span class="n">Local</span><span class="p">()</span>
<span class="n">request</span> <span class="o">=</span> <span class="n">LocalProxy</span><span class="p">(</span><span class="n">local</span><span class="p">,</span> <span class="s1">&#39;request&#39;</span><span class="p">)</span>
</pre></div>
</div>
<dl class="method">
<dt id="werkzeug.local.LocalProxy._get_current_object">
<code class="descname">_get_current_object</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.local.LocalProxy._get_current_object" title="Permalink to this definition"></a></dt>
<dd><p>Return the current object.  This is useful if you want the real
object behind the proxy at a time for performance reasons or because
you want to pass the object into a different context.</p>
</dd></dl>

</dd></dl>

</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper"><p class="logo"><a href="index.html">
  <img class="logo" src="_static/werkzeug.png" alt="Logo"/>
</a></p><h3>Related Topics</h3>
<ul>
  <li><a href="index.html">Documentation overview</a><ul>
      <li>Previous: <a href="urls.html" title="previous chapter">URL Helpers</a></li>
      <li>Next: <a href="middlewares.html" title="next chapter">Middlewares</a></li>
  </ul></li>
</ul>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="_sources/local.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <form class="search" action="search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="footer">
      &copy; Copyright 2011, The Werkzeug Team.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a>.
    </div>
  </body>
</html>