gh-85283: Build fcntl extension with the limited C API (#116791)

This commit is contained in:
Victor Stinner 2024-03-14 13:01:13 +01:00 committed by GitHub
parent f20dfb7569
commit 97b80af897
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 48 additions and 26 deletions

View File

@ -1460,8 +1460,8 @@ Build Changes
* Building CPython now requires a compiler with support for the C11 atomic
library, GCC built-in atomic functions, or MSVC interlocked intrinsics.
* The ``errno``, ``md5``, ``resource``, ``winsound``, ``_ctypes_test``,
``_multiprocessing.posixshmem``, ``_scproxy``, ``_stat``,
* The ``errno``, ``fcntl``, ``grp``, ``md5``, ``resource``, ``winsound``,
``_ctypes_test``, ``_multiprocessing.posixshmem``, ``_scproxy``, ``_stat``,
``_testimportmultiple`` and ``_uuid`` C extensions are now built with the
:ref:`limited C API <limited-c-api>`.
(Contributed by Victor Stinner in :gh:`85283`.)

View File

@ -0,0 +1,2 @@
The ``fcntl`` and ``grp`` C extensions are now built with the :ref:`limited
C API <limited-c-api>`. (Contributed by Victor Stinner in :gh:`85283`.)

View File

@ -2,9 +2,6 @@
preserve
[clinic start generated code]*/
#include "pycore_fileutils.h" // _PyLong_FileDescriptor_Converter()
#include "pycore_modsupport.h" // _PyArg_CheckPositional()
PyDoc_STRVAR(fcntl_fcntl__doc__,
"fcntl($module, fd, cmd, arg=0, /)\n"
"--\n"
@ -22,7 +19,7 @@ PyDoc_STRVAR(fcntl_fcntl__doc__,
"corresponding to the return value of the fcntl call in the C code.");
#define FCNTL_FCNTL_METHODDEF \
{"fcntl", _PyCFunction_CAST(fcntl_fcntl), METH_FASTCALL, fcntl_fcntl__doc__},
{"fcntl", (PyCFunction)(void(*)(void))fcntl_fcntl, METH_FASTCALL, fcntl_fcntl__doc__},
static PyObject *
fcntl_fcntl_impl(PyObject *module, int fd, int code, PyObject *arg);
@ -35,10 +32,16 @@ fcntl_fcntl(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
int code;
PyObject *arg = NULL;
if (!_PyArg_CheckPositional("fcntl", nargs, 2, 3)) {
if (nargs < 2) {
PyErr_Format(PyExc_TypeError, "fcntl expected at least 2 arguments, got %zd", nargs);
goto exit;
}
if (!_PyLong_FileDescriptor_Converter(args[0], &fd)) {
if (nargs > 3) {
PyErr_Format(PyExc_TypeError, "fcntl expected at most 3 arguments, got %zd", nargs);
goto exit;
}
fd = PyObject_AsFileDescriptor(args[0]);
if (fd < 0) {
goto exit;
}
code = PyLong_AsInt(args[1]);
@ -90,7 +93,7 @@ PyDoc_STRVAR(fcntl_ioctl__doc__,
"code.");
#define FCNTL_IOCTL_METHODDEF \
{"ioctl", _PyCFunction_CAST(fcntl_ioctl), METH_FASTCALL, fcntl_ioctl__doc__},
{"ioctl", (PyCFunction)(void(*)(void))fcntl_ioctl, METH_FASTCALL, fcntl_ioctl__doc__},
static PyObject *
fcntl_ioctl_impl(PyObject *module, int fd, unsigned int code,
@ -105,10 +108,16 @@ fcntl_ioctl(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
PyObject *ob_arg = NULL;
int mutate_arg = 1;
if (!_PyArg_CheckPositional("ioctl", nargs, 2, 4)) {
if (nargs < 2) {
PyErr_Format(PyExc_TypeError, "ioctl expected at least 2 arguments, got %zd", nargs);
goto exit;
}
if (!_PyLong_FileDescriptor_Converter(args[0], &fd)) {
if (nargs > 4) {
PyErr_Format(PyExc_TypeError, "ioctl expected at most 4 arguments, got %zd", nargs);
goto exit;
}
fd = PyObject_AsFileDescriptor(args[0]);
if (fd < 0) {
goto exit;
}
code = (unsigned int)PyLong_AsUnsignedLongMask(args[1]);
@ -143,7 +152,7 @@ PyDoc_STRVAR(fcntl_flock__doc__,
"function is emulated using fcntl()).");
#define FCNTL_FLOCK_METHODDEF \
{"flock", _PyCFunction_CAST(fcntl_flock), METH_FASTCALL, fcntl_flock__doc__},
{"flock", (PyCFunction)(void(*)(void))fcntl_flock, METH_FASTCALL, fcntl_flock__doc__},
static PyObject *
fcntl_flock_impl(PyObject *module, int fd, int code);
@ -155,10 +164,12 @@ fcntl_flock(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
int fd;
int code;
if (!_PyArg_CheckPositional("flock", nargs, 2, 2)) {
if (nargs != 2) {
PyErr_Format(PyExc_TypeError, "flock expected 2 arguments, got %zd", nargs);
goto exit;
}
if (!_PyLong_FileDescriptor_Converter(args[0], &fd)) {
fd = PyObject_AsFileDescriptor(args[0]);
if (fd < 0) {
goto exit;
}
code = PyLong_AsInt(args[1]);
@ -199,7 +210,7 @@ PyDoc_STRVAR(fcntl_lockf__doc__,
" 2 - relative to the end of the file (SEEK_END)");
#define FCNTL_LOCKF_METHODDEF \
{"lockf", _PyCFunction_CAST(fcntl_lockf), METH_FASTCALL, fcntl_lockf__doc__},
{"lockf", (PyCFunction)(void(*)(void))fcntl_lockf, METH_FASTCALL, fcntl_lockf__doc__},
static PyObject *
fcntl_lockf_impl(PyObject *module, int fd, int code, PyObject *lenobj,
@ -215,10 +226,16 @@ fcntl_lockf(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
PyObject *startobj = NULL;
int whence = 0;
if (!_PyArg_CheckPositional("lockf", nargs, 2, 5)) {
if (nargs < 2) {
PyErr_Format(PyExc_TypeError, "lockf expected at least 2 arguments, got %zd", nargs);
goto exit;
}
if (!_PyLong_FileDescriptor_Converter(args[0], &fd)) {
if (nargs > 5) {
PyErr_Format(PyExc_TypeError, "lockf expected at most 5 arguments, got %zd", nargs);
goto exit;
}
fd = PyObject_AsFileDescriptor(args[0]);
if (fd < 0) {
goto exit;
}
code = PyLong_AsInt(args[1]);
@ -246,4 +263,4 @@ skip_optional:
exit:
return return_value;
}
/*[clinic end generated code: output=732e33ba92042031 input=a9049054013a1b77]*/
/*[clinic end generated code: output=26793691ab1c75ba input=a9049054013a1b77]*/

View File

@ -1,22 +1,25 @@
/* fcntl module */
#ifndef Py_BUILD_CORE_BUILTIN
# define Py_BUILD_CORE_MODULE 1
// Need limited C API version 3.13 for PyLong_AsInt()
#include "pyconfig.h" // Py_GIL_DISABLED
#ifndef Py_GIL_DISABLED
# define Py_LIMITED_API 0x030d0000
#endif
#include "Python.h"
#include <errno.h> // EINTR
#include <fcntl.h> // fcntl()
#include <string.h> // memcpy()
#include <sys/ioctl.h> // ioctl()
#ifdef HAVE_SYS_FILE_H
#include <sys/file.h>
# include <sys/file.h> // flock()
#endif
#ifdef HAVE_LINUX_FS_H
#include <linux/fs.h>
# include <linux/fs.h>
#endif
#include <sys/ioctl.h>
#include <fcntl.h>
#ifdef HAVE_STROPTS_H
#include <stropts.h>
# include <stropts.h> // I_FLUSHBAND
#endif
/*[clinic input]