Skip to content

Commit 8fbe531

Browse files
authored
gh-111662: Update socket module to use AC for optimizing performance (gh-111661)
1 parent 06efb60 commit 8fbe531

File tree

2 files changed

+223
-69
lines changed

2 files changed

+223
-69
lines changed

Modules/clinic/socketmodule.c.h

+164-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Modules/socketmodule.c

+59-68
Original file line numberDiff line numberDiff line change
@@ -6332,14 +6332,18 @@ AF_UNIX if defined on the platform; otherwise, the default is AF_INET.");
63326332
#endif /* HAVE_SOCKETPAIR */
63336333

63346334

6335+
/*[clinic input]
6336+
_socket.socket.ntohs
6337+
x: int
6338+
/
6339+
6340+
Convert a 16-bit unsigned integer from network to host byte order.
6341+
[clinic start generated code]*/
6342+
63356343
static PyObject *
6336-
socket_ntohs(PyObject *self, PyObject *args)
6344+
_socket_socket_ntohs_impl(PySocketSockObject *self, int x)
6345+
/*[clinic end generated code: output=a828a61a9fb205b2 input=9a79cb3a71652147]*/
63376346
{
6338-
int x;
6339-
6340-
if (!PyArg_ParseTuple(args, "i:ntohs", &x)) {
6341-
return NULL;
6342-
}
63436347
if (x < 0) {
63446348
PyErr_SetString(PyExc_OverflowError,
63456349
"ntohs: can't convert negative Python int to C "
@@ -6355,11 +6359,6 @@ socket_ntohs(PyObject *self, PyObject *args)
63556359
return PyLong_FromUnsignedLong(ntohs((unsigned short)x));
63566360
}
63576361

6358-
PyDoc_STRVAR(ntohs_doc,
6359-
"ntohs(integer) -> integer\n\
6360-
\n\
6361-
Convert a 16-bit unsigned integer from network to host byte order.");
6362-
63636362

63646363
static PyObject *
63656364
socket_ntohl(PyObject *self, PyObject *arg)
@@ -6395,14 +6394,18 @@ PyDoc_STRVAR(ntohl_doc,
63956394
Convert a 32-bit integer from network to host byte order.");
63966395

63976396

6397+
/*[clinic input]
6398+
_socket.socket.htons
6399+
x: int
6400+
/
6401+
6402+
Convert a 16-bit unsigned integer from host to network byte order.
6403+
[clinic start generated code]*/
6404+
63986405
static PyObject *
6399-
socket_htons(PyObject *self, PyObject *args)
6406+
_socket_socket_htons_impl(PySocketSockObject *self, int x)
6407+
/*[clinic end generated code: output=d785ee692312da47 input=053252d8416f4337]*/
64006408
{
6401-
int x;
6402-
6403-
if (!PyArg_ParseTuple(args, "i:htons", &x)) {
6404-
return NULL;
6405-
}
64066409
if (x < 0) {
64076410
PyErr_SetString(PyExc_OverflowError,
64086411
"htons: can't convert negative Python int to C "
@@ -6418,11 +6421,6 @@ socket_htons(PyObject *self, PyObject *args)
64186421
return PyLong_FromUnsignedLong(htons((unsigned short)x));
64196422
}
64206423

6421-
PyDoc_STRVAR(htons_doc,
6422-
"htons(integer) -> integer\n\
6423-
\n\
6424-
Convert a 16-bit unsigned integer from host to network byte order.");
6425-
64266424

64276425
static PyObject *
64286426
socket_htonl(PyObject *self, PyObject *arg)
@@ -6459,14 +6457,17 @@ Convert a 32-bit integer from host to network byte order.");
64596457

64606458
/* socket.inet_aton() and socket.inet_ntoa() functions. */
64616459

6462-
PyDoc_STRVAR(inet_aton_doc,
6463-
"inet_aton(string) -> bytes giving packed 32-bit IP representation\n\
6464-
\n\
6465-
Convert an IP address in string format (123.45.67.89) to the 32-bit packed\n\
6466-
binary format used in low-level network functions.");
6460+
/*[clinic input]
6461+
_socket.socket.inet_aton
6462+
ip_addr: str
6463+
/
64676464
6468-
static PyObject*
6469-
socket_inet_aton(PyObject *self, PyObject *args)
6465+
Convert an IP address in string format (123.45.67.89) to the 32-bit packed binary format used in low-level network functions.
6466+
[clinic start generated code]*/
6467+
6468+
static PyObject *
6469+
_socket_socket_inet_aton_impl(PySocketSockObject *self, const char *ip_addr)
6470+
/*[clinic end generated code: output=5bfe11a255423d8c input=a120e20cb52b9488]*/
64706471
{
64716472
#ifdef HAVE_INET_ATON
64726473
struct in_addr buf;
@@ -6479,11 +6480,6 @@ socket_inet_aton(PyObject *self, PyObject *args)
64796480
/* Have to use inet_addr() instead */
64806481
unsigned int packed_addr;
64816482
#endif
6482-
const char *ip_addr;
6483-
6484-
if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr))
6485-
return NULL;
6486-
64876483

64886484
#ifdef HAVE_INET_ATON
64896485

@@ -6532,30 +6528,29 @@ socket_inet_aton(PyObject *self, PyObject *args)
65326528
}
65336529

65346530
#ifdef HAVE_INET_NTOA
6535-
PyDoc_STRVAR(inet_ntoa_doc,
6536-
"inet_ntoa(packed_ip) -> ip_address_string\n\
6537-
\n\
6538-
Convert an IP address from 32-bit packed binary format to string format");
6531+
/*[clinic input]
6532+
_socket.socket.inet_ntoa
6533+
packed_ip: Py_buffer
6534+
/
65396535
6540-
static PyObject*
6541-
socket_inet_ntoa(PyObject *self, PyObject *args)
6536+
Convert an IP address from 32-bit packed binary format to string format.
6537+
[clinic start generated code]*/
6538+
6539+
static PyObject *
6540+
_socket_socket_inet_ntoa_impl(PySocketSockObject *self, Py_buffer *packed_ip)
6541+
/*[clinic end generated code: output=b671880a3f62461b input=95c2c4a1b2ee957c]*/
65426542
{
6543-
Py_buffer packed_ip;
65446543
struct in_addr packed_addr;
65456544

6546-
if (!PyArg_ParseTuple(args, "y*:inet_ntoa", &packed_ip)) {
6547-
return NULL;
6548-
}
6549-
6550-
if (packed_ip.len != sizeof(packed_addr)) {
6545+
if (packed_ip->len != sizeof(packed_addr)) {
65516546
PyErr_SetString(PyExc_OSError,
65526547
"packed IP wrong length for inet_ntoa");
6553-
PyBuffer_Release(&packed_ip);
6548+
PyBuffer_Release(packed_ip);
65546549
return NULL;
65556550
}
65566551

6557-
memcpy(&packed_addr, packed_ip.buf, packed_ip.len);
6558-
PyBuffer_Release(&packed_ip);
6552+
memcpy(&packed_addr, packed_ip->buf, packed_ip->len);
6553+
PyBuffer_Release(packed_ip);
65596554

65606555
SUPPRESS_DEPRECATED_CALL
65616556
return PyUnicode_FromString(inet_ntoa(packed_addr));
@@ -7049,18 +7044,23 @@ PyDoc_STRVAR(if_nameindex_doc,
70497044
\n\
70507045
Returns a list of network interface information (index, name) tuples.");
70517046

7047+
/*[clinic input]
7048+
_socket.socket.if_nametoindex
7049+
oname: object(converter="PyUnicode_FSConverter")
7050+
/
7051+
7052+
Returns the interface index corresponding to the interface name if_name.
7053+
[clinic start generated code]*/
7054+
70527055
static PyObject *
7053-
socket_if_nametoindex(PyObject *self, PyObject *args)
7056+
_socket_socket_if_nametoindex_impl(PySocketSockObject *self, PyObject *oname)
7057+
/*[clinic end generated code: output=f7fc00511a309a8e input=662688054482cd46]*/
70547058
{
7055-
PyObject *oname;
70567059
#ifdef MS_WINDOWS
70577060
NET_IFINDEX index;
70587061
#else
70597062
unsigned long index;
70607063
#endif
7061-
if (!PyArg_ParseTuple(args, "O&:if_nametoindex",
7062-
PyUnicode_FSConverter, &oname))
7063-
return NULL;
70647064

70657065
index = if_nametoindex(PyBytes_AS_STRING(oname));
70667066
Py_DECREF(oname);
@@ -7073,10 +7073,6 @@ socket_if_nametoindex(PyObject *self, PyObject *args)
70737073
return PyLong_FromUnsignedLong(index);
70747074
}
70757075

7076-
PyDoc_STRVAR(if_nametoindex_doc,
7077-
"if_nametoindex(if_name)\n\
7078-
\n\
7079-
Returns the interface index corresponding to the interface name if_name.");
70807076

70817077
static PyObject *
70827078
socket_if_indextoname(PyObject *self, PyObject *arg)
@@ -7215,19 +7211,15 @@ static PyMethodDef socket_methods[] = {
72157211
{"socketpair", socket_socketpair,
72167212
METH_VARARGS, socketpair_doc},
72177213
#endif
7218-
{"ntohs", socket_ntohs,
7219-
METH_VARARGS, ntohs_doc},
7214+
_SOCKET_SOCKET_NTOHS_METHODDEF
72207215
{"ntohl", socket_ntohl,
72217216
METH_O, ntohl_doc},
7222-
{"htons", socket_htons,
7223-
METH_VARARGS, htons_doc},
7217+
_SOCKET_SOCKET_HTONS_METHODDEF
72247218
{"htonl", socket_htonl,
72257219
METH_O, htonl_doc},
7226-
{"inet_aton", socket_inet_aton,
7227-
METH_VARARGS, inet_aton_doc},
7220+
_SOCKET_SOCKET_INET_ATON_METHODDEF
72287221
#ifdef HAVE_INET_NTOA
7229-
{"inet_ntoa", socket_inet_ntoa,
7230-
METH_VARARGS, inet_ntoa_doc},
7222+
_SOCKET_SOCKET_INET_NTOA_METHODDEF
72317223
#endif
72327224
#ifdef HAVE_INET_PTON
72337225
{"inet_pton", socket_inet_pton,
@@ -7250,8 +7242,7 @@ static PyMethodDef socket_methods[] = {
72507242
#if defined(HAVE_IF_NAMEINDEX) || defined(MS_WINDOWS)
72517243
{"if_nameindex", socket_if_nameindex,
72527244
METH_NOARGS, if_nameindex_doc},
7253-
{"if_nametoindex", socket_if_nametoindex,
7254-
METH_VARARGS, if_nametoindex_doc},
7245+
_SOCKET_SOCKET_IF_NAMETOINDEX_METHODDEF
72557246
{"if_indextoname", socket_if_indextoname,
72567247
METH_O, if_indextoname_doc},
72577248
#endif

0 commit comments

Comments
 (0)