jobcontrol.utils.local

werkzeug.local

This module implements context-local objects.

copyright:
  1. 2014 by the Werkzeug Team, see AUTHORS for more details.
license:

BSD, see LICENSE for more details.

jobcontrol.utils.local.implements_bool(cls)[source]
jobcontrol.utils.local.release_local(local)[source]

Releases the contents of the local for the current context. This makes it possible to use locals without a manager.

Example:

>>> loc = Local()
>>> loc.foo = 42
>>> release_local(loc)
>>> hasattr(loc, 'foo')
False

With this function one can release Local objects as well as LocalStack 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.

New in version 0.6.1.

class jobcontrol.utils.local.Local[source]
class jobcontrol.utils.local.LocalStack[source]

This class works similar to a Local but keeps a stack of objects instead. This is best explained with an example:

>>> ls = LocalStack()
>>> ls.push(42)
>>> ls.top
42
>>> ls.push(23)
>>> ls.top
23
>>> ls.pop()
23
>>> ls.top
42

They can be force released by using a LocalManager or with the release_local() 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).

By calling the stack without arguments it returns a proxy that resolves to the topmost item on the stack.

New in version 0.6.1.

push(obj)[source]

Pushes a new item to the stack

pop()[source]

Removes the topmost item from the stack, will return the old value or None if the stack was already empty.

top[source]

The topmost item on the stack. If the stack is empty, None is returned.

class jobcontrol.utils.local.LocalProxy(local, name=None)[source]

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.

Example usage:

from werkzeug.local import Local
l = Local()

# these are proxies
request = l('request')
user = l('user')


from werkzeug.local import LocalStack
_response_local = LocalStack()

# this is a proxy
response = _response_local()

Whenever something is bound to l.user / l.request the proxy objects will forward all operations. If no object is bound a RuntimeError will be raised.

To create proxies to Local or LocalStack 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 LocalProxy constructor:

session = LocalProxy(lambda: get_current_request().session)

Changed in version 0.6.1: The class can be instanciated with a callable as well now.

Table Of Contents

Previous topic

jobcontrol.utils.depgraph

Next topic

jobcontrol.utils.testing

This Page