From 896060ab8412667889787ad2bb373a7b8a428fb8 Mon Sep 17 00:00:00 2001 From: Bogdan Romanyuk Date: Thu, 2 Nov 2023 23:28:22 +0300 Subject: [PATCH 01/14] change calling convention of socket.htons --- Modules/socketmodule.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index 1cf6fa4a8936e9..02be2972b17b08 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -6396,11 +6396,19 @@ Convert a 32-bit integer from network to host byte order."); static PyObject * -socket_htons(PyObject *self, PyObject *args) +socket_htons(PyObject *self, PyObject *arg) { int x; - if (!PyArg_ParseTuple(args, "i:htons", &x)) { + if (!PyLong_Check(arg)) { + return PyErr_Format(PyExc_TypeError, + "expected int, %s found", + Py_TYPE(arg)->tp_name); + } + + x = PyLong_AsInt(arg); + + if (x == -1 && PyErr_Occurred()) { return NULL; } if (x < 0) { @@ -7220,7 +7228,7 @@ static PyMethodDef socket_methods[] = { {"ntohl", socket_ntohl, METH_O, ntohl_doc}, {"htons", socket_htons, - METH_VARARGS, htons_doc}, + METH_O, htons_doc}, {"htonl", socket_htonl, METH_O, htonl_doc}, {"inet_aton", socket_inet_aton, From a06b91dadaf0c28ddc10ce5bd0237f2080aaa453 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Thu, 2 Nov 2023 20:43:19 +0000 Subject: [PATCH 02/14] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20b?= =?UTF-8?q?lurb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2023-11-02-20-43-17.gh-issue-111662.OKk1RM.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2023-11-02-20-43-17.gh-issue-111662.OKk1RM.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-11-02-20-43-17.gh-issue-111662.OKk1RM.rst b/Misc/NEWS.d/next/Core and Builtins/2023-11-02-20-43-17.gh-issue-111662.OKk1RM.rst new file mode 100644 index 00000000000000..5ad16c454f52c9 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2023-11-02-20-43-17.gh-issue-111662.OKk1RM.rst @@ -0,0 +1 @@ +Speed up the socket.htons function From 26ebcea71cca71b922a613f1b61622a08184af58 Mon Sep 17 00:00:00 2001 From: Bogdan Romanyuk <65823030+wrongnull@users.noreply.github.com> Date: Fri, 3 Nov 2023 09:06:32 +0300 Subject: [PATCH 03/14] delete news --- .../2023-11-02-20-43-17.gh-issue-111662.OKk1RM.rst | 1 - 1 file changed, 1 deletion(-) delete mode 100644 Misc/NEWS.d/next/Core and Builtins/2023-11-02-20-43-17.gh-issue-111662.OKk1RM.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-11-02-20-43-17.gh-issue-111662.OKk1RM.rst b/Misc/NEWS.d/next/Core and Builtins/2023-11-02-20-43-17.gh-issue-111662.OKk1RM.rst deleted file mode 100644 index 5ad16c454f52c9..00000000000000 --- a/Misc/NEWS.d/next/Core and Builtins/2023-11-02-20-43-17.gh-issue-111662.OKk1RM.rst +++ /dev/null @@ -1 +0,0 @@ -Speed up the socket.htons function From 0f3ca4740e3604fd9847ef1ccf04a2d6602b4394 Mon Sep 17 00:00:00 2001 From: Bogdan Romanyuk Date: Fri, 3 Nov 2023 18:19:07 +0300 Subject: [PATCH 04/14] clinic argument for socket.htons --- Modules/clinic/socketmodule.c.h | 30 +++++++++++++++++++++++++++++- Modules/socketmodule.c | 32 +++++++++++--------------------- 2 files changed, 40 insertions(+), 22 deletions(-) diff --git a/Modules/clinic/socketmodule.c.h b/Modules/clinic/socketmodule.c.h index b6c74b1e9befd2..acdd8873d46baa 100644 --- a/Modules/clinic/socketmodule.c.h +++ b/Modules/clinic/socketmodule.c.h @@ -91,4 +91,32 @@ sock_initobj(PyObject *self, PyObject *args, PyObject *kwargs) exit: return return_value; } -/*[clinic end generated code: output=c85517815c2d69cf input=a9049054013a1b77]*/ + +PyDoc_STRVAR(_socket_socket_htons__doc__, +"htons($self, x, /)\n" +"--\n" +"\n" +"Convert a 16-bit unsigned integer from host to network byte order."); + +#define _SOCKET_SOCKET_HTONS_METHODDEF \ + {"htons", (PyCFunction)_socket_socket_htons, METH_O, _socket_socket_htons__doc__}, + +static PyObject * +_socket_socket_htons_impl(PySocketSockObject *self, int x); + +static PyObject * +_socket_socket_htons(PySocketSockObject *self, PyObject *arg) +{ + PyObject *return_value = NULL; + int x; + + x = PyLong_AsInt(arg); + if (x == -1 && PyErr_Occurred()) { + goto exit; + } + return_value = _socket_socket_htons_impl(self, x); + +exit: + return return_value; +} +/*[clinic end generated code: output=32bb95d366735f37 input=a9049054013a1b77]*/ diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index 02be2972b17b08..f3a00a2a9a9951 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -6395,22 +6395,18 @@ PyDoc_STRVAR(ntohl_doc, Convert a 32-bit integer from network to host byte order."); -static PyObject * -socket_htons(PyObject *self, PyObject *arg) -{ - int x; - - if (!PyLong_Check(arg)) { - return PyErr_Format(PyExc_TypeError, - "expected int, %s found", - Py_TYPE(arg)->tp_name); - } +/*[clinic input] +_socket.socket.htons + x: int + / - x = PyLong_AsInt(arg); +Convert a 16-bit unsigned integer from host to network byte order. +[clinic start generated code]*/ - if (x == -1 && PyErr_Occurred()) { - return NULL; - } +static PyObject * +_socket_socket_htons_impl(PySocketSockObject *self, int x) +/*[clinic end generated code: output=d785ee692312da47 input=053252d8416f4337]*/ +{ if (x < 0) { PyErr_SetString(PyExc_OverflowError, "htons: can't convert negative Python int to C " @@ -6426,11 +6422,6 @@ socket_htons(PyObject *self, PyObject *arg) return PyLong_FromUnsignedLong(htons((unsigned short)x)); } -PyDoc_STRVAR(htons_doc, -"htons(integer) -> integer\n\ -\n\ -Convert a 16-bit unsigned integer from host to network byte order."); - static PyObject * socket_htonl(PyObject *self, PyObject *arg) @@ -7227,8 +7218,7 @@ static PyMethodDef socket_methods[] = { METH_VARARGS, ntohs_doc}, {"ntohl", socket_ntohl, METH_O, ntohl_doc}, - {"htons", socket_htons, - METH_O, htons_doc}, + _SOCKET_SOCKET_HTONS_METHODDEF {"htonl", socket_htonl, METH_O, htonl_doc}, {"inet_aton", socket_inet_aton, From d92ddd327d7691422205d0be7bbcd79e48c9adcb Mon Sep 17 00:00:00 2001 From: Bogdan Romanyuk Date: Fri, 3 Nov 2023 19:07:17 +0300 Subject: [PATCH 05/14] clinic argument for socket.ntohs --- Modules/clinic/socketmodule.c.h | 30 +++++++++++++++++++++++++++++- Modules/socketmodule.c | 24 +++++++++++------------- 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/Modules/clinic/socketmodule.c.h b/Modules/clinic/socketmodule.c.h index acdd8873d46baa..a1259eafcb4775 100644 --- a/Modules/clinic/socketmodule.c.h +++ b/Modules/clinic/socketmodule.c.h @@ -92,6 +92,34 @@ sock_initobj(PyObject *self, PyObject *args, PyObject *kwargs) return return_value; } +PyDoc_STRVAR(_socket_socket_ntohs__doc__, +"ntohs($self, x, /)\n" +"--\n" +"\n" +"Convert a 16-bit unsigned integer from network to host byte order."); + +#define _SOCKET_SOCKET_NTOHS_METHODDEF \ + {"ntohs", (PyCFunction)_socket_socket_ntohs, METH_O, _socket_socket_ntohs__doc__}, + +static PyObject * +_socket_socket_ntohs_impl(PySocketSockObject *self, int x); + +static PyObject * +_socket_socket_ntohs(PySocketSockObject *self, PyObject *arg) +{ + PyObject *return_value = NULL; + int x; + + x = PyLong_AsInt(arg); + if (x == -1 && PyErr_Occurred()) { + goto exit; + } + return_value = _socket_socket_ntohs_impl(self, x); + +exit: + return return_value; +} + PyDoc_STRVAR(_socket_socket_htons__doc__, "htons($self, x, /)\n" "--\n" @@ -119,4 +147,4 @@ _socket_socket_htons(PySocketSockObject *self, PyObject *arg) exit: return return_value; } -/*[clinic end generated code: output=32bb95d366735f37 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=da4ea7017df2e254 input=a9049054013a1b77]*/ diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index f3a00a2a9a9951..3f861e735f97b2 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -6332,14 +6332,18 @@ AF_UNIX if defined on the platform; otherwise, the default is AF_INET."); #endif /* HAVE_SOCKETPAIR */ +/*[clinic input] +_socket.socket.ntohs + x: int + / + +Convert a 16-bit unsigned integer from network to host byte order. +[clinic start generated code]*/ + static PyObject * -socket_ntohs(PyObject *self, PyObject *args) +_socket_socket_ntohs_impl(PySocketSockObject *self, int x) +/*[clinic end generated code: output=a828a61a9fb205b2 input=9a79cb3a71652147]*/ { - int x; - - if (!PyArg_ParseTuple(args, "i:ntohs", &x)) { - return NULL; - } if (x < 0) { PyErr_SetString(PyExc_OverflowError, "ntohs: can't convert negative Python int to C " @@ -6355,11 +6359,6 @@ socket_ntohs(PyObject *self, PyObject *args) return PyLong_FromUnsignedLong(ntohs((unsigned short)x)); } -PyDoc_STRVAR(ntohs_doc, -"ntohs(integer) -> integer\n\ -\n\ -Convert a 16-bit unsigned integer from network to host byte order."); - static PyObject * socket_ntohl(PyObject *self, PyObject *arg) @@ -7214,8 +7213,7 @@ static PyMethodDef socket_methods[] = { {"socketpair", socket_socketpair, METH_VARARGS, socketpair_doc}, #endif - {"ntohs", socket_ntohs, - METH_VARARGS, ntohs_doc}, + _SOCKET_SOCKET_NTOHS_METHODDEF {"ntohl", socket_ntohl, METH_O, ntohl_doc}, _SOCKET_SOCKET_HTONS_METHODDEF From 144f61cb4f6250b1310494de257fcb669c83b1c8 Mon Sep 17 00:00:00 2001 From: Bogdan Romanyuk Date: Sat, 4 Nov 2023 02:14:39 +0300 Subject: [PATCH 06/14] clinic argument for socket.inet_aton --- Modules/clinic/socketmodule.c.h | 34 ++++++++++++++++++++++++++++++++- Modules/socketmodule.c | 25 +++++++++++------------- 2 files changed, 44 insertions(+), 15 deletions(-) diff --git a/Modules/clinic/socketmodule.c.h b/Modules/clinic/socketmodule.c.h index a1259eafcb4775..5f8bb78df8224a 100644 --- a/Modules/clinic/socketmodule.c.h +++ b/Modules/clinic/socketmodule.c.h @@ -147,4 +147,36 @@ _socket_socket_htons(PySocketSockObject *self, PyObject *arg) exit: return return_value; } -/*[clinic end generated code: output=da4ea7017df2e254 input=a9049054013a1b77]*/ + +PyDoc_STRVAR(_socket_socket_inet_aton__doc__, +"inet_aton($self, ip_addr, /)\n" +"--\n" +"\n" +"Convert an IP address in string format (123.45.67.89) to the 32-bit packed binary format used in low-level network functions."); + +#define _SOCKET_SOCKET_INET_ATON_METHODDEF \ + {"inet_aton", (PyCFunction)_socket_socket_inet_aton, METH_O, _socket_socket_inet_aton__doc__}, + +static PyObject * +_socket_socket_inet_aton_impl(PySocketSockObject *self, const char *ip_addr); + +static PyObject * +_socket_socket_inet_aton(PySocketSockObject *self, PyObject *arg) +{ + PyObject *return_value = NULL; + const char *ip_addr; + + if (!PyUnicode_Check(arg)) { + _PyArg_BadArgument("inet_aton", "argument", "str", arg); + goto exit; + } + ip_addr = PyUnicode_AsUTF8(arg); + if (ip_addr == NULL) { + goto exit; + } + return_value = _socket_socket_inet_aton_impl(self, ip_addr); + +exit: + return return_value; +} +/*[clinic end generated code: output=fcda0595da1ee90d input=a9049054013a1b77]*/ diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index 3f861e735f97b2..fbef12285587c7 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -6457,14 +6457,17 @@ Convert a 32-bit integer from host to network byte order."); /* socket.inet_aton() and socket.inet_ntoa() functions. */ -PyDoc_STRVAR(inet_aton_doc, -"inet_aton(string) -> bytes giving packed 32-bit IP representation\n\ -\n\ -Convert an IP address in string format (123.45.67.89) to the 32-bit packed\n\ -binary format used in low-level network functions."); +/*[clinic input] +_socket.socket.inet_aton + ip_addr: str + / -static PyObject* -socket_inet_aton(PyObject *self, PyObject *args) +Convert an IP address in string format (123.45.67.89) to the 32-bit packed binary format used in low-level network functions. +[clinic start generated code]*/ + +static PyObject * +_socket_socket_inet_aton_impl(PySocketSockObject *self, const char *ip_addr) +/*[clinic end generated code: output=5bfe11a255423d8c input=a120e20cb52b9488]*/ { #ifdef HAVE_INET_ATON struct in_addr buf; @@ -6477,11 +6480,6 @@ socket_inet_aton(PyObject *self, PyObject *args) /* Have to use inet_addr() instead */ unsigned int packed_addr; #endif - const char *ip_addr; - - if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr)) - return NULL; - #ifdef HAVE_INET_ATON @@ -7219,8 +7217,7 @@ static PyMethodDef socket_methods[] = { _SOCKET_SOCKET_HTONS_METHODDEF {"htonl", socket_htonl, METH_O, htonl_doc}, - {"inet_aton", socket_inet_aton, - METH_VARARGS, inet_aton_doc}, + _SOCKET_SOCKET_INET_ATON_METHODDEF #ifdef HAVE_INET_NTOA {"inet_ntoa", socket_inet_ntoa, METH_VARARGS, inet_ntoa_doc}, From 6ccd8172777315c248f8159b8d3bf986bbef60ca Mon Sep 17 00:00:00 2001 From: Bogdan Romanyuk Date: Sat, 4 Nov 2023 02:31:25 +0300 Subject: [PATCH 07/14] clinic argument for socket.inet_ntoa --- Modules/clinic/socketmodule.c.h | 42 ++++++++++++++++++++++++++++++++- Modules/socketmodule.c | 32 ++++++++++++------------- 2 files changed, 56 insertions(+), 18 deletions(-) diff --git a/Modules/clinic/socketmodule.c.h b/Modules/clinic/socketmodule.c.h index 5f8bb78df8224a..e3c435cecc1ea9 100644 --- a/Modules/clinic/socketmodule.c.h +++ b/Modules/clinic/socketmodule.c.h @@ -179,4 +179,44 @@ _socket_socket_inet_aton(PySocketSockObject *self, PyObject *arg) exit: return return_value; } -/*[clinic end generated code: output=fcda0595da1ee90d input=a9049054013a1b77]*/ + +#if defined(HAVE_INET_NTOA) + +PyDoc_STRVAR(_socket_socket_inet_ntoa__doc__, +"inet_ntoa($self, packed_ip, /)\n" +"--\n" +"\n" +"Convert an IP address from 32-bit packed binary format to string format."); + +#define _SOCKET_SOCKET_INET_NTOA_METHODDEF \ + {"inet_ntoa", (PyCFunction)_socket_socket_inet_ntoa, METH_O, _socket_socket_inet_ntoa__doc__}, + +static PyObject * +_socket_socket_inet_ntoa_impl(PySocketSockObject *self, Py_buffer *packed_ip); + +static PyObject * +_socket_socket_inet_ntoa(PySocketSockObject *self, PyObject *arg) +{ + PyObject *return_value = NULL; + Py_buffer packed_ip = {NULL, NULL}; + + if (PyObject_GetBuffer(arg, &packed_ip, PyBUF_SIMPLE) != 0) { + goto exit; + } + return_value = _socket_socket_inet_ntoa_impl(self, &packed_ip); + +exit: + /* Cleanup for packed_ip */ + if (packed_ip.obj) { + PyBuffer_Release(&packed_ip); + } + + return return_value; +} + +#endif /* defined(HAVE_INET_NTOA) */ + +#ifndef _SOCKET_SOCKET_INET_NTOA_METHODDEF + #define _SOCKET_SOCKET_INET_NTOA_METHODDEF +#endif /* !defined(_SOCKET_SOCKET_INET_NTOA_METHODDEF) */ +/*[clinic end generated code: output=14b6716b99e1e91f input=a9049054013a1b77]*/ diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index fbef12285587c7..561aaed3d69a68 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -6528,30 +6528,29 @@ _socket_socket_inet_aton_impl(PySocketSockObject *self, const char *ip_addr) } #ifdef HAVE_INET_NTOA -PyDoc_STRVAR(inet_ntoa_doc, -"inet_ntoa(packed_ip) -> ip_address_string\n\ -\n\ -Convert an IP address from 32-bit packed binary format to string format"); +/*[clinic input] +_socket.socket.inet_ntoa + packed_ip: Py_buffer + / -static PyObject* -socket_inet_ntoa(PyObject *self, PyObject *args) +Convert an IP address from 32-bit packed binary format to string format. +[clinic start generated code]*/ + +static PyObject * +_socket_socket_inet_ntoa_impl(PySocketSockObject *self, Py_buffer *packed_ip) +/*[clinic end generated code: output=b671880a3f62461b input=95c2c4a1b2ee957c]*/ { - Py_buffer packed_ip; struct in_addr packed_addr; - if (!PyArg_ParseTuple(args, "y*:inet_ntoa", &packed_ip)) { - return NULL; - } - - if (packed_ip.len != sizeof(packed_addr)) { + if (packed_ip->len != sizeof(packed_addr)) { PyErr_SetString(PyExc_OSError, "packed IP wrong length for inet_ntoa"); - PyBuffer_Release(&packed_ip); + PyBuffer_Release(packed_ip); return NULL; } - memcpy(&packed_addr, packed_ip.buf, packed_ip.len); - PyBuffer_Release(&packed_ip); + memcpy(&packed_addr, packed_ip->buf, packed_ip->len); + PyBuffer_Release(packed_ip); SUPPRESS_DEPRECATED_CALL return PyUnicode_FromString(inet_ntoa(packed_addr)); @@ -7219,8 +7218,7 @@ static PyMethodDef socket_methods[] = { METH_O, htonl_doc}, _SOCKET_SOCKET_INET_ATON_METHODDEF #ifdef HAVE_INET_NTOA - {"inet_ntoa", socket_inet_ntoa, - METH_VARARGS, inet_ntoa_doc}, + _SOCKET_SOCKET_INET_NTOA_METHODDEF #endif #ifdef HAVE_INET_PTON {"inet_pton", socket_inet_pton, From a5331804e70007ec00f75a7d21142cd9f7a8e257 Mon Sep 17 00:00:00 2001 From: Bogdan Romanyuk Date: Sat, 4 Nov 2023 10:30:50 +0300 Subject: [PATCH 08/14] clinic argument for socket.if_nametoindex --- Modules/clinic/socketmodule.c.h | 37 ++++++++++++++++++++++++++++++++- Modules/socketmodule.c | 22 ++++++++++---------- 2 files changed, 47 insertions(+), 12 deletions(-) diff --git a/Modules/clinic/socketmodule.c.h b/Modules/clinic/socketmodule.c.h index e3c435cecc1ea9..35019e7a167119 100644 --- a/Modules/clinic/socketmodule.c.h +++ b/Modules/clinic/socketmodule.c.h @@ -216,7 +216,42 @@ _socket_socket_inet_ntoa(PySocketSockObject *self, PyObject *arg) #endif /* defined(HAVE_INET_NTOA) */ +#if (defined(HAVE_IF_NAMEINDEX) || defined(MS_WINDOWS)) + +PyDoc_STRVAR(_socket_socket_if_nametoindex__doc__, +"if_nametoindex($self, oname, /)\n" +"--\n" +"\n" +"Returns the interface index corresponding to the interface name if_name."); + +#define _SOCKET_SOCKET_IF_NAMETOINDEX_METHODDEF \ + {"if_nametoindex", (PyCFunction)_socket_socket_if_nametoindex, METH_O, _socket_socket_if_nametoindex__doc__}, + +static PyObject * +_socket_socket_if_nametoindex_impl(PySocketSockObject *self, PyObject *oname); + +static PyObject * +_socket_socket_if_nametoindex(PySocketSockObject *self, PyObject *arg) +{ + PyObject *return_value = NULL; + PyObject *oname; + + if (!PyUnicode_FSConverter(arg, &oname)) { + goto exit; + } + return_value = _socket_socket_if_nametoindex_impl(self, oname); + +exit: + return return_value; +} + +#endif /* (defined(HAVE_IF_NAMEINDEX) || defined(MS_WINDOWS)) */ + #ifndef _SOCKET_SOCKET_INET_NTOA_METHODDEF #define _SOCKET_SOCKET_INET_NTOA_METHODDEF #endif /* !defined(_SOCKET_SOCKET_INET_NTOA_METHODDEF) */ -/*[clinic end generated code: output=14b6716b99e1e91f input=a9049054013a1b77]*/ + +#ifndef _SOCKET_SOCKET_IF_NAMETOINDEX_METHODDEF + #define _SOCKET_SOCKET_IF_NAMETOINDEX_METHODDEF +#endif /* !defined(_SOCKET_SOCKET_IF_NAMETOINDEX_METHODDEF) */ +/*[clinic end generated code: output=a6621b09bcb88f6b input=a9049054013a1b77]*/ diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index 561aaed3d69a68..97f4d174666750 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -7044,18 +7044,23 @@ PyDoc_STRVAR(if_nameindex_doc, \n\ Returns a list of network interface information (index, name) tuples."); +/*[clinic input] +_socket.socket.if_nametoindex + oname: object(converter="PyUnicode_FSConverter") + / + +Returns the interface index corresponding to the interface name if_name. +[clinic start generated code]*/ + static PyObject * -socket_if_nametoindex(PyObject *self, PyObject *args) +_socket_socket_if_nametoindex_impl(PySocketSockObject *self, PyObject *oname) +/*[clinic end generated code: output=f7fc00511a309a8e input=662688054482cd46]*/ { - PyObject *oname; #ifdef MS_WINDOWS NET_IFINDEX index; #else unsigned long index; #endif - if (!PyArg_ParseTuple(args, "O&:if_nametoindex", - PyUnicode_FSConverter, &oname)) - return NULL; index = if_nametoindex(PyBytes_AS_STRING(oname)); Py_DECREF(oname); @@ -7068,10 +7073,6 @@ socket_if_nametoindex(PyObject *self, PyObject *args) return PyLong_FromUnsignedLong(index); } -PyDoc_STRVAR(if_nametoindex_doc, -"if_nametoindex(if_name)\n\ -\n\ -Returns the interface index corresponding to the interface name if_name."); static PyObject * socket_if_indextoname(PyObject *self, PyObject *arg) @@ -7241,8 +7242,7 @@ static PyMethodDef socket_methods[] = { #if defined(HAVE_IF_NAMEINDEX) || defined(MS_WINDOWS) {"if_nameindex", socket_if_nameindex, METH_NOARGS, if_nameindex_doc}, - {"if_nametoindex", socket_if_nametoindex, - METH_VARARGS, if_nametoindex_doc}, + _SOCKET_SOCKET_IF_NAMETOINDEX_METHODDEF {"if_indextoname", socket_if_indextoname, METH_O, if_indextoname_doc}, #endif From b1f28f5711376fcb84ccfbb19bbbc7c4dfc27538 Mon Sep 17 00:00:00 2001 From: Bogdan Romanyuk Date: Sat, 4 Nov 2023 10:48:18 +0300 Subject: [PATCH 09/14] clinic argument for socket.ntohl --- Modules/clinic/socketmodule.c.h | 31 ++++++++++++++++++++++++++++++- Modules/socketmodule.c | 30 +++++++++++------------------- 2 files changed, 41 insertions(+), 20 deletions(-) diff --git a/Modules/clinic/socketmodule.c.h b/Modules/clinic/socketmodule.c.h index 35019e7a167119..fe9672961766ca 100644 --- a/Modules/clinic/socketmodule.c.h +++ b/Modules/clinic/socketmodule.c.h @@ -120,6 +120,35 @@ _socket_socket_ntohs(PySocketSockObject *self, PyObject *arg) return return_value; } +PyDoc_STRVAR(_socket_socket_ntohl__doc__, +"ntohl($self, x, /)\n" +"--\n" +"\n" +"Convert a 32-bit integer from network to host byte order."); + +#define _SOCKET_SOCKET_NTOHL_METHODDEF \ + {"ntohl", (PyCFunction)_socket_socket_ntohl, METH_O, _socket_socket_ntohl__doc__}, + +static PyObject * +_socket_socket_ntohl_impl(PySocketSockObject *self, unsigned long x); + +static PyObject * +_socket_socket_ntohl(PySocketSockObject *self, PyObject *arg) +{ + PyObject *return_value = NULL; + unsigned long x; + + if (!PyLong_Check(arg)) { + _PyArg_BadArgument("ntohl", "argument", "int", arg); + goto exit; + } + x = PyLong_AsUnsignedLongMask(arg); + return_value = _socket_socket_ntohl_impl(self, x); + +exit: + return return_value; +} + PyDoc_STRVAR(_socket_socket_htons__doc__, "htons($self, x, /)\n" "--\n" @@ -254,4 +283,4 @@ _socket_socket_if_nametoindex(PySocketSockObject *self, PyObject *arg) #ifndef _SOCKET_SOCKET_IF_NAMETOINDEX_METHODDEF #define _SOCKET_SOCKET_IF_NAMETOINDEX_METHODDEF #endif /* !defined(_SOCKET_SOCKET_IF_NAMETOINDEX_METHODDEF) */ -/*[clinic end generated code: output=a6621b09bcb88f6b input=a9049054013a1b77]*/ +/*[clinic end generated code: output=f71189edaff55d1d input=a9049054013a1b77]*/ diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index 97f4d174666750..26965dc38f4e27 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -6360,15 +6360,18 @@ _socket_socket_ntohs_impl(PySocketSockObject *self, int x) } +/*[clinic input] +_socket.socket.ntohl + x: unsigned_long(bitwise=True) + / + +Convert a 32-bit integer from network to host byte order. +[clinic start generated code]*/ + static PyObject * -socket_ntohl(PyObject *self, PyObject *arg) +_socket_socket_ntohl_impl(PySocketSockObject *self, unsigned long x) +/*[clinic end generated code: output=88e46cad3e45c65c input=02a9a0c0ad14a079]*/ { - unsigned long x; - - if (PyLong_Check(arg)) { - x = PyLong_AsUnsignedLong(arg); - if (x == (unsigned long) -1 && PyErr_Occurred()) - return NULL; #if SIZEOF_LONG > 4 { unsigned long y; @@ -6380,19 +6383,9 @@ socket_ntohl(PyObject *self, PyObject *arg) x = y; } #endif - } - else - return PyErr_Format(PyExc_TypeError, - "expected int, %s found", - Py_TYPE(arg)->tp_name); return PyLong_FromUnsignedLong(ntohl(x)); } -PyDoc_STRVAR(ntohl_doc, -"ntohl(integer) -> integer\n\ -\n\ -Convert a 32-bit integer from network to host byte order."); - /*[clinic input] _socket.socket.htons @@ -7212,8 +7205,7 @@ static PyMethodDef socket_methods[] = { METH_VARARGS, socketpair_doc}, #endif _SOCKET_SOCKET_NTOHS_METHODDEF - {"ntohl", socket_ntohl, - METH_O, ntohl_doc}, + _SOCKET_SOCKET_NTOHL_METHODDEF _SOCKET_SOCKET_HTONS_METHODDEF {"htonl", socket_htonl, METH_O, htonl_doc}, From 387725b3d8ed6e223eb67bdc2791790528c16d83 Mon Sep 17 00:00:00 2001 From: Bogdan Romanyuk Date: Sat, 4 Nov 2023 11:00:21 +0300 Subject: [PATCH 10/14] clinic argument for socket.htonl --- Modules/clinic/socketmodule.c.h | 31 ++++++++++++++++++++++++++++++- Modules/socketmodule.c | 32 ++++++++++++-------------------- 2 files changed, 42 insertions(+), 21 deletions(-) diff --git a/Modules/clinic/socketmodule.c.h b/Modules/clinic/socketmodule.c.h index fe9672961766ca..d05629fd898749 100644 --- a/Modules/clinic/socketmodule.c.h +++ b/Modules/clinic/socketmodule.c.h @@ -177,6 +177,35 @@ _socket_socket_htons(PySocketSockObject *self, PyObject *arg) return return_value; } +PyDoc_STRVAR(_socket_socket_htonl__doc__, +"htonl($self, x, /)\n" +"--\n" +"\n" +"Convert a 32-bit integer from host to network byte order."); + +#define _SOCKET_SOCKET_HTONL_METHODDEF \ + {"htonl", (PyCFunction)_socket_socket_htonl, METH_O, _socket_socket_htonl__doc__}, + +static PyObject * +_socket_socket_htonl_impl(PySocketSockObject *self, unsigned long x); + +static PyObject * +_socket_socket_htonl(PySocketSockObject *self, PyObject *arg) +{ + PyObject *return_value = NULL; + unsigned long x; + + if (!PyLong_Check(arg)) { + _PyArg_BadArgument("htonl", "argument", "int", arg); + goto exit; + } + x = PyLong_AsUnsignedLongMask(arg); + return_value = _socket_socket_htonl_impl(self, x); + +exit: + return return_value; +} + PyDoc_STRVAR(_socket_socket_inet_aton__doc__, "inet_aton($self, ip_addr, /)\n" "--\n" @@ -283,4 +312,4 @@ _socket_socket_if_nametoindex(PySocketSockObject *self, PyObject *arg) #ifndef _SOCKET_SOCKET_IF_NAMETOINDEX_METHODDEF #define _SOCKET_SOCKET_IF_NAMETOINDEX_METHODDEF #endif /* !defined(_SOCKET_SOCKET_IF_NAMETOINDEX_METHODDEF) */ -/*[clinic end generated code: output=f71189edaff55d1d input=a9049054013a1b77]*/ +/*[clinic end generated code: output=bf08c9f291c95a68 input=a9049054013a1b77]*/ diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index 26965dc38f4e27..5f1ad0752ecf5f 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -6415,15 +6415,18 @@ _socket_socket_htons_impl(PySocketSockObject *self, int x) } +/*[clinic input] +_socket.socket.htonl + x: unsigned_long(bitwise=True) + / + +Convert a 32-bit integer from host to network byte order. +[clinic start generated code]*/ + static PyObject * -socket_htonl(PyObject *self, PyObject *arg) +_socket_socket_htonl_impl(PySocketSockObject *self, unsigned long x) +/*[clinic end generated code: output=04a88551f090913e input=e356f217ac56bef5]*/ { - unsigned long x; - - if (PyLong_Check(arg)) { - x = PyLong_AsUnsignedLong(arg); - if (x == (unsigned long) -1 && PyErr_Occurred()) - return NULL; #if SIZEOF_LONG > 4 { unsigned long y; @@ -6435,19 +6438,9 @@ socket_htonl(PyObject *self, PyObject *arg) x = y; } #endif - } - else - return PyErr_Format(PyExc_TypeError, - "expected int, %s found", - Py_TYPE(arg)->tp_name); - return PyLong_FromUnsignedLong(htonl((unsigned long)x)); + return PyLong_FromUnsignedLong(htonl(x)); } -PyDoc_STRVAR(htonl_doc, -"htonl(integer) -> integer\n\ -\n\ -Convert a 32-bit integer from host to network byte order."); - /* socket.inet_aton() and socket.inet_ntoa() functions. */ /*[clinic input] @@ -7207,8 +7200,7 @@ static PyMethodDef socket_methods[] = { _SOCKET_SOCKET_NTOHS_METHODDEF _SOCKET_SOCKET_NTOHL_METHODDEF _SOCKET_SOCKET_HTONS_METHODDEF - {"htonl", socket_htonl, - METH_O, htonl_doc}, + _SOCKET_SOCKET_HTONL_METHODDEF _SOCKET_SOCKET_INET_ATON_METHODDEF #ifdef HAVE_INET_NTOA _SOCKET_SOCKET_INET_NTOA_METHODDEF From bc6733b691db22587ac852ba2bdb4ca11d61a9ff Mon Sep 17 00:00:00 2001 From: Bogdan Romanyuk Date: Sat, 4 Nov 2023 12:34:45 +0300 Subject: [PATCH 11/14] remove bitwise=True from clinic converter --- Modules/clinic/socketmodule.c.h | 11 ++++------- Modules/socketmodule.c | 8 ++++---- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/Modules/clinic/socketmodule.c.h b/Modules/clinic/socketmodule.c.h index d05629fd898749..ede57871ea996d 100644 --- a/Modules/clinic/socketmodule.c.h +++ b/Modules/clinic/socketmodule.c.h @@ -6,6 +6,7 @@ preserve # include "pycore_gc.h" // PyGC_Head # include "pycore_runtime.h" // _Py_ID() #endif +#include "pycore_long.h" // _PyLong_UnsignedLong_Converter() #include "pycore_modsupport.h" // _PyArg_UnpackKeywords() static int @@ -138,11 +139,9 @@ _socket_socket_ntohl(PySocketSockObject *self, PyObject *arg) PyObject *return_value = NULL; unsigned long x; - if (!PyLong_Check(arg)) { - _PyArg_BadArgument("ntohl", "argument", "int", arg); + if (!_PyLong_UnsignedLong_Converter(arg, &x)) { goto exit; } - x = PyLong_AsUnsignedLongMask(arg); return_value = _socket_socket_ntohl_impl(self, x); exit: @@ -195,11 +194,9 @@ _socket_socket_htonl(PySocketSockObject *self, PyObject *arg) PyObject *return_value = NULL; unsigned long x; - if (!PyLong_Check(arg)) { - _PyArg_BadArgument("htonl", "argument", "int", arg); + if (!_PyLong_UnsignedLong_Converter(arg, &x)) { goto exit; } - x = PyLong_AsUnsignedLongMask(arg); return_value = _socket_socket_htonl_impl(self, x); exit: @@ -312,4 +309,4 @@ _socket_socket_if_nametoindex(PySocketSockObject *self, PyObject *arg) #ifndef _SOCKET_SOCKET_IF_NAMETOINDEX_METHODDEF #define _SOCKET_SOCKET_IF_NAMETOINDEX_METHODDEF #endif /* !defined(_SOCKET_SOCKET_IF_NAMETOINDEX_METHODDEF) */ -/*[clinic end generated code: output=bf08c9f291c95a68 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=f3a97b202b9f3af0 input=a9049054013a1b77]*/ diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index 5f1ad0752ecf5f..77dac566fa9e81 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -6362,7 +6362,7 @@ _socket_socket_ntohs_impl(PySocketSockObject *self, int x) /*[clinic input] _socket.socket.ntohl - x: unsigned_long(bitwise=True) + x: unsigned_long / Convert a 32-bit integer from network to host byte order. @@ -6370,7 +6370,7 @@ Convert a 32-bit integer from network to host byte order. static PyObject * _socket_socket_ntohl_impl(PySocketSockObject *self, unsigned long x) -/*[clinic end generated code: output=88e46cad3e45c65c input=02a9a0c0ad14a079]*/ +/*[clinic end generated code: output=88e46cad3e45c65c input=862d49375611e718]*/ { #if SIZEOF_LONG > 4 { @@ -6417,7 +6417,7 @@ _socket_socket_htons_impl(PySocketSockObject *self, int x) /*[clinic input] _socket.socket.htonl - x: unsigned_long(bitwise=True) + x: unsigned_long / Convert a 32-bit integer from host to network byte order. @@ -6425,7 +6425,7 @@ Convert a 32-bit integer from host to network byte order. static PyObject * _socket_socket_htonl_impl(PySocketSockObject *self, unsigned long x) -/*[clinic end generated code: output=04a88551f090913e input=e356f217ac56bef5]*/ +/*[clinic end generated code: output=04a88551f090913e input=0b263bade5b54efc]*/ { #if SIZEOF_LONG > 4 { From a27a86d587d2768c833ed53f4b205ec6354ee4f2 Mon Sep 17 00:00:00 2001 From: Bogdan Romanyuk Date: Mon, 6 Nov 2023 21:39:52 +0300 Subject: [PATCH 12/14] Revert "remove bitwise=True from clinic converter" This reverts commit bc6733b691db22587ac852ba2bdb4ca11d61a9ff. --- Modules/clinic/socketmodule.c.h | 11 +++++++---- Modules/socketmodule.c | 8 ++++---- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/Modules/clinic/socketmodule.c.h b/Modules/clinic/socketmodule.c.h index ede57871ea996d..d05629fd898749 100644 --- a/Modules/clinic/socketmodule.c.h +++ b/Modules/clinic/socketmodule.c.h @@ -6,7 +6,6 @@ preserve # include "pycore_gc.h" // PyGC_Head # include "pycore_runtime.h" // _Py_ID() #endif -#include "pycore_long.h" // _PyLong_UnsignedLong_Converter() #include "pycore_modsupport.h" // _PyArg_UnpackKeywords() static int @@ -139,9 +138,11 @@ _socket_socket_ntohl(PySocketSockObject *self, PyObject *arg) PyObject *return_value = NULL; unsigned long x; - if (!_PyLong_UnsignedLong_Converter(arg, &x)) { + if (!PyLong_Check(arg)) { + _PyArg_BadArgument("ntohl", "argument", "int", arg); goto exit; } + x = PyLong_AsUnsignedLongMask(arg); return_value = _socket_socket_ntohl_impl(self, x); exit: @@ -194,9 +195,11 @@ _socket_socket_htonl(PySocketSockObject *self, PyObject *arg) PyObject *return_value = NULL; unsigned long x; - if (!_PyLong_UnsignedLong_Converter(arg, &x)) { + if (!PyLong_Check(arg)) { + _PyArg_BadArgument("htonl", "argument", "int", arg); goto exit; } + x = PyLong_AsUnsignedLongMask(arg); return_value = _socket_socket_htonl_impl(self, x); exit: @@ -309,4 +312,4 @@ _socket_socket_if_nametoindex(PySocketSockObject *self, PyObject *arg) #ifndef _SOCKET_SOCKET_IF_NAMETOINDEX_METHODDEF #define _SOCKET_SOCKET_IF_NAMETOINDEX_METHODDEF #endif /* !defined(_SOCKET_SOCKET_IF_NAMETOINDEX_METHODDEF) */ -/*[clinic end generated code: output=f3a97b202b9f3af0 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=bf08c9f291c95a68 input=a9049054013a1b77]*/ diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index 77dac566fa9e81..5f1ad0752ecf5f 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -6362,7 +6362,7 @@ _socket_socket_ntohs_impl(PySocketSockObject *self, int x) /*[clinic input] _socket.socket.ntohl - x: unsigned_long + x: unsigned_long(bitwise=True) / Convert a 32-bit integer from network to host byte order. @@ -6370,7 +6370,7 @@ Convert a 32-bit integer from network to host byte order. static PyObject * _socket_socket_ntohl_impl(PySocketSockObject *self, unsigned long x) -/*[clinic end generated code: output=88e46cad3e45c65c input=862d49375611e718]*/ +/*[clinic end generated code: output=88e46cad3e45c65c input=02a9a0c0ad14a079]*/ { #if SIZEOF_LONG > 4 { @@ -6417,7 +6417,7 @@ _socket_socket_htons_impl(PySocketSockObject *self, int x) /*[clinic input] _socket.socket.htonl - x: unsigned_long + x: unsigned_long(bitwise=True) / Convert a 32-bit integer from host to network byte order. @@ -6425,7 +6425,7 @@ Convert a 32-bit integer from host to network byte order. static PyObject * _socket_socket_htonl_impl(PySocketSockObject *self, unsigned long x) -/*[clinic end generated code: output=04a88551f090913e input=0b263bade5b54efc]*/ +/*[clinic end generated code: output=04a88551f090913e input=e356f217ac56bef5]*/ { #if SIZEOF_LONG > 4 { From c62d4e3c93c14f725892c6ceaa111ab1ee390b0f Mon Sep 17 00:00:00 2001 From: Bogdan Romanyuk Date: Mon, 6 Nov 2023 21:41:49 +0300 Subject: [PATCH 13/14] Revert "clinic argument for socket.htonl" This reverts commit 387725b3d8ed6e223eb67bdc2791790528c16d83. --- Modules/clinic/socketmodule.c.h | 31 +------------------------------ Modules/socketmodule.c | 32 ++++++++++++++++++++------------ 2 files changed, 21 insertions(+), 42 deletions(-) diff --git a/Modules/clinic/socketmodule.c.h b/Modules/clinic/socketmodule.c.h index d05629fd898749..fe9672961766ca 100644 --- a/Modules/clinic/socketmodule.c.h +++ b/Modules/clinic/socketmodule.c.h @@ -177,35 +177,6 @@ _socket_socket_htons(PySocketSockObject *self, PyObject *arg) return return_value; } -PyDoc_STRVAR(_socket_socket_htonl__doc__, -"htonl($self, x, /)\n" -"--\n" -"\n" -"Convert a 32-bit integer from host to network byte order."); - -#define _SOCKET_SOCKET_HTONL_METHODDEF \ - {"htonl", (PyCFunction)_socket_socket_htonl, METH_O, _socket_socket_htonl__doc__}, - -static PyObject * -_socket_socket_htonl_impl(PySocketSockObject *self, unsigned long x); - -static PyObject * -_socket_socket_htonl(PySocketSockObject *self, PyObject *arg) -{ - PyObject *return_value = NULL; - unsigned long x; - - if (!PyLong_Check(arg)) { - _PyArg_BadArgument("htonl", "argument", "int", arg); - goto exit; - } - x = PyLong_AsUnsignedLongMask(arg); - return_value = _socket_socket_htonl_impl(self, x); - -exit: - return return_value; -} - PyDoc_STRVAR(_socket_socket_inet_aton__doc__, "inet_aton($self, ip_addr, /)\n" "--\n" @@ -312,4 +283,4 @@ _socket_socket_if_nametoindex(PySocketSockObject *self, PyObject *arg) #ifndef _SOCKET_SOCKET_IF_NAMETOINDEX_METHODDEF #define _SOCKET_SOCKET_IF_NAMETOINDEX_METHODDEF #endif /* !defined(_SOCKET_SOCKET_IF_NAMETOINDEX_METHODDEF) */ -/*[clinic end generated code: output=bf08c9f291c95a68 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=f71189edaff55d1d input=a9049054013a1b77]*/ diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index 5f1ad0752ecf5f..26965dc38f4e27 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -6415,18 +6415,15 @@ _socket_socket_htons_impl(PySocketSockObject *self, int x) } -/*[clinic input] -_socket.socket.htonl - x: unsigned_long(bitwise=True) - / - -Convert a 32-bit integer from host to network byte order. -[clinic start generated code]*/ - static PyObject * -_socket_socket_htonl_impl(PySocketSockObject *self, unsigned long x) -/*[clinic end generated code: output=04a88551f090913e input=e356f217ac56bef5]*/ +socket_htonl(PyObject *self, PyObject *arg) { + unsigned long x; + + if (PyLong_Check(arg)) { + x = PyLong_AsUnsignedLong(arg); + if (x == (unsigned long) -1 && PyErr_Occurred()) + return NULL; #if SIZEOF_LONG > 4 { unsigned long y; @@ -6438,9 +6435,19 @@ _socket_socket_htonl_impl(PySocketSockObject *self, unsigned long x) x = y; } #endif - return PyLong_FromUnsignedLong(htonl(x)); + } + else + return PyErr_Format(PyExc_TypeError, + "expected int, %s found", + Py_TYPE(arg)->tp_name); + return PyLong_FromUnsignedLong(htonl((unsigned long)x)); } +PyDoc_STRVAR(htonl_doc, +"htonl(integer) -> integer\n\ +\n\ +Convert a 32-bit integer from host to network byte order."); + /* socket.inet_aton() and socket.inet_ntoa() functions. */ /*[clinic input] @@ -7200,7 +7207,8 @@ static PyMethodDef socket_methods[] = { _SOCKET_SOCKET_NTOHS_METHODDEF _SOCKET_SOCKET_NTOHL_METHODDEF _SOCKET_SOCKET_HTONS_METHODDEF - _SOCKET_SOCKET_HTONL_METHODDEF + {"htonl", socket_htonl, + METH_O, htonl_doc}, _SOCKET_SOCKET_INET_ATON_METHODDEF #ifdef HAVE_INET_NTOA _SOCKET_SOCKET_INET_NTOA_METHODDEF From e55aac5f01ee8fbfc6bfe834c9acef392961be3d Mon Sep 17 00:00:00 2001 From: Bogdan Romanyuk Date: Mon, 6 Nov 2023 21:42:52 +0300 Subject: [PATCH 14/14] Revert "clinic argument for socket.ntohl" This reverts commit b1f28f5711376fcb84ccfbb19bbbc7c4dfc27538. --- Modules/clinic/socketmodule.c.h | 31 +------------------------------ Modules/socketmodule.c | 30 +++++++++++++++++++----------- 2 files changed, 20 insertions(+), 41 deletions(-) diff --git a/Modules/clinic/socketmodule.c.h b/Modules/clinic/socketmodule.c.h index fe9672961766ca..35019e7a167119 100644 --- a/Modules/clinic/socketmodule.c.h +++ b/Modules/clinic/socketmodule.c.h @@ -120,35 +120,6 @@ _socket_socket_ntohs(PySocketSockObject *self, PyObject *arg) return return_value; } -PyDoc_STRVAR(_socket_socket_ntohl__doc__, -"ntohl($self, x, /)\n" -"--\n" -"\n" -"Convert a 32-bit integer from network to host byte order."); - -#define _SOCKET_SOCKET_NTOHL_METHODDEF \ - {"ntohl", (PyCFunction)_socket_socket_ntohl, METH_O, _socket_socket_ntohl__doc__}, - -static PyObject * -_socket_socket_ntohl_impl(PySocketSockObject *self, unsigned long x); - -static PyObject * -_socket_socket_ntohl(PySocketSockObject *self, PyObject *arg) -{ - PyObject *return_value = NULL; - unsigned long x; - - if (!PyLong_Check(arg)) { - _PyArg_BadArgument("ntohl", "argument", "int", arg); - goto exit; - } - x = PyLong_AsUnsignedLongMask(arg); - return_value = _socket_socket_ntohl_impl(self, x); - -exit: - return return_value; -} - PyDoc_STRVAR(_socket_socket_htons__doc__, "htons($self, x, /)\n" "--\n" @@ -283,4 +254,4 @@ _socket_socket_if_nametoindex(PySocketSockObject *self, PyObject *arg) #ifndef _SOCKET_SOCKET_IF_NAMETOINDEX_METHODDEF #define _SOCKET_SOCKET_IF_NAMETOINDEX_METHODDEF #endif /* !defined(_SOCKET_SOCKET_IF_NAMETOINDEX_METHODDEF) */ -/*[clinic end generated code: output=f71189edaff55d1d input=a9049054013a1b77]*/ +/*[clinic end generated code: output=a6621b09bcb88f6b input=a9049054013a1b77]*/ diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index 26965dc38f4e27..97f4d174666750 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -6360,18 +6360,15 @@ _socket_socket_ntohs_impl(PySocketSockObject *self, int x) } -/*[clinic input] -_socket.socket.ntohl - x: unsigned_long(bitwise=True) - / - -Convert a 32-bit integer from network to host byte order. -[clinic start generated code]*/ - static PyObject * -_socket_socket_ntohl_impl(PySocketSockObject *self, unsigned long x) -/*[clinic end generated code: output=88e46cad3e45c65c input=02a9a0c0ad14a079]*/ +socket_ntohl(PyObject *self, PyObject *arg) { + unsigned long x; + + if (PyLong_Check(arg)) { + x = PyLong_AsUnsignedLong(arg); + if (x == (unsigned long) -1 && PyErr_Occurred()) + return NULL; #if SIZEOF_LONG > 4 { unsigned long y; @@ -6383,9 +6380,19 @@ _socket_socket_ntohl_impl(PySocketSockObject *self, unsigned long x) x = y; } #endif + } + else + return PyErr_Format(PyExc_TypeError, + "expected int, %s found", + Py_TYPE(arg)->tp_name); return PyLong_FromUnsignedLong(ntohl(x)); } +PyDoc_STRVAR(ntohl_doc, +"ntohl(integer) -> integer\n\ +\n\ +Convert a 32-bit integer from network to host byte order."); + /*[clinic input] _socket.socket.htons @@ -7205,7 +7212,8 @@ static PyMethodDef socket_methods[] = { METH_VARARGS, socketpair_doc}, #endif _SOCKET_SOCKET_NTOHS_METHODDEF - _SOCKET_SOCKET_NTOHL_METHODDEF + {"ntohl", socket_ntohl, + METH_O, ntohl_doc}, _SOCKET_SOCKET_HTONS_METHODDEF {"htonl", socket_htonl, METH_O, htonl_doc},