Improved implentation of iterators in tpool; thanks to Slant
This commit is contained in:
2
AUTHORS
2
AUTHORS
@@ -45,3 +45,5 @@ Thanks To
|
|||||||
* the grugq, for contributing patches, suggestions, and use cases
|
* the grugq, for contributing patches, suggestions, and use cases
|
||||||
* Ralf Schmitt, for wsgi/webob incompatibility bug report and suggested fix
|
* Ralf Schmitt, for wsgi/webob incompatibility bug report and suggested fix
|
||||||
* Benoit Chesneau, bug report on green.os and patch to fix it
|
* Benoit Chesneau, bug report on green.os and patch to fix it
|
||||||
|
* Slant, better iterator implementation in tpool
|
||||||
|
* Ambroff, nice pygtk hub
|
||||||
@@ -184,8 +184,6 @@ class Proxy(object):
|
|||||||
# the following are a buncha methods that the python interpeter
|
# the following are a buncha methods that the python interpeter
|
||||||
# doesn't use getattr to retrieve and therefore have to be defined
|
# doesn't use getattr to retrieve and therefore have to be defined
|
||||||
# explicitly
|
# explicitly
|
||||||
def __iter__(self):
|
|
||||||
return proxy_call(self._autowrap, self._obj.__iter__)
|
|
||||||
def __getitem__(self, key):
|
def __getitem__(self, key):
|
||||||
return proxy_call(self._autowrap, self._obj.__getitem__, key)
|
return proxy_call(self._autowrap, self._obj.__getitem__, key)
|
||||||
def __setitem__(self, key, value):
|
def __setitem__(self, key, value):
|
||||||
@@ -209,6 +207,13 @@ class Proxy(object):
|
|||||||
return len(self._obj)
|
return len(self._obj)
|
||||||
def __nonzero__(self):
|
def __nonzero__(self):
|
||||||
return bool(self._obj)
|
return bool(self._obj)
|
||||||
|
def __iter__(self):
|
||||||
|
if iter(self._obj) == self._obj:
|
||||||
|
return self
|
||||||
|
else:
|
||||||
|
return Proxy(iter(self._obj))
|
||||||
|
def next(self):
|
||||||
|
return proxy_call(self._autowrap, self._obj.next)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -139,6 +139,30 @@ class TestTpool(LimitedTestCase):
|
|||||||
result.append(i)
|
result.append(i)
|
||||||
self.assertEquals(range(10), result)
|
self.assertEquals(range(10), result)
|
||||||
|
|
||||||
|
@skip_with_pyevent
|
||||||
|
def test_wrap_iterator2(self):
|
||||||
|
def foo():
|
||||||
|
import time
|
||||||
|
for x in xrange(10):
|
||||||
|
yield x
|
||||||
|
time.sleep(0.01)
|
||||||
|
|
||||||
|
counter = [0]
|
||||||
|
def tick():
|
||||||
|
for i in xrange(100):
|
||||||
|
counter[0]+=1
|
||||||
|
eventlet.sleep(0.001)
|
||||||
|
|
||||||
|
gt = eventlet.spawn(tick)
|
||||||
|
previtem = 0
|
||||||
|
for item in tpool.Proxy(foo()):
|
||||||
|
self.assert_(item >= previtem)
|
||||||
|
# make sure the tick happened at least a few times so that we know
|
||||||
|
# that our iterations in foo() were actually tpooled
|
||||||
|
self.assert_(counter[0] > 10)
|
||||||
|
gt.wait()
|
||||||
|
|
||||||
|
|
||||||
@skip_with_pyevent
|
@skip_with_pyevent
|
||||||
def test_raising_exceptions(self):
|
def test_raising_exceptions(self):
|
||||||
prox = tpool.Proxy(re)
|
prox = tpool.Proxy(re)
|
||||||
|
|||||||
Reference in New Issue
Block a user