Exemplo n.º 1
0
def test_local_release():
    """Locals work without manager"""
    loc = Local()
    loc.foo = 42
    release_local(loc)
    assert not hasattr(loc, "foo")

    ls = LocalStack()
    ls.push(42)
    release_local(ls)
    assert ls.top is None
Exemplo n.º 2
0
def test_local_release():
    """Locals work without manager"""
    loc = Local()
    loc.foo = 42
    release_local(loc)
    assert not hasattr(loc, 'foo')

    ls = LocalStack()
    ls.push(42)
    release_local(ls)
    assert ls.top is None
Exemplo n.º 3
0
def test_custom_idents():
    """Local manager supports custom ident functions"""
    ident = 0
    local = Local()
    stack = LocalStack()
    mgr = LocalManager([local, stack], ident_func=lambda: ident)

    local.foo = 42
    stack.push({"foo": 42})
    ident = 1
    local.foo = 23
    stack.push({"foo": 23})
    ident = 0
    assert local.foo == 42
    assert stack.top["foo"] == 42
    stack.pop()
    assert stack.top is None
    ident = 1
    assert local.foo == 23
    assert stack.top["foo"] == 23
    stack.pop()
    assert stack.top is None
Exemplo n.º 4
0
        current context for the duration of the `with` block.

        Example usage::

            with app.request_context(environ):
                do_something_with(request)

        :params environ: a WSGI environment
        """
        return _RequestContext(self, environ)

    def test_request_context(self, *args, **kwargs):
        """Creates a WSGI environment from the given values (see
        :func:`werkzeug.create_environ` for more information, this
        function accepts the same arguments).
        """
        return self.request_context(create_environ(*args, **kwargs))

    def __call__(self, environ, start_response):
        """Shortcut for :attr:`wsgi_app`"""
        return self.wsgi_app(environ, start_response)


# context locals
_request_ctx_stack = LocalStack()
# 看起来同一时间只处理一个请求,
current_app = LocalProxy(lambda: _request_ctx_stack.top.app)
request = LocalProxy(lambda: _request_ctx_stack.top.request)
session = LocalProxy(lambda: _request_ctx_stack.top.session)
g = LocalProxy(lambda: _request_ctx_stack.top.g)
Exemplo n.º 5
0
def test_local_stack():
    """Test the LocalStack"""
    ident = get_ident()

    ls = LocalStack()
    assert ident not in ls._local.__storage__
    assert ls.top is None
    ls.push(42)
    assert ident in ls._local.__storage__
    assert ls.top == 42
    ls.push(23)
    assert ls.top == 23
    ls.pop()
    assert ls.top == 42
    ls.pop()
    assert ls.top is None
    assert ls.pop() is None
    assert ls.pop() is None

    proxy = ls()
    ls.push([1, 2])
    assert proxy == [1, 2]
    ls.push((1, 2))
    assert proxy == (1, 2)
    ls.pop()
    ls.pop()
    assert repr(proxy) == "<LocalProxy unbound>"

    assert ident not in ls._local.__storage__
Exemplo n.º 6
0
def test_local_stack():
    """Test the LocalStack"""
    ident = get_ident()

    ls = LocalStack()
    assert ident not in ls._local.__storage__
    assert ls.top is None
    ls.push(42)
    assert ident in ls._local.__storage__
    assert ls.top == 42
    ls.push(23)
    assert ls.top == 23
    ls.pop()
    assert ls.top == 42
    ls.pop()
    assert ls.top is None
    assert ls.pop() is None
    assert ls.pop() is None

    proxy = ls()
    ls.push([1, 2])
    assert proxy == [1, 2]
    ls.push((1, 2))
    assert proxy == (1, 2)
    ls.pop()
    ls.pop()
    assert repr(proxy) == '<LocalProxy unbound>'

    assert ident not in ls._local.__storage__
Exemplo n.º 7
0
import re
from datetime import datetime as _datetime, tzinfo as _tzinfo, timedelta
from tzlocal import get_localzone

import time as _time
import pytz as _pytz
import math as _math
import datetime as _datetime_mod
import re as _re
from pytz import ZERO

from werkzeug import LocalStack
from sqlalchemy import processors

_utc = _pytz.utc
_default_tz_stack = LocalStack()
_default_tz_stack.push(get_localzone())
_default_tz = _default_tz_stack()

def patch():
    _datetime_mod.datetime = datetime
    DATETIME_RE = re.compile(
            "(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)(?:\.(\d+))?")

    # (only) sqlite uses str_to_datetime to process its datetime result
    processors.str_to_datetime = processors. \
            str_to_datetime_processor_factory(DATETIME_RE, datetime)

def push_default_timezone(new_tz):
    """Sets the default time zone used by the objects
       contained in this module. new_tz may be either