diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py index 7d8b33a2ba4..053c6fe7be5 100644 --- a/Lib/test/test_socket.py +++ b/Lib/test/test_socket.py @@ -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) diff --git a/Misc/NEWS b/Misc/NEWS index 268e43800de..babe8383a27 100644 --- 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. diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index 414994e181f..506a2d320b6 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -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));