1
# -*- test-case-name: twisted.test.test_process.ProcessTestCase.testStdio -*-
6
from zope.interface import implements
8
from twisted.internet.interfaces import IHalfCloseableProtocol, ITransport, IAddress
9
from twisted.internet.interfaces import IConsumer, IPushProducer
11
from twisted.internet import _pollingfile, main
13
class Win32PipeAddress(object):
16
class StandardIO(_pollingfile._PollingTimer):
18
implements(ITransport,
25
def __init__(self, proto):
27
Start talking to standard IO with the given protocol.
29
Also, put it stdin/stdout/stderr into binary mode.
31
from twisted.internet import reactor
33
for stdfd in range(0, 1, 2):
34
msvcrt.setmode(stdfd, os.O_BINARY)
36
_pollingfile._PollingTimer.__init__(self, reactor)
39
hstdin = win32api.GetStdHandle(win32api.STD_INPUT_HANDLE)
40
hstdout = win32api.GetStdHandle(win32api.STD_OUTPUT_HANDLE)
42
self.stdin = _pollingfile._PollableReadPipe(
43
hstdin, self.dataReceived, self.readConnectionLost)
45
self.stdout = _pollingfile._PollableWritePipe(
46
hstdout, self.writeConnectionLost)
48
self._addPollableResource(self.stdin)
49
self._addPollableResource(self.stdout)
51
self.proto.makeConnection(self)
53
def dataReceived(self, data):
54
self.proto.dataReceived(data)
56
def readConnectionLost(self):
57
if IHalfCloseableProtocol.providedBy(self.proto):
58
self.proto.readConnectionLost()
61
def writeConnectionLost(self):
62
if IHalfCloseableProtocol.providedBy(self.proto):
63
self.proto.writeConnectionLost()
68
def checkConnLost(self):
70
if self.connsLost >= 2:
71
self.disconnecting = True
72
self.disconnected = True
73
self.proto.connectionLost(main.CONNECTION_DONE)
77
def write(self, data):
78
self.stdout.write(data)
80
def writeSequence(self, seq):
81
self.stdout.write(''.join(seq))
83
def loseConnection(self):
84
self.disconnecting = True
89
return Win32PipeAddress()
92
return Win32PipeAddress()
96
def registerProducer(self, producer, streaming):
97
return self.stdout.registerProducer(producer, streaming)
99
def unregisterProducer(self):
100
return self.stdout.unregisterProducer()
106
def stopProducing(self):
107
self.stdin.stopProducing()
111
def pauseProducing(self):
112
self.stdin.pauseProducing()
114
def resumeProducing(self):
115
self.stdin.resumeProducing()