[py-svn] r10583 - in py/branch/py-collect/execnet: . testing
hpk at codespeak.net
hpk at codespeak.net
Wed Apr 13 17:52:37 CEST 2005
Author: hpk
Date: Wed Apr 13 17:52:36 2005
New Revision: 10583
Modified:
py/branch/py-collect/execnet/channel.py
py/branch/py-collect/execnet/gateway.py
py/branch/py-collect/execnet/testing/test_gateway.py
Log:
intermediate approach to redirecting stdout
via a separate remote_redirect() method ...
(soon i'll try another aproach incorporating
redirection to remote_exec directly)
Modified: py/branch/py-collect/execnet/channel.py
==============================================================================
--- py/branch/py-collect/execnet/channel.py (original)
+++ py/branch/py-collect/execnet/channel.py Wed Apr 13 17:52:36 2005
@@ -176,12 +176,3 @@
state = self.channel.isclosed() and 'closed' or 'open'
return '<ChannelFile %d %s>' %(self.channel.id, state)
-def receive2file(channel, f):
- while 1:
- try:
- out = channel.receive()
- except EOFError:
- break
- f.write(out)
- f.flush()
-
Modified: py/branch/py-collect/execnet/gateway.py
==============================================================================
--- py/branch/py-collect/execnet/gateway.py (original)
+++ py/branch/py-collect/execnet/gateway.py Wed Apr 13 17:52:36 2005
@@ -5,6 +5,7 @@
import traceback
import atexit
+
# XXX the following line should not be here
g = globals()
if 'Message' not in g:
@@ -229,6 +230,53 @@
self._outgoing.put(Message.CHANNEL_OPEN(channel.id, source))
return channel
+ def remote_redirect(self, stdout):
+ """ return a handle representing a redirection of of remote
+ end's stdout to a local file object. with handle.close()
+ the redirection will be reverted.
+ """
+ handle = RedirectHandle(self, stdout)
+ handle.open()
+ return handle
+
+class RedirectHandle(object):
+ def __init__(self, gateway, stdout):
+ self.gateway = gateway
+ self.stdout = stdout
+
+ def open(self):
+ channel = self.gateway.remote_exec("""
+ import sys
+ out = channel.newchannel()
+ channel.send(out)
+ sys.__dict__.setdefault('_stdoutsubst', []).append(sys.stdout)
+ sys.stdout = out.open('w')
+ """)
+ self.outchannel = channel.receive()
+ self.thread = threading.Thread(target=self.receive2file)
+ self.thread.start()
+
+ def __del__(self):
+ self.close()
+
+ def close(self, timeout=1.0):
+ self.gateway.remote_exec("""
+ import sys
+ channel = sys.stdout.channel
+ sys.stdout = sys._stdoutsubst.pop()
+ channel.close()
+ """)
+ self.outchannel.waitclose(timeout=1.0)
+
+ def receive2file(self):
+ while 1:
+ try:
+ out = self.outchannel.receive()
+ except EOFError:
+ break
+ self.stdout.write(out)
+ self.stdout.flush()
+
def getid(gw, cache={}):
name = gw.__class__.__name__
try:
Modified: py/branch/py-collect/execnet/testing/test_gateway.py
==============================================================================
--- py/branch/py-collect/execnet/testing/test_gateway.py (original)
+++ py/branch/py-collect/execnet/testing/test_gateway.py Wed Apr 13 17:52:36 2005
@@ -129,6 +129,16 @@
''' % (channel.id))
newchan.waitclose(0.3)
+ def test_remote_redirect_stdout(self):
+ out = py.std.StringIO.StringIO()
+ handle = self.gw.remote_redirect(stdout=out)
+ try:
+ self.gw.remote_exec("print 42")
+ finally:
+ handle.close()
+ s = out.getvalue()
+ assert s.strip() == "42"
+
class TestBasicPopenGateway(PopenGatewayTestSetup, BasicRemoteExecution):
#disabled = True
def test_many_popen(self):
More information about the pytest-commit
mailing list