tpool.Proxy now wraps functions. Improved impolementations of equality and iteration in tpool as well.

This commit is contained in:
Ryan Williams
2010-06-21 17:01:13 -07:00
parent 349b99684c
commit c7b47b2f97
2 changed files with 37 additions and 3 deletions

View File

@@ -196,11 +196,16 @@ class Proxy(object):
return proxy_call(self._autowrap, self._obj.__deepcopy__, memo)
def __copy__(self, memo=None):
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
# be called often, and are unlikely to be implemented on the
# wrapped object in such a way that they would block
def __eq__(self, rhs):
return self._obj.__eq__(rhs)
return self._obj == rhs
def __hash__(self):
return self._obj.__hash__()
def __repr__(self):
@@ -212,10 +217,11 @@ class Proxy(object):
def __nonzero__(self):
return bool(self._obj)
def __iter__(self):
if iter(self._obj) == self._obj:
it = iter(self._obj)
if it == self._obj:
return self
else:
return Proxy(iter(self._obj))
return Proxy(it)
def next(self):
return proxy_call(self._autowrap, self._obj.next)

View File

@@ -95,6 +95,11 @@ class TestTpool(LimitedTestCase):
exp3 = prox.compile('/')
self.assert_(exp1 != exp3)
@skip_with_pyevent
def test_wrap_ints(self):
p = tpool.Proxy(4)
self.assert_(p == 4)
@skip_with_pyevent
def test_wrap_hash(self):
prox1 = tpool.Proxy(''+'A')
@@ -243,6 +248,28 @@ class TestTpool(LimitedTestCase):
# violating the abstraction to check that we didn't double-wrap
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):
TEST_TIMEOUT=60
@skip_with_pyevent
@@ -294,5 +321,6 @@ from eventlet.tpool import execute
iterations, tpool_overhead, best_normal, best_tpool)
tpool.killall()
if __name__ == '__main__':
main()