Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ccfb91c

Browse files
committed
fix issue #8866: parameters passed to socket.getaddrinfo can now be specified as single keyword arguments.
1 parent 67b21b7 commit ccfb91c

5 files changed

Lines changed: 44 additions & 10 deletions

File tree

Doc/library/socket.rst

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ The module :mod:`socket` exports the following constants and functions:
214214
*source_address* was added.
215215

216216

217-
.. function:: getaddrinfo(host, port, family=0, socktype=0, proto=0, flags=0)
217+
.. function:: getaddrinfo(host, port, family=0, type=0, proto=0, flags=0)
218218

219219
Translate the *host*/*port* argument into a sequence of 5-tuples that contain
220220
all the necessary arguments for creating a socket connected to that service.
@@ -223,7 +223,7 @@ The module :mod:`socket` exports the following constants and functions:
223223
port number or ``None``. By passing ``None`` as the value of *host*
224224
and *port*, you can pass ``NULL`` to the underlying C API.
225225

226-
The *family*, *socktype* and *proto* arguments can be optionally specified
226+
The *family*, *type* and *proto* arguments can be optionally specified
227227
in order to narrow the list of addresses returned. Passing zero as a
228228
value for each of these arguments selects the full range of results.
229229
The *flags* argument can be one or several of the ``AI_*`` constants,
@@ -233,9 +233,9 @@ The module :mod:`socket` exports the following constants and functions:
233233

234234
The function returns a list of 5-tuples with the following structure:
235235

236-
``(family, socktype, proto, canonname, sockaddr)``
236+
``(family, type, proto, canonname, sockaddr)``
237237

238-
In these tuples, *family*, *socktype*, *proto* are all integers and are
238+
In these tuples, *family*, *type*, *proto* are all integers and are
239239
meant to be passed to the :func:`socket` function. *canonname* will be
240240
a string representing the canonical name of the *host* if
241241
:const:`AI_CANONNAME` is part of the *flags* argument; else *canonname*
@@ -249,10 +249,13 @@ The module :mod:`socket` exports the following constants and functions:
249249
connection to ``www.python.org`` on port 80 (results may differ on your
250250
system if IPv6 isn't enabled)::
251251

252-
>>> socket.getaddrinfo("www.python.org", 80, 0, 0, socket.SOL_TCP)
252+
>>> socket.getaddrinfo("www.python.org", 80, proto=socket.SOL_TCP)
253253
[(2, 1, 6, '', ('82.94.164.162', 80)),
254254
(10, 1, 6, '', ('2001:888:2000:d::a2', 80, 0, 0))]
255255

256+
.. versionchanged:: 3.2
257+
parameters can now be passed as single keyword arguments.
258+
256259
.. function:: getfqdn([name])
257260

258261
Return a fully qualified domain name for *name*. If *name* is omitted or empty,

Doc/whatsnew/3.2.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@ New, Improved, and Deprecated Modules
181181

182182
(Contributed by Georg Brandl; :issue:`5675`.)
183183

184+
* Parameters passed to :func:`socket.getaddrinfo()` function can now be
185+
specified as single keyword arguments.
186+
187+
(Contributed by Giampaolo Rodolà; :issue:`8866`.)
184188

185189
Multi-threading
186190
===============

Lib/test/test_socket.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,28 @@ def testGetaddrinfo(self):
614614
# usually do this
615615
socket.getaddrinfo(None, 0, socket.AF_UNSPEC, socket.SOCK_STREAM, 0,
616616
socket.AI_PASSIVE)
617+
# test keyword arguments
618+
a = socket.getaddrinfo(HOST, None)
619+
b = socket.getaddrinfo(host=HOST, port=None)
620+
self.assertEqual(a, b)
621+
a = socket.getaddrinfo(HOST, None, socket.AF_INET)
622+
b = socket.getaddrinfo(HOST, None, family=socket.AF_INET)
623+
self.assertEqual(a, b)
624+
a = socket.getaddrinfo(HOST, None, 0, socket.SOCK_STREAM)
625+
b = socket.getaddrinfo(HOST, None, type=socket.SOCK_STREAM)
626+
self.assertEqual(a, b)
627+
a = socket.getaddrinfo(HOST, None, 0, 0, socket.SOL_TCP)
628+
b = socket.getaddrinfo(HOST, None, proto=socket.SOL_TCP)
629+
self.assertEqual(a, b)
630+
a = socket.getaddrinfo(HOST, None, 0, 0, 0, socket.AI_PASSIVE)
631+
b = socket.getaddrinfo(HOST, None, flags=socket.AI_PASSIVE)
632+
self.assertEqual(a, b)
633+
a = socket.getaddrinfo(None, 0, socket.AF_UNSPEC, socket.SOCK_STREAM, 0,
634+
socket.AI_PASSIVE)
635+
b = socket.getaddrinfo(host=None, port=0, family=socket.AF_UNSPEC,
636+
type=socket.SOCK_STREAM, proto=0,
637+
flags=socket.AI_PASSIVE)
638+
self.assertEqual(a, b)
617639

618640

619641
@unittest.skipUnless(thread, 'Threading required for this test.')

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ Extensions
9393
Library
9494
-------
9595

96+
- Issue #8866: parameters passed to socket.getaddrinfo can now be specified as
97+
single keyword arguments.
98+
9699
- Address XXX comment in dis.py by having inspect.py prefer to reuse the
97100
dis.py compiler flag values over defining its own
98101

Modules/socketmodule.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3822,8 +3822,10 @@ socket_inet_ntop(PyObject *self, PyObject *args)
38223822

38233823
/*ARGSUSED*/
38243824
static PyObject *
3825-
socket_getaddrinfo(PyObject *self, PyObject *args)
3825+
socket_getaddrinfo(PyObject *self, PyObject *args, PyObject* kwargs)
38263826
{
3827+
static char* kwnames[] = {"host", "port", "family", "type", "proto",
3828+
"flags", 0};
38273829
struct addrinfo hints, *res;
38283830
struct addrinfo *res0 = NULL;
38293831
PyObject *hobj = NULL;
@@ -3837,8 +3839,8 @@ socket_getaddrinfo(PyObject *self, PyObject *args)
38373839

38383840
family = socktype = protocol = flags = 0;
38393841
family = AF_UNSPEC;
3840-
if (!PyArg_ParseTuple(args, "OO|iiii:getaddrinfo",
3841-
&hobj, &pobj, &family, &socktype,
3842+
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|iiii:getaddrinfo",
3843+
kwnames, &hobj, &pobj, &family, &socktype,
38423844
&protocol, &flags)) {
38433845
return NULL;
38443846
}
@@ -4105,8 +4107,8 @@ static PyMethodDef socket_methods[] = {
41054107
{"inet_ntop", socket_inet_ntop,
41064108
METH_VARARGS, inet_ntop_doc},
41074109
#endif
4108-
{"getaddrinfo", socket_getaddrinfo,
4109-
METH_VARARGS, getaddrinfo_doc},
4110+
{"getaddrinfo", (PyCFunction)socket_getaddrinfo,
4111+
METH_VARARGS | METH_KEYWORDS, getaddrinfo_doc},
41104112
{"getnameinfo", socket_getnameinfo,
41114113
METH_VARARGS, getnameinfo_doc},
41124114
{"getdefaulttimeout", (PyCFunction)socket_getdefaulttimeout,

0 commit comments

Comments
 (0)