"""Utility functions for dealing with POSIX file descriptors.
API Stability: stable
Maintainer: U{Itamar Shtull-Trauring<mailto:twisted@itamarst.org>}
"""
import sys
import os
import errno
import fcntl
if (sys.hexversion >> 16) >= 0x202:
FCNTL = fcntl
else:
import FCNTL
from main import CONNECTION_LOST
def setNonBlocking(fd):
"""Make a fd non-blocking."""
flags = fcntl.fcntl(fd, FCNTL.F_GETFL)
flags = flags | os.O_NONBLOCK
fcntl.fcntl(fd, FCNTL.F_SETFL, flags)
def readFromFD(fd, callback):
"""Read from fd, calling callback with resulting data.
Returns same thing FileDescriptor.doRead would.
"""
try:
output = os.read(fd, 8192)
except IOError, ioe:
if ioe.args[0] == errno.EAGAIN:
return
else:
return CONNECTION_LOST
if not output:
return CONNECTION_LOST
callback(output)
__all__ = ["setNonBlocking", "readFromFD"]