[Python-checkins] cpython (2.7): #8853: Allow port to be of type long for socket.getaddrinfo()

petri.lehtinen python-checkins at python.org
Thu Dec 20 20:11:23 CET 2012


http://hg.python.org/cpython/rev/39803c20c9bf
changeset:   80962:39803c20c9bf
branch:      2.7
user:        Petri Lehtinen <petri at digip.org>
date:        Thu Dec 20 21:06:14 2012 +0200
summary:
  #8853: Allow port to be of type long for socket.getaddrinfo()

files:
  Lib/test/test_socket.py |   3 ++-
  Misc/NEWS               |   2 ++
  Modules/socketmodule.c  |  10 +++++++---
  3 files changed, 11 insertions(+), 4 deletions(-)


diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py
--- a/Lib/test/test_socket.py
+++ b/Lib/test/test_socket.py
@@ -644,9 +644,10 @@
         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)
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -160,6 +160,8 @@
 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.
 
diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c
--- a/Modules/socketmodule.c
+++ b/Modules/socketmodule.c
@@ -4090,15 +4090,19 @@
                         "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));

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list