Add context manager to GreenPipe.

This commit is contained in:
scott
2010-03-04 17:06:23 -06:00
parent 9a26c2c6d2
commit f52381655b
2 changed files with 26 additions and 0 deletions

View File

@@ -461,6 +461,12 @@ class GreenPipe(object):
def __iter__(self):
return self.xreadlines()
def __enter__(self):
return self
def __exit__(self, *exc_info):
self.close()
def xreadlines(self, size=None):
if size is None:
while True:

View File

@@ -483,6 +483,26 @@ class TestGreenIo(LimitedTestCase):
gt.wait()
def test_pipe_context(self):
# ensure using a pipe as a context actually closes it.
r, w = os.pipe()
r = os.fdopen(r)
w = os.fdopen(w, 'w')
r = greenio.GreenPipe(r)
w = greenio.GreenPipe(w)
with r:
pass
assert r.closed and not w.closed
with w as f:
assert f == w
assert r.closed and w.closed
class TestGreenIoLong(LimitedTestCase):
TEST_TIMEOUT=10 # the test here might take a while depending on the OS