]> granicus.if.org Git - python/commitdiff
fix issue #8866: parameters passed to socket.getaddrinfo can now be specified as...
authorGiampaolo Rodolà <g.rodola@gmail.com>
Tue, 17 Aug 2010 15:30:23 +0000 (15:30 +0000)
committerGiampaolo Rodolà <g.rodola@gmail.com>
Tue, 17 Aug 2010 15:30:23 +0000 (15:30 +0000)
Doc/library/socket.rst
Doc/whatsnew/3.2.rst
Lib/test/test_socket.py
Misc/NEWS
Modules/socketmodule.c

index f340920222edbfb448104f7071f55dc48c02b9f9..581756fe671e970daa0585a50dff496dc5bad0a4 100644 (file)
@@ -214,7 +214,7 @@ The module :mod:`socket` exports the following constants and functions:
       *source_address* was added.
 
 
-.. function:: getaddrinfo(host, port, family=0, socktype=0, proto=0, flags=0)
+.. function:: getaddrinfo(host, port, family=0, type=0, proto=0, flags=0)
 
    Translate the *host*/*port* argument into a sequence of 5-tuples that contain
    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:
    port number or ``None``.  By passing ``None`` as the value of *host*
    and *port*, you can pass ``NULL`` to the underlying C API.
 
-   The *family*, *socktype* and *proto* arguments can be optionally specified
+   The *family*, *type* and *proto* arguments can be optionally specified
    in order to narrow the list of addresses returned.  Passing zero as a
    value for each of these arguments selects the full range of results.
    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:
 
    The function returns a list of 5-tuples with the following structure:
 
-   ``(family, socktype, proto, canonname, sockaddr)``
+   ``(family, type, proto, canonname, sockaddr)``
 
-   In these tuples, *family*, *socktype*, *proto* are all integers and are
+   In these tuples, *family*, *type*, *proto* are all integers and are
    meant to be passed to the :func:`socket` function.  *canonname* will be
    a string representing the canonical name of the *host* if
    :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:
    connection to ``www.python.org`` on port 80 (results may differ on your
    system if IPv6 isn't enabled)::
 
-      >>> socket.getaddrinfo("www.python.org", 80, 0, 0, socket.SOL_TCP)
+      >>> socket.getaddrinfo("www.python.org", 80, proto=socket.SOL_TCP)
       [(2, 1, 6, '', ('82.94.164.162', 80)),
        (10, 1, 6, '', ('2001:888:2000:d::a2', 80, 0, 0))]
 
+   .. versionchanged:: 3.2
+      parameters can now be passed as single keyword arguments.
+
 .. function:: getfqdn([name])
 
    Return a fully qualified domain name for *name*. If *name* is omitted or empty,
index 2c625a85ab1dab21fcea5f38bca097327e63e8c3..d03a67fd6a599036598266b5d78b9fd33a7a9e1b 100644 (file)
@@ -181,6 +181,10 @@ New, Improved, and Deprecated Modules
 
   (Contributed by Georg Brandl; :issue:`5675`.)
 
+* Parameters passed to :func:`socket.getaddrinfo()` function can now be
+  specified as single keyword arguments.
+
+  (Contributed by Giampaolo Rodolà; :issue:`8866`.)
 
 Multi-threading
 ===============
index 48ba5f0ad6b9722c754c1748d2d6e16ad7601dc1..02c99b402dab907040f5e93769edc6b92c47d348 100644 (file)
@@ -614,6 +614,28 @@ class GeneralModuleTests(unittest.TestCase):
         # usually do this
         socket.getaddrinfo(None, 0, socket.AF_UNSPEC, socket.SOCK_STREAM, 0,
                            socket.AI_PASSIVE)
+        # test keyword arguments
+        a = socket.getaddrinfo(HOST, None)
+        b = socket.getaddrinfo(host=HOST, port=None)
+        self.assertEqual(a, b)
+        a = socket.getaddrinfo(HOST, None, socket.AF_INET)
+        b = socket.getaddrinfo(HOST, None, family=socket.AF_INET)
+        self.assertEqual(a, b)
+        a = socket.getaddrinfo(HOST, None, 0, socket.SOCK_STREAM)
+        b = socket.getaddrinfo(HOST, None, type=socket.SOCK_STREAM)
+        self.assertEqual(a, b)
+        a = socket.getaddrinfo(HOST, None, 0, 0, socket.SOL_TCP)
+        b = socket.getaddrinfo(HOST, None, proto=socket.SOL_TCP)
+        self.assertEqual(a, b)
+        a = socket.getaddrinfo(HOST, None, 0, 0, 0, socket.AI_PASSIVE)
+        b = socket.getaddrinfo(HOST, None, flags=socket.AI_PASSIVE)
+        self.assertEqual(a, b)
+        a = socket.getaddrinfo(None, 0, socket.AF_UNSPEC, socket.SOCK_STREAM, 0,
+                               socket.AI_PASSIVE)
+        b = socket.getaddrinfo(host=None, port=0, family=socket.AF_UNSPEC,
+                               type=socket.SOCK_STREAM, proto=0,
+                               flags=socket.AI_PASSIVE)
+        self.assertEqual(a, b)
 
 
 @unittest.skipUnless(thread, 'Threading required for this test.')
index 4b52c2b62de98431321cb73ef5540c6745657f0b..8e68e71d0e685d6feba58e46a9f9be98b88b6113 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -93,6 +93,9 @@ Extensions
 Library
 -------
 
+- Issue #8866: parameters passed to socket.getaddrinfo can now be specified as
+  single keyword arguments.
+
 - Address XXX comment in dis.py by having inspect.py prefer to reuse the
   dis.py compiler flag values over defining its own
 
index b1a616e2be68d6f818b30e20fdf8470ef14cd8ab..ea546da7036e53001706bcda26de30e8fa1ca1b0 100644 (file)
@@ -3822,8 +3822,10 @@ socket_inet_ntop(PyObject *self, PyObject *args)
 
 /*ARGSUSED*/
 static PyObject *
-socket_getaddrinfo(PyObject *self, PyObject *args)
+socket_getaddrinfo(PyObject *self, PyObject *args, PyObject* kwargs)
 {
+    static char* kwnames[] = {"host", "port", "family", "type", "proto", 
+                              "flags", 0};
     struct addrinfo hints, *res;
     struct addrinfo *res0 = NULL;
     PyObject *hobj = NULL;
@@ -3837,8 +3839,8 @@ socket_getaddrinfo(PyObject *self, PyObject *args)
 
     family = socktype = protocol = flags = 0;
     family = AF_UNSPEC;
-    if (!PyArg_ParseTuple(args, "OO|iiii:getaddrinfo",
-                          &hobj, &pobj, &family, &socktype,
+    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|iiii:getaddrinfo", 
+                          kwnames, &hobj, &pobj, &family, &socktype,
                           &protocol, &flags)) {
         return NULL;
     }
@@ -4105,8 +4107,8 @@ static PyMethodDef socket_methods[] = {
     {"inet_ntop",               socket_inet_ntop,
      METH_VARARGS, inet_ntop_doc},
 #endif
-    {"getaddrinfo",             socket_getaddrinfo,
-     METH_VARARGS, getaddrinfo_doc},
+    {"getaddrinfo",             (PyCFunction)socket_getaddrinfo,
+     METH_VARARGS | METH_KEYWORDS, getaddrinfo_doc},
     {"getnameinfo",             socket_getnameinfo,
      METH_VARARGS, getnameinfo_doc},
     {"getdefaulttimeout",       (PyCFunction)socket_getdefaulttimeout,