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))
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")
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]
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)
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"
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
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]
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__)
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
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__)
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
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
def __call__(self, proxy): """Create a proxy for a name.""" return local.LocalProxy(self, proxy)