]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-92210: Move socket.__init__ to argument clinic (#92237)
authorJoshua Herman <30265+zitterbewegung@users.noreply.github.com>
Wed, 4 May 2022 13:38:01 +0000 (07:38 -0600)
committerGitHub <noreply@github.com>
Wed, 4 May 2022 13:38:01 +0000 (07:38 -0600)
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>
Misc/NEWS.d/next/Library/2022-05-03-17-33-46.gh-issue-92210.csDOQM.rst [new file with mode: 0644]
Modules/clinic/socketmodule.c.h [new file with mode: 0644]
Modules/socketmodule.c

diff --git a/Misc/NEWS.d/next/Library/2022-05-03-17-33-46.gh-issue-92210.csDOQM.rst b/Misc/NEWS.d/next/Library/2022-05-03-17-33-46.gh-issue-92210.csDOQM.rst
new file mode 100644 (file)
index 0000000..13f8197
--- /dev/null
@@ -0,0 +1 @@
+Port ``socket.__init__`` to Argument Clinic. Patch by Cinder.
diff --git a/Modules/clinic/socketmodule.c.h b/Modules/clinic/socketmodule.c.h
new file mode 100644 (file)
index 0000000..dab2b6d
--- /dev/null
@@ -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]*/
index bedb8bb423ceec82d1b82355d253cc4d3444728a..9aefa443c7c694745a073578a7b623b56286ccc5 100644 (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;
     }