sources for inputoutput.py [rev. 38799]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
"""
  InputOutput Classes used for connecting gateways
  across process or computer barriers.
"""
import socket, os, sys
class SocketIO:
    server_stmt = """
io = SocketIO(clientsock)
import sys
#try:
#    sys.stdout = sys.stderr = open('/tmp/execnet-socket-debug.log', 'a', 0)
#except (IOError, OSError):
#    sys.stdout = sys.stderr = open('/dev/null', 'w')
#print '='*60
"""
    error = (socket.error, EOFError)
    def __init__(self, sock):
        self.sock = sock
        try:
            sock.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 1)
            sock.setsockopt(socket.SOL_IP, socket.IP_TOS, 0x10)  # IPTOS_LOWDELAY
        except socket.error, e:
            print "WARNING: Cannot set socket option:", str(e)
        self.readable = self.writeable = True
    def read(self, numbytes):
        "Read exactly 'bytes' bytes from the socket."
        buf = ""
        while len(buf) < numbytes:
            t = self.sock.recv(numbytes - len(buf))
            #print 'recv -->', len(t)
            if not t:
                raise EOFError
            buf += t
        return buf
    def write(self, data):
        """write out all bytes to the socket. """
        self.sock.sendall(data)
    def close_read(self):
        if self.readable:
            self.sock.shutdown(0)
            self.readable = None
    def close_write(self):
        if self.writeable:
            self.sock.shutdown(1)
            self.writeable = None
class Popen2IO:
    server_stmt = """
import sys, StringIO
io = Popen2IO(sys.stdout, sys.stdin)
sys.stdout = sys.stderr = StringIO.StringIO() 
#try:
#    sys.stdout = sys.stderr = open('/tmp/execnet-popen-debug.log', 'a', 0)
#except (IOError, OSError):
#    sys.stdout = sys.stderr = open('/dev/null', 'w')
#print '='*60
"""
    error = (IOError, OSError, EOFError)
    def __init__(self, infile, outfile):
        if sys.platform == 'win32':
            import msvcrt
            msvcrt.setmode(infile.fileno(), os.O_BINARY)
            msvcrt.setmode(outfile.fileno(), os.O_BINARY)
        self.outfile, self.infile = infile, outfile
        self.readable = self.writeable = True
    def read(self, numbytes):
        """Read exactly 'bytes' bytes from the pipe. """
        #import sys
        #print >> sys.stderr, "reading..."
        s = self.infile.read(numbytes)
        #print >> sys.stderr, "read: %r" % s
        if len(s) < numbytes:
            raise EOFError
        return s
    def write(self, data):
        """write out all bytes to the pipe. """
        #import sys
        #print >> sys.stderr, "writing: %r" % data
        self.outfile.write(data)
        self.outfile.flush()
    def close_read(self):
        if self.readable:
            self.infile.close()
            self.readable = None
    def close_write(self):
        if self.writeable:
            self.outfile.close()
            self.writeable = None