Skip to content

Commit 38b5e50

Browse files
serhiy-storchakaaisk
authored andcommitted
pythongh-114321: Expose more constants in the fcntl module (pythonGH-114322)
1 parent ef2ca79 commit 38b5e50

File tree

3 files changed

+86
-10
lines changed

3 files changed

+86
-10
lines changed

Doc/library/fcntl.rst

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,33 +31,54 @@ descriptor.
3131
raise an :exc:`OSError`.
3232

3333
.. versionchanged:: 3.8
34-
The fcntl module now contains ``F_ADD_SEALS``, ``F_GET_SEALS``, and
34+
The :mod:`!fcntl` module now contains ``F_ADD_SEALS``, ``F_GET_SEALS``, and
3535
``F_SEAL_*`` constants for sealing of :func:`os.memfd_create` file
3636
descriptors.
3737

3838
.. versionchanged:: 3.9
39-
On macOS, the fcntl module exposes the ``F_GETPATH`` constant, which obtains
40-
the path of a file from a file descriptor.
41-
On Linux(>=3.15), the fcntl module exposes the ``F_OFD_GETLK``, ``F_OFD_SETLK``
42-
and ``F_OFD_SETLKW`` constants, which are used when working with open file
43-
description locks.
39+
On macOS, the :mod:`!fcntl` module exposes the ``F_GETPATH`` constant,
40+
which obtains the path of a file from a file descriptor.
41+
On Linux(>=3.15), the :mod:`!fcntl` module exposes the ``F_OFD_GETLK``,
42+
``F_OFD_SETLK`` and ``F_OFD_SETLKW`` constants, which are used when working
43+
with open file description locks.
4444

4545
.. versionchanged:: 3.10
46-
On Linux >= 2.6.11, the fcntl module exposes the ``F_GETPIPE_SZ`` and
46+
On Linux >= 2.6.11, the :mod:`!fcntl` module exposes the ``F_GETPIPE_SZ`` and
4747
``F_SETPIPE_SZ`` constants, which allow to check and modify a pipe's size
4848
respectively.
4949

5050
.. versionchanged:: 3.11
51-
On FreeBSD, the fcntl module exposes the ``F_DUP2FD`` and ``F_DUP2FD_CLOEXEC``
52-
constants, which allow to duplicate a file descriptor, the latter setting
53-
``FD_CLOEXEC`` flag in addition.
51+
On FreeBSD, the :mod:`!fcntl` module exposes the ``F_DUP2FD`` and
52+
``F_DUP2FD_CLOEXEC`` constants, which allow to duplicate a file descriptor,
53+
the latter setting ``FD_CLOEXEC`` flag in addition.
5454

5555
.. versionchanged:: 3.12
5656
On Linux >= 4.5, the :mod:`fcntl` module exposes the ``FICLONE`` and
5757
``FICLONERANGE`` constants, which allow to share some data of one file with
5858
another file by reflinking on some filesystems (e.g., btrfs, OCFS2, and
5959
XFS). This behavior is commonly referred to as "copy-on-write".
6060

61+
.. versionchanged:: 3.13
62+
On Linux >= 2.6.32, the :mod:`!fcntl` module exposes the
63+
``F_GETOWN_EX``, ``F_SETOWN_EX``, ``F_OWNER_TID``, ``F_OWNER_PID``, ``F_OWNER_PGRP`` constants, which allow to direct I/O availability signals
64+
to a specific thread, process, or process group.
65+
On Linux >= 4.13, the :mod:`!fcntl` module exposes the
66+
``F_GET_RW_HINT``, ``F_SET_RW_HINT``, ``F_GET_FILE_RW_HINT``,
67+
``F_SET_FILE_RW_HINT``, and ``RWH_WRITE_LIFE_*`` constants, which allow
68+
to inform the kernel about the relative expected lifetime of writes on
69+
a given inode or via a particular open file description.
70+
On Linux >= 5.1 and NetBSD, the :mod:`!fcntl` module exposes the
71+
``F_SEAL_FUTURE_WRITE`` constant for use with ``F_ADD_SEALS`` and
72+
``F_GET_SEALS`` operations.
73+
On FreeBSD, the :mod:`!fcntl` module exposes the ``F_READAHEAD``, ``F_ISUNIONSTACK``, and ``F_KINFO`` constants.
74+
On macOS and FreeBSD, the :mod:`!fcntl` module exposes the ``F_RDAHEAD``
75+
constant.
76+
On NetBSD and AIX, the :mod:`!fcntl` module exposes the ``F_CLOSEM``
77+
constant.
78+
On NetBSD, the :mod:`!fcntl` module exposes the ``F_MAXFD`` constant.
79+
On macOS and NetBSD, the :mod:`!fcntl` module exposes the ``F_GETNOSIGPIPE``
80+
and ``F_SETNOSIGPIPE`` constant.
81+
6182
The module defines the following functions:
6283

6384

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Expose more platform specific constants in the :mod:`fcntl` module on Linux,
2+
macOS, FreeBSD and NetBSD.

Modules/fcntlmodule.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,30 @@ all_ins(PyObject* m)
583583
#ifdef FICLONERANGE
584584
if (PyModule_AddIntMacro(m, FICLONERANGE)) return -1;
585585
#endif
586+
#ifdef F_GETOWN_EX
587+
// since Linux 2.6.32
588+
if (PyModule_AddIntMacro(m, F_GETOWN_EX)) return -1;
589+
if (PyModule_AddIntMacro(m, F_SETOWN_EX)) return -1;
590+
if (PyModule_AddIntMacro(m, F_OWNER_TID)) return -1;
591+
if (PyModule_AddIntMacro(m, F_OWNER_PID)) return -1;
592+
if (PyModule_AddIntMacro(m, F_OWNER_PGRP)) return -1;
593+
#endif
594+
#ifdef F_GET_RW_HINT
595+
// since Linux 4.13
596+
if (PyModule_AddIntMacro(m, F_GET_RW_HINT)) return -1;
597+
if (PyModule_AddIntMacro(m, F_SET_RW_HINT)) return -1;
598+
if (PyModule_AddIntMacro(m, F_GET_FILE_RW_HINT)) return -1;
599+
if (PyModule_AddIntMacro(m, F_SET_FILE_RW_HINT)) return -1;
600+
#ifndef RWH_WRITE_LIFE_NOT_SET // typo in Linux < 5.5
601+
# define RWH_WRITE_LIFE_NOT_SET RWF_WRITE_LIFE_NOT_SET
602+
#endif
603+
if (PyModule_AddIntMacro(m, RWH_WRITE_LIFE_NOT_SET)) return -1;
604+
if (PyModule_AddIntMacro(m, RWH_WRITE_LIFE_NONE)) return -1;
605+
if (PyModule_AddIntMacro(m, RWH_WRITE_LIFE_SHORT)) return -1;
606+
if (PyModule_AddIntMacro(m, RWH_WRITE_LIFE_MEDIUM)) return -1;
607+
if (PyModule_AddIntMacro(m, RWH_WRITE_LIFE_LONG)) return -1;
608+
if (PyModule_AddIntMacro(m, RWH_WRITE_LIFE_EXTREME)) return -1;
609+
#endif
586610

587611
/* OS X specifics */
588612
#ifdef F_FULLFSYNC
@@ -599,6 +623,32 @@ all_ins(PyObject* m)
599623
#ifdef F_DUP2FD_CLOEXEC
600624
if (PyModule_AddIntMacro(m, F_DUP2FD_CLOEXEC)) return -1;
601625
#endif
626+
#ifdef F_READAHEAD
627+
if (PyModule_AddIntMacro(m, F_READAHEAD)) return -1;
628+
#endif
629+
#ifdef F_RDAHEAD
630+
if (PyModule_AddIntMacro(m, F_RDAHEAD)) return -1;
631+
#endif
632+
#ifdef F_ISUNIONSTACK
633+
if (PyModule_AddIntMacro(m, F_ISUNIONSTACK)) return -1;
634+
#endif
635+
#ifdef F_KINFO
636+
if (PyModule_AddIntMacro(m, F_KINFO)) return -1;
637+
#endif
638+
639+
/* NetBSD specifics */
640+
#ifdef F_CLOSEM
641+
if (PyModule_AddIntMacro(m, F_CLOSEM)) return -1;
642+
#endif
643+
#ifdef F_MAXFD
644+
if (PyModule_AddIntMacro(m, F_MAXFD)) return -1;
645+
#endif
646+
#ifdef F_GETNOSIGPIPE
647+
if (PyModule_AddIntMacro(m, F_GETNOSIGPIPE)) return -1;
648+
#endif
649+
#ifdef F_SETNOSIGPIPE
650+
if (PyModule_AddIntMacro(m, F_SETNOSIGPIPE)) return -1;
651+
#endif
602652

603653
/* For F_{GET|SET}FL */
604654
#ifdef FD_CLOEXEC
@@ -673,6 +723,9 @@ all_ins(PyObject* m)
673723
if (PyModule_AddIntMacro(m, F_SEAL_SHRINK)) return -1;
674724
if (PyModule_AddIntMacro(m, F_SEAL_GROW)) return -1;
675725
if (PyModule_AddIntMacro(m, F_SEAL_WRITE)) return -1;
726+
#ifdef F_SEAL_FUTURE_WRITE
727+
if (PyModule_AddIntMacro(m, F_SEAL_FUTURE_WRITE)) return -1;
728+
#endif
676729
#endif
677730
return 0;
678731
}

0 commit comments

Comments
 (0)