예제 #1
0
 def enable_proxy(self):
     """
     Overwrites __stdout__, __stderr__, stdout, and stderr with the proxied
     objects.
     """
     sys.__stdout__ = local.LocalProxy(self._get_stream(sys.__stdout__))
     sys.__stderr__ = local.LocalProxy(self._get_stream(sys.__stderr__))
     sys.stdout = local.LocalProxy(self._get_stream(sys.stdout))
     sys.stderr = local.LocalProxy(self._get_stream(sys.stderr))
예제 #2
0
def test_proxy_doc():
    def example():
        """example doc"""

    assert local.LocalProxy(lambda: example).__doc__ == "example doc"
    # The __doc__ descriptor shouldn't block the LocalProxy's class doc.
    assert local.LocalProxy.__doc__.startswith("A proxy")
예제 #3
0
def test_local_proxies_with_callables():
    foo = 42
    ls = local.LocalProxy(lambda: foo)
    assert ls == 42
    foo = [23]
    ls.append(42)
    assert ls == [23, 42]
    assert foo == [23, 42]
예제 #4
0
def test_proxy_fallback():
    def _raises():
        raise RuntimeError()

    local_proxy = local.LocalProxy(_raises)
    assert repr(local_proxy) == "<LocalProxy unbound>"
    assert isinstance(local_proxy, local.LocalProxy)
    assert not isinstance(local_proxy, Thread)
예제 #5
0
def test_local_proxy_operations_strings():
    foo = "foo"
    ls = local.LocalProxy(lambda: foo)
    assert ls + "bar" == "foobar"
    assert "bar" + ls == "barfoo"
    assert ls * 2 == "foofoo"

    foo = "foo %s"
    assert ls % ("bar", ) == "foo bar"
예제 #6
0
def test_proxy_callable():
    value = 42
    p = local.LocalProxy(lambda: value)
    assert p == 42
    value = [23]
    p.append(42)
    assert p == [23, 42]
    assert value == [23, 42]
    assert p._get_current_object() is value
예제 #7
0
def test_local_proxy():
    foo = []
    ls = local.LocalProxy(lambda: foo)
    ls.append(42)
    ls.append(23)
    ls[1:] = [1, 2, 3]
    assert foo == [42, 1, 2, 3]
    assert repr(foo) == repr(ls)
    assert foo[0] == 42
    foo += [1]
    assert list(foo) == [42, 1, 2, 3, 1]
예제 #8
0
def test_proxy_wrapped():
    class SomeClassWithWrapped:
        __wrapped__ = "wrapped"

    def lookup_func():
        return 42

    proxy = local.LocalProxy(lookup_func)
    assert proxy.__wrapped__ is lookup_func

    partial_lookup_func = partial(lookup_func)
    partial_proxy = local.LocalProxy(partial_lookup_func)
    assert partial_proxy.__wrapped__ == partial_lookup_func

    ns = local.Local()
    ns.foo = SomeClassWithWrapped()
    ns.bar = 42

    assert ns("foo").__wrapped__ == "wrapped"
    pytest.raises(AttributeError, lambda: ns("bar").__wrapped__)
예제 #9
0
def test_deepcopy_on_proxy():
    class Foo(object):
        attr = 42
        def __copy__(self):
            return self
        def __deepcopy__(self, memo):
            return self
    f = Foo()
    p = local.LocalProxy(lambda: f)
    assert p.attr == 42
    assert copy.deepcopy(p) is f
    assert copy.copy(p) is f

    a = []
    p2 = local.LocalProxy(lambda: [a])
    assert copy.copy(p2) == [a]
    assert copy.copy(p2)[0] is a

    assert copy.deepcopy(p2) == [a]
    assert copy.deepcopy(p2)[0] is not a
예제 #10
0
def test_local_proxy_wrapped_attribute():
    class SomeClassWithWrapped(object):
        __wrapped__ = 'wrapped'

    def lookup_func():
        return 42

    partial_lookup_func = partial(lookup_func)

    proxy = local.LocalProxy(lookup_func)
    assert proxy.__wrapped__ is lookup_func

    partial_proxy = local.LocalProxy(partial_lookup_func)
    assert partial_proxy.__wrapped__ == partial_lookup_func

    l = local.Local()
    l.foo = SomeClassWithWrapped()
    l.bar = 42

    assert l('foo').__wrapped__ == 'wrapped'
    pytest.raises(AttributeError, lambda: l('bar').__wrapped__)
예제 #11
0
def test_local_proxy_operations_math():
    foo = 2
    ls = local.LocalProxy(lambda: foo)
    assert ls + 1 == 3
    assert 1 + ls == 3
    assert ls - 1 == 1
    assert 1 - ls == -1
    assert ls * 1 == 2
    assert 1 * ls == 2
    assert ls / 1 == 2
    assert 1.0 / ls == 0.5
    assert ls // 1.0 == 2.0
    assert 1.0 // ls == 0.0
    assert ls % 2 == 0
    assert 2 % ls == 0
예제 #12
0
def test_proxy_local():
    ns = local.Local()
    ns.foo = []
    p = local.LocalProxy(ns, "foo")
    p.append(42)
    p.append(23)
    p[1:] = [1, 2, 3]
    assert p == [42, 1, 2, 3]
    assert p == ns.foo
    ns.foo += [1]
    assert list(p) == [42, 1, 2, 3, 1]
    p_from_local = ns("foo")
    p_from_local.append(2)
    assert p == p_from_local
    assert p._get_current_object() is ns.foo
예제 #13
0
 def __call__(self, proxy):
     """Create a proxy for a name."""
     return local.LocalProxy(self, proxy)