8
7
Utility functions for dealing with POSIX file descriptors.
15
if (sys.hexversion >> 16) >= 0x202:
21
18
from twisted.internet.main import CONNECTION_LOST, CONNECTION_DONE
19
from twisted.python.runtime import platformType
24
21
def setNonBlocking(fd):
26
23
Make a file descriptor non-blocking.
28
flags = fcntl.fcntl(fd, FCNTL.F_GETFL)
25
flags = fcntl.fcntl(fd, fcntl.F_GETFL)
29
26
flags = flags | os.O_NONBLOCK
30
fcntl.fcntl(fd, FCNTL.F_SETFL, flags)
27
fcntl.fcntl(fd, fcntl.F_SETFL, flags)
33
30
def setBlocking(fd):
35
32
Make a file descriptor blocking.
37
flags = fcntl.fcntl(fd, FCNTL.F_GETFL)
34
flags = fcntl.fcntl(fd, fcntl.F_GETFL)
38
35
flags = flags & ~os.O_NONBLOCK
39
fcntl.fcntl(fd, FCNTL.F_SETFL, flags)
36
fcntl.fcntl(fd, fcntl.F_SETFL, flags)
40
# fcntl isn't available on Windows. By default, handles aren't
41
# inherited on Windows, so we can do nothing here.
42
_setCloseOnExec = _unsetCloseOnExec = lambda fd: None
44
def _setCloseOnExec(fd):
46
Make a file descriptor close-on-exec.
48
flags = fcntl.fcntl(fd, fcntl.F_GETFD)
49
flags = flags | fcntl.FD_CLOEXEC
50
fcntl.fcntl(fd, fcntl.F_SETFD, flags)
53
def _unsetCloseOnExec(fd):
55
Make a file descriptor close-on-exec.
57
flags = fcntl.fcntl(fd, fcntl.F_GETFD)
58
flags = flags & ~fcntl.FD_CLOEXEC
59
fcntl.fcntl(fd, fcntl.F_SETFD, flags)
42
62
def readFromFD(fd, callback):
44
64
Read from file descriptor, calling callback with resulting data.
46
Returns same thing FileDescriptor.doRead would.
66
If successful, call 'callback' with a single argument: the
69
Returns same thing FileDescriptor.doRead would: CONNECTION_LOST,
70
CONNECTION_DONE, or None.
49
73
@param fd: non-blocking file descriptor to be read from.