bpo-41985: Add _PyLong_FileDescriptor_Converter and AC converter for "fildes". (GH-22620)

This commit is contained in:
Serhiy Storchaka 2020-10-09 23:00:45 +03:00 committed by GitHub
parent b2c0a43699
commit 9975cc5008
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 61 additions and 94 deletions

View File

@ -22,3 +22,5 @@ typedef PyObject * (*Py_OpenCodeHookFunction)(PyObject *, void *);
PyAPI_FUNC(PyObject *) PyFile_OpenCode(const char *utf8path);
PyAPI_FUNC(PyObject *) PyFile_OpenCodeObject(PyObject *path);
PyAPI_FUNC(int) PyFile_SetOpenCodeHook(Py_OpenCodeHookFunction hook, void *userData);
PyAPI_FUNC(int) _PyLong_FileDescriptor_Converter(PyObject *, void *);

View File

@ -35,7 +35,7 @@ fcntl_fcntl(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
if (!_PyArg_CheckPositional("fcntl", nargs, 2, 3)) {
goto exit;
}
if (!conv_descriptor(args[0], &fd)) {
if (!_PyLong_FileDescriptor_Converter(args[0], &fd)) {
goto exit;
}
code = _PyLong_AsInt(args[1]);
@ -105,7 +105,7 @@ fcntl_ioctl(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
if (!_PyArg_CheckPositional("ioctl", nargs, 2, 4)) {
goto exit;
}
if (!conv_descriptor(args[0], &fd)) {
if (!_PyLong_FileDescriptor_Converter(args[0], &fd)) {
goto exit;
}
code = (unsigned int)PyLong_AsUnsignedLongMask(args[1]);
@ -155,7 +155,7 @@ fcntl_flock(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
if (!_PyArg_CheckPositional("flock", nargs, 2, 2)) {
goto exit;
}
if (!conv_descriptor(args[0], &fd)) {
if (!_PyLong_FileDescriptor_Converter(args[0], &fd)) {
goto exit;
}
code = _PyLong_AsInt(args[1]);
@ -215,7 +215,7 @@ fcntl_lockf(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
if (!_PyArg_CheckPositional("lockf", nargs, 2, 5)) {
goto exit;
}
if (!conv_descriptor(args[0], &fd)) {
if (!_PyLong_FileDescriptor_Converter(args[0], &fd)) {
goto exit;
}
code = _PyLong_AsInt(args[1]);
@ -243,4 +243,4 @@ skip_optional:
exit:
return return_value;
}
/*[clinic end generated code: output=91c2295402509595 input=a9049054013a1b77]*/
/*[clinic end generated code: output=8ea34bd0f7cf25ec input=a9049054013a1b77]*/

View File

@ -357,7 +357,7 @@ os_fchdir(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *k
if (!args) {
goto exit;
}
if (!fildes_converter(args[0], &fd)) {
if (!_PyLong_FileDescriptor_Converter(args[0], &fd)) {
goto exit;
}
return_value = os_fchdir_impl(module, fd);
@ -727,7 +727,7 @@ os_fsync(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kw
if (!args) {
goto exit;
}
if (!fildes_converter(args[0], &fd)) {
if (!_PyLong_FileDescriptor_Converter(args[0], &fd)) {
goto exit;
}
return_value = os_fsync_impl(module, fd);
@ -787,7 +787,7 @@ os_fdatasync(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject
if (!args) {
goto exit;
}
if (!fildes_converter(args[0], &fd)) {
if (!_PyLong_FileDescriptor_Converter(args[0], &fd)) {
goto exit;
}
return_value = os_fdatasync_impl(module, fd);
@ -6821,7 +6821,7 @@ os_fpathconf(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
if (!_PyArg_CheckPositional("fpathconf", nargs, 2, 2)) {
goto exit;
}
if (!fildes_converter(args[0], &fd)) {
if (!_PyLong_FileDescriptor_Converter(args[0], &fd)) {
goto exit;
}
if (!conv_path_confname(args[1], &name)) {
@ -8919,4 +8919,4 @@ exit:
#ifndef OS_WAITSTATUS_TO_EXITCODE_METHODDEF
#define OS_WAITSTATUS_TO_EXITCODE_METHODDEF
#endif /* !defined(OS_WAITSTATUS_TO_EXITCODE_METHODDEF) */
/*[clinic end generated code: output=a0fbdea47249ee0c input=a9049054013a1b77]*/
/*[clinic end generated code: output=936f33448cd66ccb input=a9049054013a1b77]*/

View File

@ -92,7 +92,7 @@ select_poll_register(pollObject *self, PyObject *const *args, Py_ssize_t nargs)
if (!_PyArg_CheckPositional("register", nargs, 1, 2)) {
goto exit;
}
if (!fildes_converter(args[0], &fd)) {
if (!_PyLong_FileDescriptor_Converter(args[0], &fd)) {
goto exit;
}
if (nargs < 2) {
@ -140,7 +140,7 @@ select_poll_modify(pollObject *self, PyObject *const *args, Py_ssize_t nargs)
if (!_PyArg_CheckPositional("modify", nargs, 2, 2)) {
goto exit;
}
if (!fildes_converter(args[0], &fd)) {
if (!_PyLong_FileDescriptor_Converter(args[0], &fd)) {
goto exit;
}
if (!_PyLong_UnsignedShort_Converter(args[1], &eventmask)) {
@ -174,7 +174,7 @@ select_poll_unregister(pollObject *self, PyObject *arg)
PyObject *return_value = NULL;
int fd;
if (!fildes_converter(arg, &fd)) {
if (!_PyLong_FileDescriptor_Converter(arg, &fd)) {
goto exit;
}
return_value = select_poll_unregister_impl(self, fd);
@ -256,7 +256,7 @@ select_devpoll_register(devpollObject *self, PyObject *const *args, Py_ssize_t n
if (!_PyArg_CheckPositional("register", nargs, 1, 2)) {
goto exit;
}
if (!fildes_converter(args[0], &fd)) {
if (!_PyLong_FileDescriptor_Converter(args[0], &fd)) {
goto exit;
}
if (nargs < 2) {
@ -306,7 +306,7 @@ select_devpoll_modify(devpollObject *self, PyObject *const *args, Py_ssize_t nar
if (!_PyArg_CheckPositional("modify", nargs, 1, 2)) {
goto exit;
}
if (!fildes_converter(args[0], &fd)) {
if (!_PyLong_FileDescriptor_Converter(args[0], &fd)) {
goto exit;
}
if (nargs < 2) {
@ -344,7 +344,7 @@ select_devpoll_unregister(devpollObject *self, PyObject *arg)
PyObject *return_value = NULL;
int fd;
if (!fildes_converter(arg, &fd)) {
if (!_PyLong_FileDescriptor_Converter(arg, &fd)) {
goto exit;
}
return_value = select_devpoll_unregister_impl(self, fd);
@ -668,7 +668,7 @@ select_epoll_register(pyEpoll_Object *self, PyObject *const *args, Py_ssize_t na
if (!args) {
goto exit;
}
if (!fildes_converter(args[0], &fd)) {
if (!_PyLong_FileDescriptor_Converter(args[0], &fd)) {
goto exit;
}
if (!noptargs) {
@ -721,7 +721,7 @@ select_epoll_modify(pyEpoll_Object *self, PyObject *const *args, Py_ssize_t narg
if (!args) {
goto exit;
}
if (!fildes_converter(args[0], &fd)) {
if (!_PyLong_FileDescriptor_Converter(args[0], &fd)) {
goto exit;
}
eventmask = (unsigned int)PyLong_AsUnsignedLongMask(args[1]);
@ -766,7 +766,7 @@ select_epoll_unregister(pyEpoll_Object *self, PyObject *const *args, Py_ssize_t
if (!args) {
goto exit;
}
if (!fildes_converter(args[0], &fd)) {
if (!_PyLong_FileDescriptor_Converter(args[0], &fd)) {
goto exit;
}
return_value = select_epoll_unregister_impl(self, fd);
@ -1179,4 +1179,4 @@ exit:
#ifndef SELECT_KQUEUE_CONTROL_METHODDEF
#define SELECT_KQUEUE_CONTROL_METHODDEF
#endif /* !defined(SELECT_KQUEUE_CONTROL_METHODDEF) */
/*[clinic end generated code: output=7144233c42e18279 input=a9049054013a1b77]*/
/*[clinic end generated code: output=162f4f4efa850416 input=a9049054013a1b77]*/

View File

@ -20,24 +20,12 @@ module fcntl
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=124b58387c158179]*/
static int
conv_descriptor(PyObject *object, int *target)
{
int fd = PyObject_AsFileDescriptor(object);
if (fd < 0)
return 0;
*target = fd;
return 1;
}
/* Must come after conv_descriptor definition. */
#include "clinic/fcntlmodule.c.h"
/*[clinic input]
fcntl.fcntl
fd: object(type='int', converter='conv_descriptor')
fd: fildes
cmd as code: int
arg: object(c_default='NULL') = 0
/
@ -57,7 +45,7 @@ corresponding to the return value of the fcntl call in the C code.
static PyObject *
fcntl_fcntl_impl(PyObject *module, int fd, int code, PyObject *arg)
/*[clinic end generated code: output=888fc93b51c295bd input=8cefbe59b29efbe2]*/
/*[clinic end generated code: output=888fc93b51c295bd input=7955340198e5f334]*/
{
unsigned int int_arg = 0;
int ret;
@ -116,7 +104,7 @@ fcntl_fcntl_impl(PyObject *module, int fd, int code, PyObject *arg)
/*[clinic input]
fcntl.ioctl
fd: object(type='int', converter='conv_descriptor')
fd: fildes
request as code: unsigned_int(bitwise=True)
arg as ob_arg: object(c_default='NULL') = 0
mutate_flag as mutate_arg: bool = True
@ -155,7 +143,7 @@ code.
static PyObject *
fcntl_ioctl_impl(PyObject *module, int fd, unsigned int code,
PyObject *ob_arg, int mutate_arg)
/*[clinic end generated code: output=7f7f5840c65991be input=ede70c433cccbbb2]*/
/*[clinic end generated code: output=7f7f5840c65991be input=967b4a4cbeceb0a8]*/
{
#define IOCTL_BUFSZ 1024
/* We use the unsigned non-checked 'I' format for the 'code' parameter
@ -280,7 +268,7 @@ fcntl_ioctl_impl(PyObject *module, int fd, unsigned int code,
/*[clinic input]
fcntl.flock
fd: object(type='int', converter='conv_descriptor')
fd: fildes
operation as code: int
/
@ -292,7 +280,7 @@ function is emulated using fcntl()).
static PyObject *
fcntl_flock_impl(PyObject *module, int fd, int code)
/*[clinic end generated code: output=84059e2b37d2fc64 input=b70a0a41ca22a8a0]*/
/*[clinic end generated code: output=84059e2b37d2fc64 input=0bfc00f795953452]*/
{
int ret;
int async_err = 0;
@ -346,7 +334,7 @@ fcntl_flock_impl(PyObject *module, int fd, int code)
/*[clinic input]
fcntl.lockf
fd: object(type='int', converter='conv_descriptor')
fd: fildes
cmd as code: int
len as lenobj: object(c_default='NULL') = 0
start as startobj: object(c_default='NULL') = 0
@ -380,7 +368,7 @@ starts. `whence` is as with fileobj.seek(), specifically:
static PyObject *
fcntl_lockf_impl(PyObject *module, int fd, int code, PyObject *lenobj,
PyObject *startobj, int whence)
/*[clinic end generated code: output=4985e7a172e7461a input=3a5dc01b04371f1a]*/
/*[clinic end generated code: output=4985e7a172e7461a input=5480479fc63a04b8]*/
{
int ret;
int async_err = 0;

View File

@ -1634,18 +1634,6 @@ path_error2(path_t *path, path_t *path2)
/* POSIX generic methods */
static int
fildes_converter(PyObject *o, void *p)
{
int fd;
int *pointer = (int *)p;
fd = PyObject_AsFileDescriptor(o);
if (fd < 0)
return 0;
*pointer = fd;
return 1;
}
static PyObject *
posix_fildes_fd(int fd, int (*func)(int))
{
@ -2642,10 +2630,6 @@ class dir_fd_converter(CConverter):
else:
self.converter = 'dir_fd_converter'
class fildes_converter(CConverter):
type = 'int'
converter = 'fildes_converter'
class uid_t_converter(CConverter):
type = "uid_t"
converter = '_Py_Uid_Converter'
@ -2708,7 +2692,7 @@ class sysconf_confname_converter(path_confname_converter):
converter="conv_sysconf_confname"
[python start generated code]*/
/*[python end generated code: output=da39a3ee5e6b4b0d input=f1c8ae8d744f6c8b]*/
/*[python end generated code: output=da39a3ee5e6b4b0d input=3338733161aa7879]*/
/*[clinic input]

View File

@ -88,25 +88,6 @@ class select.kqueue "kqueue_queue_Object *" "_selectstate_global->kqueue_queue_T
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=41071028e0ede093]*/
static int
fildes_converter(PyObject *o, void *p)
{
int fd;
int *pointer = (int *)p;
fd = PyObject_AsFileDescriptor(o);
if (fd == -1)
return 0;
*pointer = fd;
return 1;
}
/*[python input]
class fildes_converter(CConverter):
type = 'int'
converter = 'fildes_converter'
[python start generated code]*/
/*[python end generated code: output=da39a3ee5e6b4b0d input=ca54eb5aa476e20a]*/
/* list of Python objects and their file descriptor */
typedef struct {
PyObject *obj; /* owned reference */

View File

@ -51,18 +51,6 @@ get_termios_state(PyObject *module)
return (termiosmodulestate *)state;
}
static int fdconv(PyObject* obj, void* p)
{
int fd;
fd = PyObject_AsFileDescriptor(obj);
if (fd >= 0) {
*(int*)p = fd;
return 1;
}
return 0;
}
static struct PyModuleDef termiosmodule;
PyDoc_STRVAR(termios_tcgetattr__doc__,
@ -81,7 +69,7 @@ termios_tcgetattr(PyObject *module, PyObject *args)
{
int fd;
if (!PyArg_ParseTuple(args, "O&:tcgetattr",
fdconv, (void*)&fd)) {
_PyLong_FileDescriptor_Converter, (void*)&fd)) {
return NULL;
}
@ -160,7 +148,7 @@ termios_tcsetattr(PyObject *module, PyObject *args)
int fd, when;
PyObject *term;
if (!PyArg_ParseTuple(args, "O&iO:tcsetattr",
fdconv, &fd, &when, &term)) {
_PyLong_FileDescriptor_Converter, &fd, &when, &term)) {
return NULL;
}
@ -233,7 +221,7 @@ termios_tcsendbreak(PyObject *module, PyObject *args)
{
int fd, duration;
if (!PyArg_ParseTuple(args, "O&i:tcsendbreak",
fdconv, &fd, &duration)) {
_PyLong_FileDescriptor_Converter, &fd, &duration)) {
return NULL;
}
@ -255,7 +243,7 @@ termios_tcdrain(PyObject *module, PyObject *args)
{
int fd;
if (!PyArg_ParseTuple(args, "O&:tcdrain",
fdconv, &fd)) {
_PyLong_FileDescriptor_Converter, &fd)) {
return NULL;
}
@ -280,7 +268,7 @@ termios_tcflush(PyObject *module, PyObject *args)
{
int fd, queue;
if (!PyArg_ParseTuple(args, "O&i:tcflush",
fdconv, &fd, &queue)) {
_PyLong_FileDescriptor_Converter, &fd, &queue)) {
return NULL;
}
@ -305,7 +293,7 @@ termios_tcflow(PyObject *module, PyObject *args)
{
int fd, action;
if (!PyArg_ParseTuple(args, "O&i:tcflow",
fdconv, &fd, &action)) {
_PyLong_FileDescriptor_Converter, &fd, &action)) {
return NULL;
}

View File

@ -223,6 +223,17 @@ PyObject_AsFileDescriptor(PyObject *o)
return fd;
}
int
_PyLong_FileDescriptor_Converter(PyObject *o, void *ptr)
{
int fd = PyObject_AsFileDescriptor(o);
if (fd == -1) {
return 0;
}
*(int *)ptr = fd;
return 1;
}
/*
** Py_UniversalNewlineFgets is an fgets variation that understands
** all of \r, \n and \r\n conventions.

View File

@ -3103,6 +3103,19 @@ class size_t_converter(CConverter):
return super().parse_arg(argname, displayname)
class fildes_converter(CConverter):
type = 'int'
converter = '_PyLong_FileDescriptor_Converter'
def _parse_arg(self, argname, displayname):
return """
{paramname} = PyObject_AsFileDescriptor({argname});
if ({paramname} == -1) {{{{
goto exit;
}}}}
""".format(argname=argname, paramname=self.name)
class float_converter(CConverter):
type = 'float'
default_type = float