gh-117764: Use Argument Clinic for signal.set_wakeup_fd() (GH-117777)

This commit is contained in:
Serhiy Storchaka 2024-04-12 14:21:00 +03:00 committed by GitHub
parent 3a8c1ca7e7
commit 39a6b29756
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 106 additions and 26 deletions

View File

@ -1275,6 +1275,7 @@ _PyStaticObjects_CheckRefcnt(PyInterpreterState *interp) {
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(version));
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(volume));
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(wait_all));
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(warn_on_full_buffer));
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(warnings));
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(warnoptions));
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(wbits));

View File

@ -764,6 +764,7 @@ struct _Py_global_strings {
STRUCT_FOR_ID(version)
STRUCT_FOR_ID(volume)
STRUCT_FOR_ID(wait_all)
STRUCT_FOR_ID(warn_on_full_buffer)
STRUCT_FOR_ID(warnings)
STRUCT_FOR_ID(warnoptions)
STRUCT_FOR_ID(wbits)

View File

@ -1273,6 +1273,7 @@ extern "C" {
INIT_ID(version), \
INIT_ID(volume), \
INIT_ID(wait_all), \
INIT_ID(warn_on_full_buffer), \
INIT_ID(warnings), \
INIT_ID(warnoptions), \
INIT_ID(wbits), \

View File

@ -2133,6 +2133,9 @@ _PyUnicode_InitStaticStrings(PyInterpreterState *interp) {
string = &_Py_ID(wait_all);
assert(_PyUnicode_CheckConsistency(string, 1));
_PyUnicode_InternInPlace(interp, &string);
string = &_Py_ID(warn_on_full_buffer);
assert(_PyUnicode_CheckConsistency(string, 1));
_PyUnicode_InternInPlace(interp, &string);
string = &_Py_ID(warnings);
assert(_PyUnicode_CheckConsistency(string, 1));
_PyUnicode_InternInPlace(interp, &string);

View File

@ -2,6 +2,10 @@
preserve
[clinic start generated code]*/
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
# include "pycore_gc.h" // PyGC_Head
# include "pycore_runtime.h" // _Py_ID()
#endif
#include "pycore_modsupport.h" // _PyArg_CheckPositional()
PyDoc_STRVAR(signal_default_int_handler__doc__,
@ -276,6 +280,77 @@ exit:
#endif /* defined(HAVE_SIGINTERRUPT) */
PyDoc_STRVAR(signal_set_wakeup_fd__doc__,
"set_wakeup_fd($module, fd, /, *, warn_on_full_buffer=True)\n"
"--\n"
"\n"
"Sets the fd to be written to (with the signal number) when a signal comes in.\n"
"\n"
"A library can use this to wakeup select or poll.\n"
"The previous fd or -1 is returned.\n"
"\n"
"The fd must be non-blocking.");
#define SIGNAL_SET_WAKEUP_FD_METHODDEF \
{"set_wakeup_fd", _PyCFunction_CAST(signal_set_wakeup_fd), METH_FASTCALL|METH_KEYWORDS, signal_set_wakeup_fd__doc__},
static PyObject *
signal_set_wakeup_fd_impl(PyObject *module, PyObject *fdobj,
int warn_on_full_buffer);
static PyObject *
signal_set_wakeup_fd(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
{
PyObject *return_value = NULL;
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
#define NUM_KEYWORDS 1
static struct {
PyGC_Head _this_is_not_used;
PyObject_VAR_HEAD
PyObject *ob_item[NUM_KEYWORDS];
} _kwtuple = {
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_item = { &_Py_ID(warn_on_full_buffer), },
};
#undef NUM_KEYWORDS
#define KWTUPLE (&_kwtuple.ob_base.ob_base)
#else // !Py_BUILD_CORE
# define KWTUPLE NULL
#endif // !Py_BUILD_CORE
static const char * const _keywords[] = {"", "warn_on_full_buffer", NULL};
static _PyArg_Parser _parser = {
.keywords = _keywords,
.fname = "set_wakeup_fd",
.kwtuple = KWTUPLE,
};
#undef KWTUPLE
PyObject *argsbuf[2];
Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
PyObject *fdobj;
int warn_on_full_buffer = 1;
args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
if (!args) {
goto exit;
}
fdobj = args[0];
if (!noptargs) {
goto skip_optional_kwonly;
}
warn_on_full_buffer = PyObject_IsTrue(args[1]);
if (warn_on_full_buffer < 0) {
goto exit;
}
skip_optional_kwonly:
return_value = signal_set_wakeup_fd_impl(module, fdobj, warn_on_full_buffer);
exit:
return return_value;
}
#if defined(HAVE_SETITIMER)
PyDoc_STRVAR(signal_setitimer__doc__,
@ -701,4 +776,4 @@ exit:
#ifndef SIGNAL_PIDFD_SEND_SIGNAL_METHODDEF
#define SIGNAL_PIDFD_SEND_SIGNAL_METHODDEF
#endif /* !defined(SIGNAL_PIDFD_SEND_SIGNAL_METHODDEF) */
/*[clinic end generated code: output=5a9928cb2dc75b5f input=a9049054013a1b77]*/
/*[clinic end generated code: output=1c11c1b6f12f26be input=a9049054013a1b77]*/

View File

@ -706,35 +706,43 @@ signal_siginterrupt_impl(PyObject *module, int signalnum, int flag)
#endif
/*[clinic input]
signal.set_wakeup_fd
fd as fdobj: object
/
*
warn_on_full_buffer: bool = True
Sets the fd to be written to (with the signal number) when a signal comes in.
A library can use this to wakeup select or poll.
The previous fd or -1 is returned.
The fd must be non-blocking.
[clinic start generated code]*/
static PyObject *
signal_set_wakeup_fd(PyObject *self, PyObject *args, PyObject *kwds)
signal_set_wakeup_fd_impl(PyObject *module, PyObject *fdobj,
int warn_on_full_buffer)
/*[clinic end generated code: output=2280d72dd2a54c4f input=5b545946a28b8339]*/
{
struct _Py_stat_struct status;
static char *kwlist[] = {
"", "warn_on_full_buffer", NULL,
};
int warn_on_full_buffer = 1;
#ifdef MS_WINDOWS
PyObject *fdobj;
SOCKET_T sockfd, old_sockfd;
int res;
int res_size = sizeof res;
PyObject *mod;
int is_socket;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|$p:set_wakeup_fd", kwlist,
&fdobj, &warn_on_full_buffer))
return NULL;
sockfd = PyLong_AsSocket_t(fdobj);
if (sockfd == (SOCKET_T)(-1) && PyErr_Occurred())
return NULL;
#else
int fd;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "i|$p:set_wakeup_fd", kwlist,
&fd, &warn_on_full_buffer))
int fd = PyLong_AsInt(fdobj);
if (fd == -1 && PyErr_Occurred()) {
return NULL;
}
#endif
PyThreadState *tstate = _PyThreadState_GET();
@ -820,15 +828,6 @@ signal_set_wakeup_fd(PyObject *self, PyObject *args, PyObject *kwds)
#endif
}
PyDoc_STRVAR(set_wakeup_fd_doc,
"set_wakeup_fd(fd, *, warn_on_full_buffer=True) -> fd\n\
\n\
Sets the fd to be written to (with the signal number) when a signal\n\
comes in. A library can use this to wakeup select or poll.\n\
The previous fd or -1 is returned.\n\
\n\
The fd must be non-blocking.");
/* C API for the same, without all the error checking */
int
PySignal_SetWakeupFd(int fd)
@ -1344,7 +1343,7 @@ static PyMethodDef signal_methods[] = {
SIGNAL_RAISE_SIGNAL_METHODDEF
SIGNAL_STRSIGNAL_METHODDEF
SIGNAL_GETSIGNAL_METHODDEF
{"set_wakeup_fd", _PyCFunction_CAST(signal_set_wakeup_fd), METH_VARARGS | METH_KEYWORDS, set_wakeup_fd_doc},
SIGNAL_SET_WAKEUP_FD_METHODDEF
SIGNAL_SIGINTERRUPT_METHODDEF
SIGNAL_PAUSE_METHODDEF
SIGNAL_PIDFD_SEND_SIGNAL_METHODDEF