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_OpenCode(const char *utf8path);
PyAPI_FUNC(PyObject *) PyFile_OpenCodeObject(PyObject *path); PyAPI_FUNC(PyObject *) PyFile_OpenCodeObject(PyObject *path);
PyAPI_FUNC(int) PyFile_SetOpenCodeHook(Py_OpenCodeHookFunction hook, void *userData); 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)) { if (!_PyArg_CheckPositional("fcntl", nargs, 2, 3)) {
goto exit; goto exit;
} }
if (!conv_descriptor(args[0], &fd)) { if (!_PyLong_FileDescriptor_Converter(args[0], &fd)) {
goto exit; goto exit;
} }
code = _PyLong_AsInt(args[1]); 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)) { if (!_PyArg_CheckPositional("ioctl", nargs, 2, 4)) {
goto exit; goto exit;
} }
if (!conv_descriptor(args[0], &fd)) { if (!_PyLong_FileDescriptor_Converter(args[0], &fd)) {
goto exit; goto exit;
} }
code = (unsigned int)PyLong_AsUnsignedLongMask(args[1]); 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)) { if (!_PyArg_CheckPositional("flock", nargs, 2, 2)) {
goto exit; goto exit;
} }
if (!conv_descriptor(args[0], &fd)) { if (!_PyLong_FileDescriptor_Converter(args[0], &fd)) {
goto exit; goto exit;
} }
code = _PyLong_AsInt(args[1]); 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)) { if (!_PyArg_CheckPositional("lockf", nargs, 2, 5)) {
goto exit; goto exit;
} }
if (!conv_descriptor(args[0], &fd)) { if (!_PyLong_FileDescriptor_Converter(args[0], &fd)) {
goto exit; goto exit;
} }
code = _PyLong_AsInt(args[1]); code = _PyLong_AsInt(args[1]);
@ -243,4 +243,4 @@ skip_optional:
exit: exit:
return return_value; 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) { if (!args) {
goto exit; goto exit;
} }
if (!fildes_converter(args[0], &fd)) { if (!_PyLong_FileDescriptor_Converter(args[0], &fd)) {
goto exit; goto exit;
} }
return_value = os_fchdir_impl(module, fd); 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) { if (!args) {
goto exit; goto exit;
} }
if (!fildes_converter(args[0], &fd)) { if (!_PyLong_FileDescriptor_Converter(args[0], &fd)) {
goto exit; goto exit;
} }
return_value = os_fsync_impl(module, fd); 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) { if (!args) {
goto exit; goto exit;
} }
if (!fildes_converter(args[0], &fd)) { if (!_PyLong_FileDescriptor_Converter(args[0], &fd)) {
goto exit; goto exit;
} }
return_value = os_fdatasync_impl(module, fd); 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)) { if (!_PyArg_CheckPositional("fpathconf", nargs, 2, 2)) {
goto exit; goto exit;
} }
if (!fildes_converter(args[0], &fd)) { if (!_PyLong_FileDescriptor_Converter(args[0], &fd)) {
goto exit; goto exit;
} }
if (!conv_path_confname(args[1], &name)) { if (!conv_path_confname(args[1], &name)) {
@ -8919,4 +8919,4 @@ exit:
#ifndef OS_WAITSTATUS_TO_EXITCODE_METHODDEF #ifndef OS_WAITSTATUS_TO_EXITCODE_METHODDEF
#define OS_WAITSTATUS_TO_EXITCODE_METHODDEF #define OS_WAITSTATUS_TO_EXITCODE_METHODDEF
#endif /* !defined(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)) { if (!_PyArg_CheckPositional("register", nargs, 1, 2)) {
goto exit; goto exit;
} }
if (!fildes_converter(args[0], &fd)) { if (!_PyLong_FileDescriptor_Converter(args[0], &fd)) {
goto exit; goto exit;
} }
if (nargs < 2) { 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)) { if (!_PyArg_CheckPositional("modify", nargs, 2, 2)) {
goto exit; goto exit;
} }
if (!fildes_converter(args[0], &fd)) { if (!_PyLong_FileDescriptor_Converter(args[0], &fd)) {
goto exit; goto exit;
} }
if (!_PyLong_UnsignedShort_Converter(args[1], &eventmask)) { if (!_PyLong_UnsignedShort_Converter(args[1], &eventmask)) {
@ -174,7 +174,7 @@ select_poll_unregister(pollObject *self, PyObject *arg)
PyObject *return_value = NULL; PyObject *return_value = NULL;
int fd; int fd;
if (!fildes_converter(arg, &fd)) { if (!_PyLong_FileDescriptor_Converter(arg, &fd)) {
goto exit; goto exit;
} }
return_value = select_poll_unregister_impl(self, fd); 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)) { if (!_PyArg_CheckPositional("register", nargs, 1, 2)) {
goto exit; goto exit;
} }
if (!fildes_converter(args[0], &fd)) { if (!_PyLong_FileDescriptor_Converter(args[0], &fd)) {
goto exit; goto exit;
} }
if (nargs < 2) { 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)) { if (!_PyArg_CheckPositional("modify", nargs, 1, 2)) {
goto exit; goto exit;
} }
if (!fildes_converter(args[0], &fd)) { if (!_PyLong_FileDescriptor_Converter(args[0], &fd)) {
goto exit; goto exit;
} }
if (nargs < 2) { if (nargs < 2) {
@ -344,7 +344,7 @@ select_devpoll_unregister(devpollObject *self, PyObject *arg)
PyObject *return_value = NULL; PyObject *return_value = NULL;
int fd; int fd;
if (!fildes_converter(arg, &fd)) { if (!_PyLong_FileDescriptor_Converter(arg, &fd)) {
goto exit; goto exit;
} }
return_value = select_devpoll_unregister_impl(self, fd); 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) { if (!args) {
goto exit; goto exit;
} }
if (!fildes_converter(args[0], &fd)) { if (!_PyLong_FileDescriptor_Converter(args[0], &fd)) {
goto exit; goto exit;
} }
if (!noptargs) { if (!noptargs) {
@ -721,7 +721,7 @@ select_epoll_modify(pyEpoll_Object *self, PyObject *const *args, Py_ssize_t narg
if (!args) { if (!args) {
goto exit; goto exit;
} }
if (!fildes_converter(args[0], &fd)) { if (!_PyLong_FileDescriptor_Converter(args[0], &fd)) {
goto exit; goto exit;
} }
eventmask = (unsigned int)PyLong_AsUnsignedLongMask(args[1]); 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) { if (!args) {
goto exit; goto exit;
} }
if (!fildes_converter(args[0], &fd)) { if (!_PyLong_FileDescriptor_Converter(args[0], &fd)) {
goto exit; goto exit;
} }
return_value = select_epoll_unregister_impl(self, fd); return_value = select_epoll_unregister_impl(self, fd);
@ -1179,4 +1179,4 @@ exit:
#ifndef SELECT_KQUEUE_CONTROL_METHODDEF #ifndef SELECT_KQUEUE_CONTROL_METHODDEF
#define SELECT_KQUEUE_CONTROL_METHODDEF #define SELECT_KQUEUE_CONTROL_METHODDEF
#endif /* !defined(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 start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=124b58387c158179]*/ /*[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" #include "clinic/fcntlmodule.c.h"
/*[clinic input] /*[clinic input]
fcntl.fcntl fcntl.fcntl
fd: object(type='int', converter='conv_descriptor') fd: fildes
cmd as code: int cmd as code: int
arg: object(c_default='NULL') = 0 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 * static PyObject *
fcntl_fcntl_impl(PyObject *module, int fd, int code, PyObject *arg) 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; unsigned int int_arg = 0;
int ret; int ret;
@ -116,7 +104,7 @@ fcntl_fcntl_impl(PyObject *module, int fd, int code, PyObject *arg)
/*[clinic input] /*[clinic input]
fcntl.ioctl fcntl.ioctl
fd: object(type='int', converter='conv_descriptor') fd: fildes
request as code: unsigned_int(bitwise=True) request as code: unsigned_int(bitwise=True)
arg as ob_arg: object(c_default='NULL') = 0 arg as ob_arg: object(c_default='NULL') = 0
mutate_flag as mutate_arg: bool = True mutate_flag as mutate_arg: bool = True
@ -155,7 +143,7 @@ code.
static PyObject * static PyObject *
fcntl_ioctl_impl(PyObject *module, int fd, unsigned int code, fcntl_ioctl_impl(PyObject *module, int fd, unsigned int code,
PyObject *ob_arg, int mutate_arg) 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 #define IOCTL_BUFSZ 1024
/* We use the unsigned non-checked 'I' format for the 'code' parameter /* 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] /*[clinic input]
fcntl.flock fcntl.flock
fd: object(type='int', converter='conv_descriptor') fd: fildes
operation as code: int operation as code: int
/ /
@ -292,7 +280,7 @@ function is emulated using fcntl()).
static PyObject * static PyObject *
fcntl_flock_impl(PyObject *module, int fd, int code) 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 ret;
int async_err = 0; int async_err = 0;
@ -346,7 +334,7 @@ fcntl_flock_impl(PyObject *module, int fd, int code)
/*[clinic input] /*[clinic input]
fcntl.lockf fcntl.lockf
fd: object(type='int', converter='conv_descriptor') fd: fildes
cmd as code: int cmd as code: int
len as lenobj: object(c_default='NULL') = 0 len as lenobj: object(c_default='NULL') = 0
start as startobj: 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 * static PyObject *
fcntl_lockf_impl(PyObject *module, int fd, int code, PyObject *lenobj, fcntl_lockf_impl(PyObject *module, int fd, int code, PyObject *lenobj,
PyObject *startobj, int whence) PyObject *startobj, int whence)
/*[clinic end generated code: output=4985e7a172e7461a input=3a5dc01b04371f1a]*/ /*[clinic end generated code: output=4985e7a172e7461a input=5480479fc63a04b8]*/
{ {
int ret; int ret;
int async_err = 0; int async_err = 0;

View File

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

View File

@ -88,25 +88,6 @@ class select.kqueue "kqueue_queue_Object *" "_selectstate_global->kqueue_queue_T
[clinic start generated code]*/ [clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=41071028e0ede093]*/ /*[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 */ /* list of Python objects and their file descriptor */
typedef struct { typedef struct {
PyObject *obj; /* owned reference */ PyObject *obj; /* owned reference */

View File

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

View File

@ -223,6 +223,17 @@ PyObject_AsFileDescriptor(PyObject *o)
return fd; 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 ** Py_UniversalNewlineFgets is an fgets variation that understands
** all of \r, \n and \r\n conventions. ** 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) 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): class float_converter(CConverter):
type = 'float' type = 'float'
default_type = float default_type = float