Skip to content

gh-132742: Update documentation for the fcntl module #132765

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 23, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 46 additions & 29 deletions Doc/library/fcntl.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,65 +82,82 @@ descriptor.
The module defines the following functions:


.. function:: fcntl(fd, cmd, arg=0)
.. function:: fcntl(fd, cmd, arg=0, /)

Perform the operation *cmd* on file descriptor *fd* (file objects providing
a :meth:`~io.IOBase.fileno` method are accepted as well). The values used
for *cmd* are operating system dependent, and are available as constants
in the :mod:`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
:class:`bytes` object. With an integer value, the return value of this
function is the integer return value of the C :c:func:`fcntl` call. When
the argument is bytes it represents a binary structure, e.g. created by
:func:`struct.pack`. The binary data is copied to a buffer whose address is
header files. The argument *arg* can either be an integer value, a
:class:`bytes` object, or a string.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it accepts any readable buffer

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may accept other buffers without bf_releasebuffer, but bytes is the only standard type. Read-only memoryview or mmap are not supported.

The type and size of *arg* must match the type and size of
the argument of the operation as specified in the relevant C documentation.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Link?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Depending on platform. Various manpages on Posix systems, some Microsoft links on Windows.


When *arg* is an integer, the function returns the integer
return value of the C :c:func:`fcntl` call.

When the argument is bytes, it represents a binary structure,
for example, created by :func:`struct.pack`.
A string value is encoded to binary using the UTF-8 encoding.
The binary data is copied to a buffer whose address is
passed to the C :c:func:`fcntl` call. The return value after a successful
call is the contents of the buffer, converted to a :class:`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.
*arg* argument. This is limited to 1024 bytes.

If the :c:func:`fcntl` call fails, an :exc:`OSError` is raised.

.. note::
If the type or the size of *arg* does not match the type or size
of the argument of the operation (for example, if an integer is
passed when a pointer is expected, or 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.

.. audit-event:: fcntl.fcntl fd,cmd,arg fcntl.fcntl


.. function:: ioctl(fd, request, arg=0, mutate_flag=True)
.. function:: ioctl(fd, request, arg=0, mutate_flag=True, /)

This function is identical to the :func:`~fcntl.fcntl` function, except
that the argument handling is even more complicated.

The *request* parameter is limited to values that can fit in 32-bits.
The *request* parameter is limited to values that can fit in 32-bits
or 64-bits, depending on the platform.
Additional constants of interest for use as the *request* argument can be
found in the :mod:`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 :class:`bytes`) or an object supporting
the read-write buffer interface (like :class:`bytearray`).
The parameter *arg* can be an integer, a :term:`bytes-like object`,
or a string.
The type and size of *arg* must match the type and size of
the argument of the operation as specified in the relevant C documentation.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Link?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking the same thing, but since most fcntl values are platform-dependent there probably isn't a good documentation link to use here.


In all but the last case, behaviour is as for the :func:`~fcntl.fcntl`
If *arg* does not support the read-write buffer interface or
the *mutate_flag* is false, behavior is as for the :func:`~fcntl.fcntl`
function.

If a mutable buffer is passed, then the behaviour is determined by the value of
the *mutate_flag* parameter.

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 --
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was incorrect, the 1024 byte limit is not avoided if mutate_flag is false.

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 *mutate_flag* is true (the default), then the buffer is (in effect) passed
to the underlying :func:`ioctl` system call, the latter's return code is
If *arg* supports the read-write buffer interface (like :class:`bytearray`)
and *mutate_flag* is true (the default), then the buffer is (in effect) passed
to the underlying :c:func:`!ioctl` system call, the latter's return code is
passed back to the calling Python, and the buffer's new contents reflect the
action of the :func:`ioctl`. This is a slight simplification, because if the
action of the :c:func:`ioctl`. This is a slight simplification, because if the
supplied buffer is less than 1024 bytes long it is first copied into a static
buffer 1024 bytes long which is then passed to :func:`ioctl` and copied back
into the supplied buffer.

If the :c:func:`ioctl` call fails, an :exc:`OSError` exception is raised.

.. note::
If the type or size of *arg* does not match the type or size
of the operation's argument (for example, if an integer is
passed when a pointer is expected, or the information returned in
the buffer by the operating system is larger than 1024 bytes,
or the size of the mutable bytes-like object is too small),
this is most likely to result in a segmentation violation or
a more subtle data corruption.
Comment on lines +158 to +159
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
this is most likely to result in a segmentation violation or
a more subtle data corruption.
it is most likely to result in a segmentation violation or
a more subtle data corruption.

Continues on from previous suggestion


An example::

>>> import array, fcntl, struct, termios, os
Expand All @@ -157,7 +174,7 @@ The module defines the following functions:
.. audit-event:: fcntl.ioctl fd,request,arg fcntl.ioctl


.. function:: flock(fd, operation)
.. function:: flock(fd, operation, /)

Perform the lock operation *operation* on file descriptor *fd* (file objects providing
a :meth:`~io.IOBase.fileno` method are accepted as well). See the Unix manual
Expand All @@ -169,7 +186,7 @@ The module defines the following functions:
.. audit-event:: fcntl.flock fd,operation fcntl.flock


.. function:: lockf(fd, cmd, len=0, start=0, whence=0)
.. function:: lockf(fd, cmd, len=0, start=0, whence=0, /)

This is essentially a wrapper around the :func:`~fcntl.fcntl` locking calls.
*fd* is the file descriptor (file objects providing a :meth:`~io.IOBase.fileno`
Expand Down
Loading