Mimicking basic fcntl or SetHandleInformation call in .Net

Posted by Tristan on Stack Overflow See other posts from Stack Overflow or by Tristan
Published on 2010-06-09T02:53:33Z Indexed on 2010/06/09 3:02 UTC
Read the original article Hit count: 421

Filed under:
|
|
|

Tornado enables win32 support by faking Python's fcntl function using SetHandleInformation, which is available via ctypes on Windows. After some other small fixes, this actually works using IronPython on Windows as well (sadly, IronPython is five times slower).

I'd like to get Tornado working on any CLI platform, such using Mono on OSX or Linux. Is there a managed, cross-platform, .Net approach that can fake fcntl?

Here's the win32 code from Tornado:

SetHandleInformation = ctypes.windll.kernel32.SetHandleInformation
SetHandleInformation.argtypes = (ctypes.wintypes.HANDLE, ctypes.wintypes.DWORD, ctypes.wintypes.DWORD)
SetHandleInformation.restype = ctypes.wintypes.BOOL

HANDLE_FLAG_INHERIT = 0x00000001

F_GETFD = 1
F_SETFD = 2
F_GETFL = 3
F_SETFL = 4

FD_CLOEXEC = 1

os.O_NONBLOCK = 2048

FIONBIO = 126

def fcntl(fd, op, arg=0):
    if op == F_GETFD or op == F_GETFL:
        return 0
    elif op == F_SETFD:
        # Check that the flag is CLOEXEC and translate
        if arg == FD_CLOEXEC:
            fd = int(fd)
            success = SetHandleInformation(fd, HANDLE_FLAG_INHERIT, arg)
            if not success:
                raise ctypes.GetLastError()
        else:
            raise ValueError("Unsupported arg")
    else:
        raise ValueError("Unsupported op")

© Stack Overflow or respective owner

Related posts about .NET

Related posts about python