Add context manager to GreenPipe.
This commit is contained in:
@@ -461,6 +461,12 @@ class GreenPipe(object):
|
|||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
return self.xreadlines()
|
return self.xreadlines()
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __exit__(self, *exc_info):
|
||||||
|
self.close()
|
||||||
|
|
||||||
def xreadlines(self, size=None):
|
def xreadlines(self, size=None):
|
||||||
if size is None:
|
if size is None:
|
||||||
while True:
|
while True:
|
||||||
|
@@ -483,6 +483,26 @@ class TestGreenIo(LimitedTestCase):
|
|||||||
|
|
||||||
gt.wait()
|
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):
|
class TestGreenIoLong(LimitedTestCase):
|
||||||
TEST_TIMEOUT=10 # the test here might take a while depending on the OS
|
TEST_TIMEOUT=10 # the test here might take a while depending on the OS
|
||||||
|
Reference in New Issue
Block a user