Skip to content

gh-92210: Port of Cinder's socket.__init__ which uses argument clinic. #92237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
May 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Port ``socket.__init__`` to Argument Clinic. Patch by Cinder.
65 changes: 65 additions & 0 deletions Modules/clinic/socketmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 26 additions & 14 deletions Modules/socketmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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... */

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
}
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down