]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
#8853: Allow port to be of type long for socket.getaddrinfo()
authorPetri Lehtinen <petri@digip.org>
Thu, 20 Dec 2012 19:06:14 +0000 (21:06 +0200)
committerPetri Lehtinen <petri@digip.org>
Thu, 20 Dec 2012 19:09:56 +0000 (21:09 +0200)
Lib/test/test_socket.py
Misc/NEWS
Modules/socketmodule.c

index 7d8b33a2ba4191c6e8e0bcb39c4917394c302f7b..053c6fe7be553843cb580b44fe47008c62f7e0ee 100644 (file)
@@ -644,9 +644,10 @@ class GeneralModuleTests(unittest.TestCase):
         if SUPPORTS_IPV6:
             socket.getaddrinfo('::1', 80)
         # port can be a string service name such as "http", a numeric
-        # port number or None
+        # port number (int or long), or None
         socket.getaddrinfo(HOST, "http")
         socket.getaddrinfo(HOST, 80)
+        socket.getaddrinfo(HOST, 80L)
         socket.getaddrinfo(HOST, None)
         # test family and socktype filters
         infos = socket.getaddrinfo(HOST, None, socket.AF_INET)
index 268e43800de7a3b8652d6bdcb911a5a7dda16c9e..babe8383a27aac0a25d13013ab838089579d4380 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -160,6 +160,8 @@ Core and Builtins
 Library
 -------
 
+- Issue #8853: Allow port to be of type long for socket.getaddrinfo().
+
 - Issue #16597: In buffered and text IO, call close() on the underlying stream
   if invoking flush() fails.
 
index 414994e181fc3d1f94c85d1edb752c9db4345bda..506a2d320b656d6313515b72e72188064c56061f 100644 (file)
@@ -4090,15 +4090,19 @@ socket_getaddrinfo(PyObject *self, PyObject *args)
                         "getaddrinfo() argument 1 must be string or None");
         return NULL;
     }
-    if (PyInt_Check(pobj)) {
-        PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", PyInt_AsLong(pobj));
+    if (PyInt_Check(pobj) || PyLong_Check(pobj)) {
+        long value = PyLong_AsLong(pobj);
+        if (value == -1 && PyErr_Occurred())
+            return NULL;
+        PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", value);
         pptr = pbuf;
     } else if (PyString_Check(pobj)) {
         pptr = PyString_AsString(pobj);
     } else if (pobj == Py_None) {
         pptr = (char *)NULL;
     } else {
-        PyErr_SetString(socket_error, "Int or String expected");
+        PyErr_SetString(socket_error,
+                        "getaddrinfo() argument 2 must be integer or string");
         goto err;
     }
     memset(&hints, 0, sizeof(hints));