tpool.Proxy now wraps functions. Improved impolementations of equality and iteration in tpool as well.
This commit is contained in:
@@ -196,11 +196,16 @@ class Proxy(object):
|
|||||||
return proxy_call(self._autowrap, self._obj.__deepcopy__, memo)
|
return proxy_call(self._autowrap, self._obj.__deepcopy__, memo)
|
||||||
def __copy__(self, memo=None):
|
def __copy__(self, memo=None):
|
||||||
return proxy_call(self._autowrap, self._obj.__copy__, memo)
|
return proxy_call(self._autowrap, self._obj.__copy__, memo)
|
||||||
|
def __call__(self, *a, **kw):
|
||||||
|
if '__call__' in self._autowrap_names:
|
||||||
|
return Proxy(proxy_call(self._autowrap, self._obj, *a, **kw))
|
||||||
|
else:
|
||||||
|
return proxy_call(self._autowrap, self._obj, *a, **kw)
|
||||||
# these don't go through a proxy call, because they're likely to
|
# these don't go through a proxy call, because they're likely to
|
||||||
# be called often, and are unlikely to be implemented on the
|
# be called often, and are unlikely to be implemented on the
|
||||||
# wrapped object in such a way that they would block
|
# wrapped object in such a way that they would block
|
||||||
def __eq__(self, rhs):
|
def __eq__(self, rhs):
|
||||||
return self._obj.__eq__(rhs)
|
return self._obj == rhs
|
||||||
def __hash__(self):
|
def __hash__(self):
|
||||||
return self._obj.__hash__()
|
return self._obj.__hash__()
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
@@ -212,10 +217,11 @@ class Proxy(object):
|
|||||||
def __nonzero__(self):
|
def __nonzero__(self):
|
||||||
return bool(self._obj)
|
return bool(self._obj)
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
if iter(self._obj) == self._obj:
|
it = iter(self._obj)
|
||||||
|
if it == self._obj:
|
||||||
return self
|
return self
|
||||||
else:
|
else:
|
||||||
return Proxy(iter(self._obj))
|
return Proxy(it)
|
||||||
def next(self):
|
def next(self):
|
||||||
return proxy_call(self._autowrap, self._obj.next)
|
return proxy_call(self._autowrap, self._obj.next)
|
||||||
|
|
||||||
|
@@ -95,6 +95,11 @@ class TestTpool(LimitedTestCase):
|
|||||||
exp3 = prox.compile('/')
|
exp3 = prox.compile('/')
|
||||||
self.assert_(exp1 != exp3)
|
self.assert_(exp1 != exp3)
|
||||||
|
|
||||||
|
@skip_with_pyevent
|
||||||
|
def test_wrap_ints(self):
|
||||||
|
p = tpool.Proxy(4)
|
||||||
|
self.assert_(p == 4)
|
||||||
|
|
||||||
@skip_with_pyevent
|
@skip_with_pyevent
|
||||||
def test_wrap_hash(self):
|
def test_wrap_hash(self):
|
||||||
prox1 = tpool.Proxy(''+'A')
|
prox1 = tpool.Proxy(''+'A')
|
||||||
@@ -243,6 +248,28 @@ class TestTpool(LimitedTestCase):
|
|||||||
# violating the abstraction to check that we didn't double-wrap
|
# violating the abstraction to check that we didn't double-wrap
|
||||||
self.assert_(not isinstance(x._obj, tpool.Proxy))
|
self.assert_(not isinstance(x._obj, tpool.Proxy))
|
||||||
|
|
||||||
|
@skip_with_pyevent
|
||||||
|
def test_callable(self):
|
||||||
|
def wrapped(arg):
|
||||||
|
return arg
|
||||||
|
x = tpool.Proxy(wrapped)
|
||||||
|
self.assertEquals(4, x(4))
|
||||||
|
# verify that it wraps return values if specified
|
||||||
|
x = tpool.Proxy(wrapped, autowrap_names=('__call__',))
|
||||||
|
self.assert_(isinstance(x(4), tpool.Proxy))
|
||||||
|
self.assertEquals("4", str(x(4)))
|
||||||
|
|
||||||
|
@skip_with_pyevent
|
||||||
|
def test_callable_iterator(self):
|
||||||
|
def wrapped(arg):
|
||||||
|
yield arg
|
||||||
|
yield arg
|
||||||
|
yield arg
|
||||||
|
|
||||||
|
x = tpool.Proxy(wrapped, autowrap_names=('__call__',))
|
||||||
|
for r in x(3):
|
||||||
|
self.assertEquals(3, r)
|
||||||
|
|
||||||
class TpoolLongTests(LimitedTestCase):
|
class TpoolLongTests(LimitedTestCase):
|
||||||
TEST_TIMEOUT=60
|
TEST_TIMEOUT=60
|
||||||
@skip_with_pyevent
|
@skip_with_pyevent
|
||||||
@@ -294,5 +321,6 @@ from eventlet.tpool import execute
|
|||||||
iterations, tpool_overhead, best_normal, best_tpool)
|
iterations, tpool_overhead, best_normal, best_tpool)
|
||||||
tpool.killall()
|
tpool.killall()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
Reference in New Issue
Block a user