#8853: Allow port to be of type long for socket.getaddrinfo()
diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py
index 7d8b33a..053c6fe 100644
--- 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
index 268e438..babe838 100644
--- 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
index 414994e..506a2d3 100644
--- 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));