fcntl --- The fcntl and ioctl system calls

This module performs file control and I/O control on file descriptors. It is an interface to the fcntl() and ioctl() Unix routines. For a complete description of these calls, see fcntl(2) and ioctl(2) Unix manual pages.

All functions in this module take a file descriptor fd as their first argument. This can be an integer file descriptor, such as returned by sys.stdin.fileno(), or an io.IOBase object, such as sys.stdin itself, which provides a fileno() that returns a genuine file descriptor.

Modifié dans la version 3.3: Operations in this module used to raise an IOError where they now raise an OSError.

Le module définit les fonctions suivantes :

fcntl.fcntl(fd, cmd, arg=0)

Perform the operation cmd on file descriptor fd (file objects providing a fileno() method are accepted as well). The values used for cmd are operating system dependent, and are available as constants in the fcntl module, using the same names as used in the relevant C header files. The argument arg can either be an integer value, or a bytes object. With an integer value, the return value of this function is the integer return value of the C fcntl() call. When the argument is bytes it represents a binary structure, e.g. created by struct.pack(). The binary data is copied to a buffer whose address is passed to the C fcntl() call. The return value after a successful call is the contents of the buffer, converted to a bytes object. The length of the returned object will be the same as the length of the arg argument. This is limited to 1024 bytes. If the information returned in the buffer by the operating system is larger than 1024 bytes, this is most likely to result in a segmentation violation or a more subtle data corruption.

If the fcntl() fails, an OSError is raised.

fcntl.ioctl(fd, request, arg=0, mutate_flag=True)


The request parameter is limited to values that can fit in 32-bits. Additional constants of interest for use as the request argument can be found in the termios module, under the same names as used in the relevant C header files.

The parameter arg can be one of an integer, an object supporting the read-only buffer interface (like bytes) or an object supporting the read-write buffer interface (like bytearray).



If it is false, the buffer's mutability is ignored and behaviour is as for a read-only buffer, except that the 1024 byte limit mentioned above is avoided -- so long as the buffer you pass is at least as long as what the operating system wants to put there, things should work.


If the ioctl() fails, an OSError exception is raised.

Un exemple :

>>> os.getpgrp()
>>> buf
array('h', [13341])
fcntl.flock(fd, operation)


If the flock() fails, an OSError exception is raised.

fcntl.lockf(fd, cmd, len=0, start=0, whence=0)

This is essentially a wrapper around the fcntl() locking calls. fd is the file descriptor of the file to lock or unlock, and cmd is one of the following values:

  • LOCK_UN -- unlock

  • LOCK_SH -- acquire a shared lock

  • LOCK_EX -- acquire an exclusive lock



The default for start is 0, which means to start at the beginning of the file. The default for len is 0 which means to lock to the end of the file. The default for whence is also 0.

Examples (all on a SVR4 compliant system):

import struct, fcntl, os

f = open(...)
rv = fcntl.fcntl(f, fcntl.F_SETFL, os.O_NDELAY)

rv = fcntl.fcntl(f, fcntl.F_SETLKW, lockdata)

Note that in the first example the return value variable rv will hold an integer value; in the second example it will hold a bytes object. The structure lay-out for the lockdata variable is system dependent --- therefore using the flock() call may be better.

Voir aussi

Module os
