gh-92210: Move socket.__init__ to argument clinic (#92237)

Co-authored-by: Łukasz Langa <lukasz@langa.pl>
Co-authored-by: Erlend Egeberg Aasland <erlend.aasland@innova.no>
Co-authored-by: Erlend Egeberg Aasland <erlend.aasland@protonmail.com>
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
This commit is contained in:
Joshua Herman 2022-05-04 07:38:01 -06:00 committed by GitHub
parent 090819ec5f
commit 000a072318
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 92 additions and 14 deletions

View File

@ -0,0 +1 @@
Port ``socket.__init__`` to Argument Clinic. Patch by Cinder.

65
Modules/clinic/socketmodule.c.h generated Normal file
View File

@ -0,0 +1,65 @@
/*[clinic input]
preserve
[clinic start generated code]*/
static int
sock_initobj_impl(PySocketSockObject *self, int family, int type, int proto,
PyObject *fdobj);
static int
sock_initobj(PyObject *self, PyObject *args, PyObject *kwargs)
{
int return_value = -1;
static const char * const _keywords[] = {"family", "type", "proto", "fileno", NULL};
static _PyArg_Parser _parser = {NULL, _keywords, "socket", 0};
PyObject *argsbuf[4];
PyObject * const *fastargs;
Py_ssize_t nargs = PyTuple_GET_SIZE(args);
Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0;
int family = -1;
int type = -1;
int proto = -1;
PyObject *fdobj = NULL;
fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 0, 4, 0, argsbuf);
if (!fastargs) {
goto exit;
}
if (!noptargs) {
goto skip_optional_pos;
}
if (fastargs[0]) {
family = _PyLong_AsInt(fastargs[0]);
if (family == -1 && PyErr_Occurred()) {
goto exit;
}
if (!--noptargs) {
goto skip_optional_pos;
}
}
if (fastargs[1]) {
type = _PyLong_AsInt(fastargs[1]);
if (type == -1 && PyErr_Occurred()) {
goto exit;
}
if (!--noptargs) {
goto skip_optional_pos;
}
}
if (fastargs[2]) {
proto = _PyLong_AsInt(fastargs[2]);
if (proto == -1 && PyErr_Occurred()) {
goto exit;
}
if (!--noptargs) {
goto skip_optional_pos;
}
}
fdobj = fastargs[3];
skip_optional_pos:
return_value = sock_initobj_impl((PySocketSockObject *)self, family, type, proto, fdobj);
exit:
return return_value;
}
/*[clinic end generated code: output=2433d6ac51bc962a input=a9049054013a1b77]*/

View File

@ -328,6 +328,12 @@ static FlagRuntimeInfo win_runtime_flags[] = {
{14393, "TCP_FASTOPEN"}
};
/*[clinic input]
module _socket
class _socket.socket "PySocketSockObject *" "&sock_type"
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=7a8313d9b7f51988]*/
static int
remove_unusable_flags(PyObject *m)
{
@ -511,6 +517,8 @@ remove_unusable_flags(PyObject *m)
#define INADDR_NONE (-1)
#endif
#include "clinic/socketmodule.c.h"
/* XXX There's a problem here: *static* functions are not supposed to have
a Py prefix (or use CapitalizedWords). Later... */
@ -1708,7 +1716,7 @@ getsockaddrarg(PySocketSockObject *s, PyObject *args,
}
addr->sun_family = s->sock_family;
memcpy(addr->sun_path, path.buf, path.len);
retval = 1;
unix_out:
PyBuffer_Release(&path);
@ -5103,14 +5111,23 @@ static int sock_cloexec_works = -1;
#endif
/*ARGSUSED*/
/*[clinic input]
_socket.socket.__init__ as sock_initobj
family: int = -1
type: int = -1
proto: int = -1
fileno as fdobj: object = NULL
[clinic start generated code]*/
static int
sock_initobj(PyObject *self, PyObject *args, PyObject *kwds)
sock_initobj_impl(PySocketSockObject *self, int family, int type, int proto,
PyObject *fdobj)
/*[clinic end generated code: output=d114d026b9a9a810 input=04cfc32953f5cc25]*/
{
PySocketSockObject *s = (PySocketSockObject *)self;
PyObject *fdobj = NULL;
SOCKET_T fd = INVALID_SOCKET;
int family = -1, type = -1, proto = -1;
static char *keywords[] = {"family", "type", "proto", "fileno", 0};
#ifndef MS_WINDOWS
#ifdef SOCK_CLOEXEC
int *atomic_flag_works = &sock_cloexec_works;
@ -5119,18 +5136,13 @@ sock_initobj(PyObject *self, PyObject *args, PyObject *kwds)
#endif
#endif
if (!PyArg_ParseTupleAndKeywords(args, kwds,
"|iiiO:socket", keywords,
&family, &type, &proto, &fdobj))
return -1;
#ifdef MS_WINDOWS
/* In this case, we don't use the family, type and proto args */
if (fdobj == NULL || fdobj == Py_None)
#endif
{
if (PySys_Audit("socket.__new__", "Oiii",
s, family, type, proto) < 0) {
self, family, type, proto) < 0) {
return -1;
}
}
@ -5148,7 +5160,7 @@ sock_initobj(PyObject *self, PyObject *args, PyObject *kwds)
}
memcpy(&info, PyBytes_AS_STRING(fdobj), sizeof(info));
if (PySys_Audit("socket.__new__", "Oiii", s,
if (PySys_Audit("socket.__new__", "Oiii", self,
info.iAddressFamily, info.iSocketType,
info.iProtocol) < 0) {
return -1;
@ -5318,7 +5330,7 @@ sock_initobj(PyObject *self, PyObject *args, PyObject *kwds)
}
#endif
}
if (init_sockobject(s, fd, family, type, proto) == -1) {
if (init_sockobject(self, fd, family, type, proto) == -1) {
SOCKETCLOSE(fd);
return -1;
}